Untitled
unknown
python
2 years ago
977 B
6
Indexable
import numpy as np from scipy.optimize import minimize def objective(x): # Calculate the objective function value f = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2] return f def constraint1(x): # Calculate the constraint function value return x[0] * x[1] * x[2] * x[3] - 25.0 def constraint2(x): # Calculate the constraint function value sum_sq = 40.0 for i in range(4): sum_sq = sum_sq - x[i]**2 return sum_sq # Initialize the design variables x0 = [1, 5, 5, 1] # Set the bounds for the design variables b = (1.0, 5.0) bnds = (b, b, b, b) # Set the constraints con1 = {"type": "ineq", "fun": constraint1} con2 = {"type": "eq", "fun": constraint2} cons = [con1, con2] # Run the optimization solution = minimize(objective, x0, method="SLSQP", bounds=bnds, constraints=cons) # Print the results print(f"Optimal design variables: {solution.x}") print(f"Objective function value: {solution.fun}")
Editor is loading...