Originally posted by: SystemAdmin
[rastogi said:]
Didier,
I was able to solve the model as per your example, iteratively. When I solve the model independently, the output is written to the database, but when I am solving the model through the script, the output is not written in the database. I am using following code
[b]For Looping [/b]
main {
for(var i = 0; i <= 4; i++) {<br />
var source = new IloOplModelSource("modelFile.mod");
var cplex = new IloCplex();
var def = new IloOplModelDefinition(source);
var opl = new IloOplModel(def,cplex);
var data = new IloOplDataSource("dataFile.dat");
var data1 = new IloOplDataElements();
data1.loc=i;
opl.addDataSource(data1);
opl.addDataSource(data);
opl.generate();
if (cplex.solve()) {
writeln("OBJ = " + cplex.getObjValue());
} else {
writeln("No solution");
}
opl.end();
data.end();
data1.end();
def.end();
cplex.end();
source.end();
}
}
[b]Model File[/b]
int loc=...;
{string} Gasolines = ...;
{string} Oils = ...;
tuple gasType {
string name;
float demand;
float price;
float octane;
float lead;
}
tuple oilType {
string name;
float capacity;
float price;
float octane;
float lead;
}
{gasType} GasData = ...;
execute {
writeln("Node " + loc);
writeln("GasData " + GasData );
}
tuple result {
string oil;
string gas;
float blend;
float a;
}
{oilType} OilData = ...;
gasType Gas[Gasolines] = [ g.name : g | g in GasData ];
oilType Oil[Oils] = [ o.name : o | o in OilData ];
/* Alternate way to initialize the indexing arrays 'Gas', 'Oil':
gasType Gas[Gasolines];
execute {
for(var g in GasData) {
Gas[g.name] = g
}
}
oilType Oil[Oils];
execute {
for(var o in OilData) {
Oil[o.name] = o
}
}
*/
float MaxProduction = ...;
float ProdCost = ...;
dvar float+ a[Gasolines];
dvar float+ Blend[Oils][Gasolines];
maximize
sum( g in Gasolines , o in Oils )
(Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
- sum( g in Gasolines ) a[g]+5;
subject to {
ctDemand: forall( g in Gasolines )
sum( o in Oils )
Blend[o][g] == Gas[g].demand + 10 * a[g];
ctCapacity: forall( o in Oils )
sum( g in Gasolines )
Blend[o][g] <= Oil[o].capacity;<br />
ctMaxProd: sum( o in Oils , g in Gasolines )
Blend[o][g] <= MaxProduction;<br />
ctOctane: forall( g in Gasolines )
sum( o in Oils )
(Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
ctLead: forall( g in Gasolines )
sum( o in Oils )
(Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;<br />}
{result} Result =
{ <o,g,Blend[o][g],a[g]> | o in Oils, g in Gasolines };
execute DISPLAY_RESULT{
writeln("Result = ",Result)
}
[b]Data File[/b]
DBConnection db("odbc","OPT//");
Gasolines from DBRead(db,"SELECT name FROM GasData " );
Oils from DBRead(db,"SELECT name FROM OilData WHere Octane>=?")(loc);
GasData from DBRead(db,"SELECT Name,demand, price, Octane,Lead FROM GasData WHere Octane>=?")(loc);
OilData from DBRead(db,"SELECT Name,capacity, price, Octane,Lead FROM OilData WHere Octane>=?")(loc);
MaxProduction = 14000;
ProdCost = 4;
DBExecute(db,"drop table Result");
DBExecute(db,"create table Result(oil varchar(10), gas varchar(10), blend real, a real)");
Result to DBUpdate(db,"INSERT INTO Result(oil,gas,blend,a) VALUES(?,?,?,?)");
#DecisionOptimization#OPLusingCPLEXOptimizer