opti production avec contraintes

This commit is contained in:
Dumbobelix 2023-12-10 23:57:18 +01:00
parent 96c90979d9
commit 66cb5f32a7

View file

@ -3,6 +3,10 @@ import matplotlib.pyplot as plt
import PyQt5 as qt import PyQt5 as qt
ptot = 1000 # [MW] ptot = 1000 # [MW]
p2max = 400 # [MW]
p23max = 500
t21 = 0.4545
t22 = 0.8182
def C1(x): def C1(x):
return 30*x + 0.01*x**2 return 30*x + 0.01*x**2
@ -11,7 +15,13 @@ def C2(x):
return 20*x + 0.02*x**2 return 20*x + 0.02*x**2
def f(x): def f(x):
return C1(x[0]) + C2(x[1]) + x[-1] * (ptot - x[0] - x[1]) return C1(x[0]) + C2(x[1]) + x[2] * (ptot - x[0] - x[1])
def f2(x):
return f(x[0:3]) - abs(x[3]) * (p2max - x[1])
def f3(x):
return f(x[0:3]) - abs(x[3]) * (p23max - t21 * x[0] - t22 * x[1])
def grad(f, x, h=1e-4): def grad(f, x, h=1e-4):
res = [] res = []
@ -27,21 +37,27 @@ def norm(x):
return np.sqrt(n) return np.sqrt(n)
def g(x): def g(x):
return norm(grad(f, x)) return norm(grad(f, x, h=1e-5))
def minize(f, x0, h=1e-4, step=1e-1, tol=1e-8, N=1e4, echo=False): def g2(x):
return norm(grad(f2, x, h=1e-6))
def g3(x):
return norm(grad(f3, x, h=1e-6))
def minimize(f, x0, h=1e-4, step=1e-1, tol=1e-8, N=1e4, echo=False):
x = x0 x = x0
g = grad(f, x, h) g = grad(f, x, h)
print(g)
n = 0 n = 0
prev = norm(g) + 2*tol prev = norm(g) + 2*tol
print(prev, norm(g), abs(norm(g) - prev))
while abs(norm(g) - prev) > tol: while abs(norm(g) - prev) > tol:
n += 1 n += 1
prev = norm(g) prev = norm(g)
for i in range(len(x)): for i in range(len(x)):
x[i] -= g[i] * step x[i] -= g[i] * step
g = grad(f, x, h) g = grad(f, x, h)
if (n % 100 == 0) and echo: if (n % 100 == 0) and echo:
print("Itération ", n) print("Itération ", n)
print("norm(g) = ", norm(g)) print("norm(g) = ", norm(g))
@ -50,11 +66,22 @@ def minize(f, x0, h=1e-4, step=1e-1, tol=1e-8, N=1e4, echo=False):
print("g = ", g) print("g = ", g)
if n > N: if n > N:
return x return x
return x return x
#print(f([500, 500, 40])) def custom_minimize(f, x0):
#print(f([450, 450, 35])) res_app = minimize(f, x0, step=5e-1)
#print(C1(500)) print(res_app)
#print(C2(500)) res_app = minimize(f, res_app, step=1e-3, tol=1e-12, h=1e-5)
print(minize(g, [0, 0, 0])) print(res_app)
res_app = minimize(f, res_app, step=1e-5, tol=1e-14, h=1e-5)
print(res_app)
res_app = minimize(f, res_app, step=1e-6, tol=1e-16, h=5e-6)
print(res_app)
return res_app
print(minimize(g, [0, 0, 0]))
custom_minimize(g2, [0, 0, 0, 0.01])
custom_minimize(g3, [0, 0, 0, 0.01])