
Write a program in python Using swarm basic optimization with arguments to find a minimum of Rosenbrook function, To find the minimum of the test function y = x – 2sin(x).
set up the bound for the value [-5.12, 5.12].
Use Swarm to find the value of X which minimizes the test function.
Use the default hyperparameters.
# hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
![Using Arguments
Arguments can either be passed in using a tuple or a dictionary, using the kwargs={} paradigm. First
lets optimize the Rosenbrock function using keyword arguments. Note in the definition of the
Rosenbrock function above, there were two arguments that need to be passed other than the
design variables, and one optional keyword argument, a, b, and c, respectively
[7]: from pyswarms.single.global_best import GlobalBest PSO
#instatiate the optimizer
x_max = 10 np.ones (2)
x_min = -1 * x_max
*
bounds = (x_min, x_max)
options {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
optimizer = GlobalBest PSO(n_particles=10, dimensions=2, options=options, bounds-bounds)
# now run the optimization, pass a=1 and b=100 as a tuple assigned to args
cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, a=1, b=100, c=0)](https://content.bartleby.com/qna-images/question/84b9759d-3bd7-481b-9da5-01ee09be0953/b81a5524-dbcb-45f1-bd84-354f980f3fc3/cywhn8b_thumbnail.png)
![Basic Optimization with Arguments
Here, we will run a basic optimization using an objective function that needs parameterization. We
will use the single. GBestPSO and a version of the rosenbrock function to demonstrate
[6]: # import modules
import numpy as np
# create a parameterized version of the classic Rosenbrock unconstrained optimzation function
def rosenbrock_with_args(x, a, b, c=0):
f = (a - x[:, 0]) ** 2 + b * (x[:, 1] x[:, 0] ** 2) ** 2 + c
return f](https://content.bartleby.com/qna-images/question/84b9759d-3bd7-481b-9da5-01ee09be0953/b81a5524-dbcb-45f1-bd84-354f980f3fc3/xmbn7cq_thumbnail.png)

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

What if the swarm was 2-dimentional and the max bound was 10* np.ones(2) and minimum bound = -1 *max.
How can I write a helper function that, given a
every element’s value is sine of the corresponding element’s value in A. For instance,
given array A=(e1 e2, e3), the helper function will return an array B=(sine(e1), sine(e2),
sine(e3)).
What if the swarm was 2-dimentional and the max bound was 10* np.ones(2) and minimum bound = -1 *max.
How can I write a helper function that, given a
every element’s value is sine of the corresponding element’s value in A. For instance,
given array A=(e1 e2, e3), the helper function will return an array B=(sine(e1), sine(e2),
sine(e3)).
- write the following usong for loopsarrow_forwardin python with a print statmentarrow_forwardWrite the code in python to define the function has_adjacent_repeats(mystr), which takes a string parameter and returns a boolean result. - If mystr has at least one instance of adjacent characters being equal, return True - Otherwise, return False Hint: You can iterate over the positive indices i for characters in mystr: 1, 2, 3, ...., len(mystr)-1 with a for loop. For each i, if the character at index i matches the character at index i - 1, return True. Otherwise, if no doubled character is found in the entire string, return False. For example: Test Result if not (has_adjacent_repeats("NOODLES") is True): print("error") if not (has_adjacent_repeats("Bananas") is False): print("shwoopsie") if not (has_adjacent_repeats("Hanoverr") is True): print("error")arrow_forward
- Without importing any Lib:arrow_forwardIn python, The function slice_end takes a string parameter, word, and a positive integer n; the function returns a slice of length n from the end of word. However, if word has length less than n, then the None object is returned instead. Hint: Notice that returning the empty string when n is zero might be a special case. You can use an if-elif-else decision structure and the slicing operator [ : ] to solve this problem. For example: Test Result word = "pineapple" n = 5 print(slice_end(word, n)) apple word = "too short" n = 20 print(slice_end(word, n)) None word = "llama" n = 0 print(slice_end(word, n)) word = "Saturn" n = 6 print(slice_end(word, n)) Saturnarrow_forwardWrite a function using Java Function Name: winInRowParameters: board: 2D integer array, row: integer, piece: integerReturn: booleanAssume board is valid 2D int array, row is valid index in the board, piece is X==1/O==2Look at indicated row at given index in board. If that row has at least 3 consecutive entries withgiven type of piece (X/O) (3 in a row XXX, OOO), then return true, otherwise false.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





