In: Computer Science
Using Python write a function that implements the following two-dimensional objective function: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22 . Determine how many local minimums and maximums and their location for this objective function.
Solution :
Objective function from the question: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22
Function which implements the objective function :
Data Used :
Note this can be replace with any data and the solution will still
Step 1 : Calculating the value of the function
will be using the argrelextrema function from scipy.signals lib. This is the most effective , easy and simple method for finding minima and maxima for multi-var. objective functions. It takes two arguments the function values (in out case the array calculated in step 1) and the np.less and np.greater value for minima and maxima respectively. The function doesnt return values but the index.
Step 2 : Calculating the minima
step 3: Calculating Maxima
step 4: Combining all steps into a single function :
Code :
from scipy.signal import argrelextrema
import numpy as np
x = np.random.rand(20)
y = np.random.rand(20)
def get_min_max(x,y):
def f_x_y(x,y):
return (x**2 + y -
11)**2 + (x + y**2 - 7 )**22
f_vals = []
for _x,_y in zip(x,y):
f_vals.append(f_x_y(_x,_y))
#this will make our life more easier.
f_vals = np.asarray(f_vals)
#### Calculating Minima
index =
argrelextrema(np.asarray(f_vals),np.less)
print(f'No. of minima = {len(index[0])}')
print(f'values of minima =
{f_vals[index[0]]}')
### Calculating Maxima
index =
argrelextrema(np.asarray(f_vals),np.greater)
print(f'No. of maxima = {len(index[0])}')
print(f'values of maxima =
{f_vals[index[0]]}')
get_min_max(x,y)
Code in textformat:
from scipy.signal import argrelextrema
import numpy as np
x = np.random.rand(20)
y = np.random.rand(20)
def get_min_max(x,y):
def f_x_y(x,y):
return (x**2 + y - 11)**2 + (x + y**2 - 7 )**22
f_vals = []
for _x,_y in zip(x,y):
f_vals.append(f_x_y(_x,_y))
#this will make our life more easier.
f_vals = np.asarray(f_vals)
#### Calculating Minima
index = argrelextrema(np.asarray(f_vals),np.less)
print(f'No. of minima = {len(index[0])}')
print(f'values of minima = {f_vals[index[0]]}')
### Calculating Maxima
index = argrelextrema(np.asarray(f_vals),np.greater)
print(f'No. of maxima = {len(index[0])}')
print(f'values of maxima = {f_vals[index[0]]}')
get_min_max(x,y)
Implementing Our Own Functions
from numpy import diff,sign
def get_minmax(f_vals):
min_index = (diff(sign(diff(f_vals))) > 0).nonzero()[0] + 1
max_index = (diff(sign(diff(f_vals))) < 0).nonzero()[0] + 1
return min_index,max_index
# remember the function returns an array
min_index,max_index = get_minmax(f_vals)
Explanation
let the function values be arr = [x,x2,x3,x4]
Say u had a a local minima at ith position and a maxima at (i+1)th position
then arr(i+i) - a(i) would be a +ve val indicating an minima
if now the positios of the minima and maxima were interchnaged it would produce a -ve number
So. conditions for finding minima and maxima are
if value > 0 then minima
if value < 0 then maxima
we also need to remove the flat area areas which dont have a minima or maxia and make them 0. this is done by the
*********************** If the answer helps then please Upvote.