Wie man die Boolesche Logik mit Pyomo löstPython

Python-Programme
Anonymous
 Wie man die Boolesche Logik mit Pyomo löst

Post by Anonymous »

Ich muss eine Berechnung in die Pyomo -Optimierung einbeziehen, aber ich erhalte eine Nachricht, dass sie eine Boolesche Logik wird. Ich verwende GLPK, das für die lineare Programmierung geeignet ist. Gibt es eine Alternative, um diese Berechnung in die Optimierung aufzunehmen? Unten ist ein sehr vereinfachtes Beispiel. < /P>

Code: Select all

spread_min_150  =  0.01
spread_150_max  =  0.05

SPOT = 600
avg = 10
h = 730

def price(SPOT, POSITION, spread_min_150, spread_150_max):
if SPOT < 150:
spread_price  =  SPOT * (1 + spread_min_150) if POSITION >= 0 else SPOT * (1 - spread_min_150)
else:
spread_price  =  SPOT * (1 + spread_150_max) if POSITION >= 0 else SPOT * (1 - spread_150_max)
return spread_price

model = pyo.ConcreteModel()
model.a = pyo.Var(domain=pyo.NonNegativeReals)
model.b = pyo.Var(domain=pyo.NonNegativeReals)

mcp = avg * 730 - (model.a*730 + model.b*730)

B = mcp * price(SPOT, mcp, spread_min_150, spread_150_max)

model.obj = pyo.Objective(expr=B, sense=pyo.minimize)

model.obj.pprint()

opt = pyo.SolverFactory('glpk') # ('glpk', executable='/usr/bin/glpsol')
result = opt.solve(model)
print(result)
model.display()
< /code>
Die Fehlermeldung ist unten. < /p>
---------------------------------------------------------------------------
PyomoException                            Traceback (most recent call last)
 in ()
20 mcp = avg * 730 - (model.a*730 + model.b*730)
21
---> 22 B = mcp * price(SPOT, mcp, spread_min_150, spread_150_max)
23
24 model.obj = pyo.Objective(expr=B, sense=pyo.minimize)

1 frames
/usr/local/lib/python3.10/dist-packages/pyomo/core/expr/relational_expr.py in __bool__(self)
46         if self.is_constant():
47             return bool(self())
---> 48         raise PyomoException(
49             """
50 Cannot convert non-constant Pyomo expression (%s) to bool.

PyomoException: Cannot convert non-constant Pyomo expression (0  >> m.x = Var()
>>> if m.x >= 1:
...     pass
and
>>> m.y = Var()
>>> if m.y in [m.x, m.y]:
...     pass
would both cause this exception.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post