Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use eggholder.py. (code below) for this question v import numpy as np def eggholder(x, y): Takes in either a scalar x and y, or

Use eggholder.py. (code below) for this question

v

import numpy as np

def eggholder(x, y):

"""

Takes in either a scalar x and y, or a 1-D numpy array for both x and y.

"""

if (isinstance(x, np.ndarray) and not isinstance(y, np.ndarray)) or (

isinstance(y, np.ndarray) and not isinstance(x, np.ndarray)):

pass

result = -(y + 47) * np.sin(np.sqrt(np.abs(x / 2 + (y + 47)))) - x * np.sin(np.sqrt(np.abs(x - (y + 47))))

if isinstance(x, np.ndarray):

outside = (np.abs(x) > 512) | (np.abs(y) > 512)

result[outside] = 2000

return result

else:

return 2000 if (abs(x) > 512 or abs(y) > 512) else result

^

code screen shot image text in transcribed

image text in transcribed

Use python 3.8 thank you

import numpy as np def eggholder(x, y): Takes in either a scalar x and y, or a 1-D numpy array for both x and y. if (1sinstance(x, np.ndarray) and not 1sinstance(y, np.ndarray)) or ( 1sinstance(y, np.ndarray) and not isinstance(x, np.ndarray)): pass result = -(y + 47) np.sin(np. sqrt(np.abs(x / 2 + (y + 47)))) - X * np.sin(np. sqrt(np.abs(x - (y + 47)))) 1f 1sinstance(x, np.ndarray): outside = (np.abs(x) > 512) | (np.abs(y) > 512) result(outside) = 2000 return result else: return 2000 if (abs(x) > 512 or abs(y) > 512) else result Download the file eggholder.py. This file contains a single function egg- holder which takes in an x and y value, and returns a single value. A visual- ization of the function is shown here: Egghelder Function 1500 1000 500 1,22) -500 600 400 200 -1000 600 400 200 0 -200 -200 400 -600 400 x1 -600 x2 The goal here is to use fmin to see how well it can optimize this function. Put all of your code inside the same file eggholder.py. 1. Write a function minimize_eggholder (guess, max_calls=100) which takes in an (x,y) guess as guess, and then uses the fmin func- tion from scipy to attempt to minimize the function. It should set maxfun equal to max_calls to limit the number of function evalua- tions. It should return two values: The (x,y) coordinate which minimizes the function, followed by the actual value at the minimum. 2. On the interval [-512, 512] x [-512, 512], the global minimum of the function is at (512, 404.2319). Write some code which randomly gen- erates 1000 points in the range [-512, 512] x [-512, 512] and runs minimize_eggholder with those points as an initial guess. It should then plot a histogram of the absolute difference between the mini- mum obtained from minimize_eggholder versus the true global minimum. Set bins=25. import numpy as np def eggholder(x, y): Takes in either a scalar x and y, or a 1-D numpy array for both x and y. if (1sinstance(x, np.ndarray) and not 1sinstance(y, np.ndarray)) or ( 1sinstance(y, np.ndarray) and not isinstance(x, np.ndarray)): pass result = -(y + 47) np.sin(np. sqrt(np.abs(x / 2 + (y + 47)))) - X * np.sin(np. sqrt(np.abs(x - (y + 47)))) 1f 1sinstance(x, np.ndarray): outside = (np.abs(x) > 512) | (np.abs(y) > 512) result(outside) = 2000 return result else: return 2000 if (abs(x) > 512 or abs(y) > 512) else result Download the file eggholder.py. This file contains a single function egg- holder which takes in an x and y value, and returns a single value. A visual- ization of the function is shown here: Egghelder Function 1500 1000 500 1,22) -500 600 400 200 -1000 600 400 200 0 -200 -200 400 -600 400 x1 -600 x2 The goal here is to use fmin to see how well it can optimize this function. Put all of your code inside the same file eggholder.py. 1. Write a function minimize_eggholder (guess, max_calls=100) which takes in an (x,y) guess as guess, and then uses the fmin func- tion from scipy to attempt to minimize the function. It should set maxfun equal to max_calls to limit the number of function evalua- tions. It should return two values: The (x,y) coordinate which minimizes the function, followed by the actual value at the minimum. 2. On the interval [-512, 512] x [-512, 512], the global minimum of the function is at (512, 404.2319). Write some code which randomly gen- erates 1000 points in the range [-512, 512] x [-512, 512] and runs minimize_eggholder with those points as an initial guess. It should then plot a histogram of the absolute difference between the mini- mum obtained from minimize_eggholder versus the true global minimum. Set bins=25

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

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions