// File: examples/src/transport.cpp
// Version 9.0
// --------------------------------------------------------------------------
// Copyright (C) 1999-2003 by ILOG.
// All Rights Reserved.
// Permission is expressly granted to use this example in the
// course of developing applications that use ILOG products.
// --------------------------------------------------------------------------
//
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
typedef IloArray<IloNumArray> NumMatrix;
typedef IloArray<IloNumVarArray> NumVarMatrix;
int main(int argc, char** argv) {
if (argc <= 1) {
cerr << "Usage: " << argv[0] << " <model>" << endl;
cerr << " model = 0 -> convex piecewise linear model, " << endl;
cerr << " model = 1 -> concave piecewise linear model. [default]" <<
endl;
}
IloBool convex;
if (argc <= 1)
convex = IloFalse;
else
convex = atoi(argv[1]) == 0 ? IloTrue : IloFalse;
IloEnv env;
try {
IloInt i, j;
IloModel model(env);
IloInt nbDemand = 4;
IloInt nbSupply = 3;
IloNumArray supply(env, nbSupply, 1000., 850., 1250.);
IloNumArray demand(env, nbDemand, 900., 1200., 600., 400.);
NumVarMatrix x(env, nbSupply);
NumVarMatrix y(env, nbSupply);
for(i = 0; i < nbSupply; i++) {
x[i] = IloNumVarArray(env, nbDemand, 0, IloInfinity, ILOFLOAT);
y[i] = IloNumVarArray(env, nbDemand, 0, IloInfinity, ILOFLOAT);
}
for(i = 0; i < nbSupply; i++) { // supply must meet demand
model.add(IloSum(x[i]) == supply[i]);
}
for(j = 0; j < nbDemand; j++) { // demand must meet supply
IloExpr v(env);
for(i = 0; i < nbSupply; i++)
v += x[i][j];
model.add(v == demand[j]);
v.end();
}
if (convex) {
for(i = 0; i < nbSupply; i++) {
for(j = 0; j < nbDemand; j++) {
model.add(y[i][j] == IloPiecewiseLinear(x[i][j],
IloNumArray(env, 2, 200., 400.),
IloNumArray(env, 3, 30., 80., 130.),
0, 0)
);
}
}
}
else {
for(i = 0; i < nbSupply; i++) {
for(j = 0; j < nbDemand; j++) {
model.add(y[i][j] == IloPiecewiseLinear(x[i][j],
IloNumArray(env, 2, 200., 400.),
IloNumArray(env, 3, 120., 80., 50.),
0, 0)
);
}
}
}
IloExpr obj(env);
for(i = 0; i < nbSupply; i++) {
obj += IloSum(y[i]);
}
model.add(IloMinimize(env, obj));
IloCplex cplex(env);
cplex.extract(model);
cplex.exportModel("transport.lp");
cplex.solve();
env.out() << " - Solution: " << endl;
for(i = 0; i < nbSupply; i++) {
env.out() << " " << i << ": ";
for(j = 0; j < nbDemand; j++) {
env.out() << cplex.getValue(x[i][j]) << "\t";
}
env.out() << endl;
}
env.out() << " Cost = " << cplex.getObjValue() << endl;
} catch(IloException& e) {
cerr << "ERROR: " << e.getMessage() << endl;
} catch (...) {
cerr << "Error" << endl;
}
env.end();
return 0;
} // END main
|