Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the current code I have. How do I edit it so I can input multiple variables for my objective function and input a

Below is the current code I have. How do I edit it so I can input multiple variables for my objective function and input a gradient rather than a 1D vector for the derivative function?
For example, I want my objective function to be f(x1,x2)= x1^2+x2^2+ x1*x2
import numpy as np
#Algorithm 4.3 Bracketing Phase for Line Search Algorithm
#The bracketing phase finds an interval within which we are certain
#to find a point that satisfies the strong Wolfe conditions.
def line_search_bracketing(phi, phi_prime, alpha_init, mu1, mu2, sigma):
#Inputs:
#phi (objective function)
#phi_prime (objective function derivative)
#alpha_init (initial step size)
#mu1(decrease factor 01)
a1=0 #Define Initial Bracket <--this is the initial point as well?
phi0= phi(0)
phi0_prime = phi_prime(0)
a2= alpha_init
phi1= phi0
phi1_prime = phi0_prime #Used in Pinpoint
first = True
#Iteration Loop to Find Suitable Step Size (to satisfy Wolfe conditions)
while True:
phi2= phi(a2) #Calculate value for step point
phi2_prime = phi_prime(a2) #Calculate derivative for step point
#if step point is greater than sufficient decrease line OR greater than initial point
if (phi2> phi0+ mu1* a2* phi0_prime) or (not first and phi2> phi1):
a_star = pinpoint(phi, phi_prime, a1, a2) #run pinpoint function
return a_star
phi2_prime_abs = np.abs(phi2_prime)
#if magnitude of slope of step point is close enough to zero then exit line search
if phi2_prime_abs <=-mu2* phi0_prime:
return a2
#if magnitude of slope of step point is too great, call pinpoint function
elif phi2_prime >=0:
a_star = pinpoint(phi, phi_prime, a2, a1)
return a_star
#Shift all values over to search again
a1= a2
phi1= phi2
phi1_prime = phi2_prime
a2= sigma * a2
first = False
#Algorithm 4.4 Pinpoint function for the line search algorithm
#The pinpointing phase finds a point that satisfies the strong Wolfe
#conditions within the interval provided by the bracketing phase.
def pinpoint(phi, phi_prime, a_low, a_high, mu1=0.25, mu2=0.9):
k =0
while True:
# Cubic interpolation - Call function to locate potential minimum between a1 and a2
a_p = cubic_interpolation(phi, phi_prime, a_low, a_high)
phi_p = phi(a_p)
phi_p_prime = phi_prime(a_p)
#Check wolfe conditions and update interval accordingly.
#If conditions are met, return step size
if phi_p > phi(0)+ mu1* a_p * phi(0):
a_high = a_p
elif phi_p_prime >= mu2* phi(0):
return a_p
elif phi_p_prime *(a_high - a_low)>=0:
a_high = a_low
a_low = a_p
k +=1
#Cubic Interpolation - Choses 3 points within the interval, connects the points using cubic polynomial fit
#and finds the minimum of the three points to return the interpolated step size.
def cubic_interpolation(phi, phi_prime, a_low, a_high):
# Perform cubic interpolation using polyfit
alpha_values = np.array([a_low, 0.5*(a_low + a_high), a_high]) #Bisection Method (array of 3 points)
phi_values = np.array([phi(alpha) for alpha in alpha_values]) #Find phi values of three points
phi_prime_values = np.array([phi_prime(alpha) for alpha in alpha_values]) #Find sloped of three points
# Fit a cubic polynomial to the data
coefficients = np.polyfit(alpha_values, phi_values, 3)
# Find the minimum of the cubic polynomial
a_p =-coefficients[1]/(3* coefficients[0])
return a_p #return minimum point between original a1 and a2 as calcuulated by cubic interpolation
#-----------------------------------------------------------------------------------------------------------
# Define your phi and phi_prime functions
#Objective Function
def phi(alpha):
return np.sum(alpha -2)**2+1
#Objective Function Derivative
def phi_prime(alpha):
return 2*(alpha -2)
# Set initial parameters
#Initial Step Size
alpha_init =0.1
#Decrease Factor 01
sigma =2
# Perform line search bracketing
optimal_step = line_search_bracketing(phi, phi_prime, alpha_init, mu1, mu2, sigma)
print("Optimal Step:", optimal_step)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions