习题5.4
import numpy as np
from scipy.optimize import minimize
def objective_function(x):
return np.sum(np.sqrt(x))
def linear_constraint(x):
weights = np.arange(1, 101)
return 1000 - np.dot(x, weights)
constraints = [
{'type': 'ineq', 'fun': lambda x: 10 - x[0]},
{'type': 'ineq', 'fun': lambda x: 20 - x[0] - 2 * x[1]},
{'type': 'ineq', 'fun': lambda x: 30 - x[0] - 2 * x[1] - 3 * x[2]},
{'type': 'ineq', 'fun': lambda x: 40 - x[0] - 2 * x[1] - 3 * x[2] - 4 * x[3]},
{'type': 'ineq', 'fun': linear_constraint}]
initial_guess = np.zeros(100)
bounds = [(0, None)] * 100
minimization_result = minimize(objective_function, initial_guess, constraints=constraints, bounds=bounds)
def negative_objective_function(x):
return -objective_function(x)
maximization_result = minimize(negative_objective_function, initial_guess, constraints=constraints, bounds=bounds)
print('Optimal solution:', maximization_result.x)
print('Objective value at optimal solution:', -maximization_result.fun)
print('学号:',3023)
结果
Optimal solution: [8.82652025e+00 3.73852721e-02 7.67676881e-01 7.16007244e+00
1.94939440e-01 5.40062434e+00 4.91415859e+00 4.45445305e+00
2.15480635e+00 7.22207062e-03 2.54777735e-02 7.84749422e-02
3.11519217e+00 2.71004602e+00 2.69158393e-02 4.05869821e+00
3.02250249e+00 1.47139792e+00 4.38786017e-02 2.40551675e-02
1.40485774e+00 2.45590320e-02 2.93991608e-03 4.67101847e-01
1.03957331e+00 9.73991097e-01 7.22970842e-01 2.08776713e-02
6.00564889e-01 5.75747646e-01 4.88166109e-01 3.52482236e-02
4.90478126e-01 3.39490411e-01 3.67571039e-03 1.90311105e-02
4.51313963e-01 2.51508049e-01 2.44526909e-02 1.23484403e-02
3.36737423e-01 2.78688832e-01 3.05562400e-03 1.57130638e-02
5.04309450e-03 4.10890569e-01 2.26339485e-01 3.82384113e-04
2.08848641e-01 1.95141020e-01 1.03785686e-03 1.44101654e-02
2.27488309e-02 1.43758398e-01 1.17104288e-03 1.29444396e-01
2.42985031e-01 1.69553273e-01 1.39391187e-01 1.88726302e-02
2.42218369e-02 1.17549200e-01 2.16884826e-01 1.38481348e-01
1.27775075e-01 6.69194471e-02 2.23146750e-03 8.64065185e-04
1.91624725e-03 9.28898430e-02 9.51312140e-02 8.43949644e-02
7.24639664e-02 9.59457402e-02 3.20710851e-02 1.88169824e-02
1.06856129e-03 8.76641196e-02 7.85802458e-02 6.26238946e-02
8.67245918e-02 7.20373133e-02 2.37962201e-02 4.52968627e-02
6.65023434e-02 5.40038015e-02 6.20663760e-02 1.96010408e-03
7.93283434e-02 8.01372611e-02 1.50016809e-02 6.43324215e-04
5.33335672e-02 7.05580884e-02 9.60272346e-04 7.24535058e-02
1.73601278e-02 4.22693909e-03 5.56531506e-02 9.23327296e-01]
Objective value at optimal solution: 50.36551134798356
学号: 3023
习题5.5
import numpy as np
from scipy.optimize import minimize
def objective_function(x):
return 2 * x[0] + 3 * x[0]2 + 3 * x[1] + x[1]2 + x[2]
def con1(x):
return 10 - (x[0] + 2 * x[0]2 + x[1] + 2 * x[1]2 + x[2])
def con2(x):
return 50 - (x[0] + x[0]2 + x[1] + x[1]2 - x[2])
def con3(x):
return 40 - (2 * x[0] + x[0]2 + 2 * x[1] + x[2])
def con4(x):
return 1-(x[0] + 2 * x[1])
def con5(x):
return x[0]2 + x[2] - 2
cons = ({'type': 'ineq', 'fun': con1},
{'type': 'ineq', 'fun': con2},
{'type': 'ineq', 'fun': con3},
{'type': 'ineq', 'fun':lambda x: -con4(x)},
{'type': 'eq', 'fun': con5})
initial_guess = np.random.rand(3)
bounds = [(0, None)] * 3
minimization_result = minimize(objective_function, initial_guess, constraints=cons, bounds=bounds)
def negative_objective_function(x):
return -objective_function(x)
maximization_result = minimize(negative_objective_function, initial_guess, constraints=cons, bounds=bounds)
print('Optimal solution:', maximization_result.x)
print('Objective value at optimal solution:', -maximization_result.fun)
print('学号:',3023)
结果
Optimal solution: [1.41421356e+00 1.28472904e+00 2.24835282e-14]
Objective value at optimal solution: 14.333142938637696
学号: 3023