I'd like to be able to fix an error in the Python code below relating to "wmax". I'm trying to find a solution of weights (the w[i]) such that all the weights are less than or equal to 2.5.
Here is the code:
import numpy as np
import pandas as pd
from gekko import GEKKO
x1=pd.DataFrame([1,5,2,3,2,5,2,6,
8,4,9,9,7,5,2,4]).iloc[:,0]
m = GEKKO(remote=False)
M1 = 5
TM1 = 0.05
S1=3
TS1=0.05
n = len(x1)
a0,a1,a2,a3,a4 = m.Array(m.Var,5,lb=-500,ub=500)
w = [m.Intermediate(m.exp(a0+a1*x1[i]
+a2*x1[i]**2
+a3*x1[i]**3
+a4*x1[i]**4))
for i in range(n)]
w2 = [w[i]**2 for i in range(n)]
wx1 = [w[i]*x1[i] for i in range(n)]
wx1M1 = [w[i]*(x1[i]-M1)**2 for i in range(n)]
wmax= [(((n/m.sum(w))*w[i])<=2.5) for i in range(n)]
m.Equation(m.abs3(m.sum(wx1)/n-M1)<=TM1)
m.Equation(m.abs3(m.sqrt(m.sum(wx1M1)/(n-1))-S1)<=TS1)
m.Equation(m.sum(w)==n)
m.Equation(m.sum(wmax)==n)
m.Minimize(m.sum(w2))
m.options.SOLVER = 1
m.solve()
This is the error I get:
Traceback (most recent call last):
File "C:\...\sas.py", line 43, in <module>
m.solve()
File "C:\...\gekko\gekko.py", line 2140, in solve
---------------------------------------------
APMonitor, Version 1.0.0
APMonitor Optimization Suite
---------------------------------------------
@error: Duplicate Names
error: duplicate variable declarations
intermediate: 1
intermediate: 2
STOPPING. . .
The problem with
wmaxis the inequality<=in the list comprehension.Instead, separate the definition of
wmaxas a variable and include an upper bound on the value. The solver attempts to find a solution with this modification, but fails to converge. Try removing them.abs3()functions:with an equivalent reformulation:
This produces a successful solution in 37 iterations:
Here is the complete code: