microproduct/soilMoisture_geo_sar/test_tool/minimize.py

156 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: UTF-8 -*-
"""
@Project onestar
@File minimize.py
@Contact
@Author SHJ
@Date 2021/11/24 15:46
@Version 1.0.0
"""
from scipy.optimize import minimize
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
#目标函数:
def func(args):
fun = lambda x: 10 - x[0]**2 - x[1]**2
return fun
#约束条件,包括等式约束和不等式约束
def con(args):
cons = ({'type': 'ineq', 'fun': lambda x: x[1]-x[0]**2},
{'type': 'eq', 'fun': lambda x: x[0]+x[1]})
return cons
#画三维模式图
def draw3D():
fig = plt.figure()
ax = Axes3D(fig)
x_arange = np.arange(-5.0, 5.0)
y_arange = np.arange(-5.0, 5.0)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = 10 - X**2 - Y**2
Z2 = Y - X**2
Z3 = X + Y
plt.xlabel('x')
plt.ylabel('y')
ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap='rainbow')
ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, cmap='rainbow')
ax.plot_surface(X, Y, Z3, rstride=1, cstride=1, cmap='rainbow')
plt.show()
#画等高线图
def drawContour():
x_arange = np.linspace(-3.0, 4.0, 256)
y_arange = np.linspace(-3.0, 4.0, 256)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = 10 - X**2 - Y**2
Z2 = Y - X**2
Z3 = X + Y
plt.xlabel('x')
plt.ylabel('y')
plt.contourf(X, Y, Z1, 8, alpha=0.75, cmap='rainbow')
plt.contourf(X, Y, Z2, 8, alpha=0.75, cmap='rainbow')
plt.contourf(X, Y, Z3, 8, alpha=0.75, cmap='rainbow')
C1 = plt.contour(X, Y, Z1, 8, colors='black')
C2 = plt.contour(X, Y, Z2, 8, colors='blue')
C3 = plt.contour(X, Y, Z3, 8, colors='red')
plt.clabel(C1, inline=1, fontsize=10)
plt.clabel(C2, inline=1, fontsize=10)
plt.clabel(C3, inline=1, fontsize=10)
plt.show()
# 目标函数:
def func1(args):
fun = lambda x: 10 - x[0] ** 2 - x[1] ** 2
return fun
def samper():
args = ()
args1 = ()
cons = con(args1)
x0 = np.array((1.0, 2.0)) #设置初始值,初始值的设置很重要,很容易收敛到另外的极值点中,建议多试几个值
#求解#
res = minimize(func(args), x0, method='SLSQP', constraints=cons)
#####
print(res.fun)
print(res.success)
print(res.x)
# draw3D()
drawContour()
def samper1():
fun = lambda x: np.abs(np.abs((0.7- (x[0] -0.49)**0.5)/(0.7- (x[0] +0.49)**0.5)) - 0.5*np.abs((0.7- (x[1] -0.49)**0.5)/(0.7- (x[1] +0.49)**0.5)))
# fun = lambda x: np.abs(x[0]**2 -x[1]**2)
x_min = 1
x_max = 20
cons = ({'type': 'ineq', 'fun': lambda x: x[0] - x_min},
{'type': 'ineq', 'fun': lambda x: -x[0] + x_max},
{'type': 'ineq', 'fun': lambda x: x[1] - x_min},
{'type': 'ineq', 'fun': lambda x: -x[1] + x_max})
bnds = ((0, 30), (0, 30))
res = minimize(fun, (3, 3), method='SLSQP', bounds=bnds,constraints=cons)
print("res::",res)
x_arange = np.linspace(0, 25, 256)
y_arange = np.linspace(0, 25, 256)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = np.abs(np.abs((0.7- (X -0.49)**0.5)/(0.7- (X +0.49)**0.5)) - 0.5*np.abs((0.7- (Y -0.49)**0.5)/(0.7- (Y +0.49)**0.5)))
# Z1 = np.abs(X**2 - Y**2)
plt.xlabel('x')
plt.ylabel('y')
plt.contourf(X, Y, Z1, 8, alpha=0.75, cmap='rainbow')
C1 = plt.contour(X, Y, Z1, 8, colors='black')
plt.clabel(C1, inline=1, fontsize=10)
plt.show()
if __name__ == "__main__":
# samper1()
s1 = 0.9414927
c1 = 0.24188292
s2 = 0.9414927
c2 = 0.24188292
t = 1.0
x_min = 1
x_max = 20
bnds = ((x_min, x_max), (x_min, x_max))
x_mid = 0.5 * (x_max - x_min)
x = [9,8]
a = (s2 - x[1]*(1 + s2))
b = np.abs((x[1] - 1)*(s2 - x[1]*(1 + s2)) / (x[1] * c2 + (x[1] - s2) ** 0.5) ** 2)- t * np.abs((x[0] - 1)*(s1 - x[0]*(1 + s1)) / (x[0] * c1 + (x[0] - s1) ** 0.5) ** 2)
fun = lambda x: np.abs(
np.abs((x[1] - 1)*(s2 - x[1]*(1 + s2)) / (x[1] * c2 + (x[1] - s2) ** 0.5) ** 2)
- t * np.abs((x[0] - 1)*(s1 - x[0]*(1 + s1)) / (x[0] * c1 + (x[0] - s1) ** 0.5) ** 2)
)
res = minimize(fun, (x_mid, x_mid), method='SLSQP', bounds=bnds)
print("res::", res)
x_arange = np.linspace(0, 25, 256)
y_arange = np.linspace(0, 25, 256)
X, Y = np.meshgrid(x_arange, y_arange)
Z1 = np.abs(
np.abs((Y - 1)*(s2 - Y*(1 + s2)) / (Y * c2 + (Y - s2) ** 0.5) ** 2)
- t * np.abs((X - 1)*(s1 - X*(1 + s1)) / (X * c1 + (X - s1) ** 0.5) ** 2)
)
# Z1 = np.abs(X**2 - Y**2)
plt.xlabel('x')
plt.ylabel('y')
plt.contourf(X, Y, Z1, 8, alpha=0.75, cmap='rainbow')
C1 = plt.contour(X, Y, Z1, 8, colors='black')
plt.clabel(C1, inline=1, fontsize=10)
plt.show()
print('done')