I am just starting with Pyomo and I have a big problem. I want to create an Abstract Model and use AMPL data format to feed it. The question is a classic transportation problem. I need to find the optimal solution for cost. M means if the shipment is impossible between a given source and destination, a large cost of M is entered. I need to convert it into AMPL data. Apart from this, I do not know how to create this abstract model. The code for this table and model are shown below. Also after reading this problem, I created the mathematical model as follows.
[the mathematical model that I created][1]
[the classic transportation problem tabel][2]
from __future__ import division
from pyomo.environ import *
model = AbstractModel()
model.I = Set()
model.J = Set()
model.a = Param(model.I)
model.b = Param(model.J)
model.cost = Param(model.I,model.J)
model.supply = Var(model.I,model.J)
def obj_expression(model):
return sum(model.supply[i,j] * model.cost[i,j] for i in model.I for j in model.J)
model.OBJ = Objective(rule=obj_expression)
def ax_constraint_rule_1(model, i):
return sum(model.supply[i,j] for j in model.J )<= model.a[i]
def ax_constraint_rule_2(model, j):
return sum(model.supply[i,j] for i in model.I )>= model.b[j]
model.AxbConstraint = Constraint(model.I, rule=ax_constraint_rule_1)
model.AxbConstraint_2 = Constraint(model.J, rule=ax_constraint_rule_2)
pyomo solve --solver=glpk test.py transportation_data.dat
model.pprint()
set I := D1 D2 D3 ;
set J := S1 S2 S3 ;
param cost :=
S1 D1 3
S1 D2 1
S2 D1 4
S2 D2 2
S2 D3 4
S3 D2 3
S3 D3 3
;
param b :=
D1 7
D2 3
D3 5 ;
param a:=
S1 5
S2 7
S3 3
;
Any help with this code? Really need help with the model creation and AMPL data construction.
Thanks anyway
=============================================== The result
File "E:/pycharm_project/test.py", line 28
pyomo solve --solver=glpk test.py transportation_data.dat
^
SyntaxError: invalid syntax```
[1]: https://i.sstatic.net/DoWXA.png
[2]: https://i.sstatic.net/Fwmjb.png
Well, you are close, I think. You have a few things to clean up.
You do NOT need model.m
and model.n
I'm not sure what you are trying to do there.
For the sets, I and J, just list them as Set(), because you are providing values for them in your AMPL data. Like:
model.I = Set()
model.J = Set()
In your formulation you are double indexing c[i,j] but in your formulation and data, c is only indexed by model.I
Similarly, in your model you are only single-indexing a[i], but you have a double index on it in your data and formulation.
In your constraints, the definitions should only hold a variable for the "for each" part, not the variable you are summing over.
Clean that stuff up, give it a whirl, comment me back if it is still broke.
Edit: A couple more items.....
I'd recommend naming your parameters and sets intuitively, such as:
model.supply
, model.cost
, etc. Makes it MUCH easier to read & troubleshoot. :)
============
Edit #2: Your code cleanup is vastly improved. A couple cleanup items remain:
In your constraints, you only need to pass variable for the "for each" side of the equation if you are summing over the other variable. You are making model.I
constraints here, so this is appropriate:
def ax_constraint_rule_1(model, i): # note removal of j
return sum(model.supply[i,j] for j in model.J ) <= model.a[i]
Note that the summation here is over j
for each i
so I changed your for loop also.
flip that around for the other constraint.
your a, b, cost, supply
don't line up with your data names. Check them all. In your data, a
appears to be cost[i, j]
your cost data is also missing some values!
In AMPL syntax, set
is not capitalized. If you get it running, it will barf and call out that error.