Originally posted by: Roni.Mohammad
Hi,
I am trying to get insight of using special order set. (SOS). I am new to SOS1 formulation and one of the challenge I am facing is that how to provide input of SOS1 information to model. I was trying to modify facility.cpp as an SOS1 formulation. One way could be done is that converting binary variable IloNumVarArray open(env, nbLocations, 0, 1, ILOINT) to IloSOS1Array sos1(env, nbLocations) and setting the constraints as well. . Not sure if that's the appropriate approach because cplex is not producing any result .
My question is- how can we modify this problem ( facility.cpp) so that we can implement SOS1 formulation . At this point, I am just looking for implement sos concept. Any help greatly appreciated.
Yes, I have seen example such as http://www-01.ibm.com/support/knowledgecenter/SSSA5P_12.2.0/ilog.odms.cplex.help/Content/Optimization/Documentation/CPLEX/_pubskel/CPLEX1217.html and http://www-01.ibm.com/support/knowledgecenter/SSSA5P_12.2.0/ilog.odms.cplex.help/Content/Optimization/Documentation/CPLEX/_pubskel/CPLEX638.html. But it hasn't been great help except understanding sos1 .
I add the code facility.cpp for your convenience.
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
typedef IloArray<IloNumArray> FloatMatrix;
typedef IloArray<IloNumVarArray> NumVarMatrix;
int
main(int argc, char **argv)
{
IloEnv env;
try {
IloInt i, j;
IloNumArray capacity(env), fixedCost(env);
FloatMatrix cost(env);
IloInt nbLocations;
IloInt nbClients;
const char* filename = "../../../examples/data/facility.dat";
if (argc > 1)
filename = argv[1];
ifstream file(filename);
if (!file) {
cerr << "ERROR: could not open file '" << filename
<< "' for reading" << endl;
cerr << "usage: " << argv[0] << " <file>" << endl;
throw(-1);
}
file >> capacity >> fixedCost >> cost;
nbLocations = capacity.getSize();
nbClients = cost.getSize();
IloBool consistentData = (fixedCost.getSize() == nbLocations);
for(i = 0; consistentData && (i < nbClients); i++)
consistentData = (cost[i].getSize() == nbLocations);
if (!consistentData) {
cerr << "ERROR: data file '"
<< filename << "' contains inconsistent data" << endl;
throw(-1);
}
IloNumVarArray open(env, nbLocations, 0, 1, ILOINT);
NumVarMatrix supply(env, nbClients);
for(i = 0; i < nbClients; i++)
supply[i] = IloNumVarArray(env, nbLocations, 0, 1, ILOINT);
IloModel model(env);
for(i = 0; i < nbClients; i++)
model.add(IloSum(supply[i]) == 1);
for(j = 0; j < nbLocations; j++) {
IloExpr v(env);
for(i = 0; i < nbClients; i++)
v += supply[i][j];
model.add(v <= capacity[j] * open[j]);
v.end();
}
IloExpr obj = IloScalProd(fixedCost, open);
for(i = 0; i < nbClients; i++) {
obj += IloScalProd(cost[i], supply[i]);
}
model.add(IloMinimize(env, obj));
obj.end();
IloCplex cplex(env);
cplex.extract(model);
cplex.solve();
cplex.out() << "Solution status: " << cplex.getStatus() << endl;
IloNum tolerance = cplex.getParam(
IloCplex::Param::MIP::Tolerances::Integrality);
cplex.out() << "Optimal value: " << cplex.getObjValue() << endl;
for(j = 0; j < nbLocations; j++) {
if (cplex.getValue(open[j]) >= 1 - tolerance) {
cplex.out() << "Facility " << j << " is open, it serves clients ";
for(i = 0; i < nbClients; i++) {
if (cplex.getValue(supply[i][j]) >= 1 - tolerance)
cplex.out() << i << " ";
}
cplex.out() << endl;
}
}
}
catch(IloException& e) {
cerr << " ERROR: " << e << endl;
}
catch(...) {
cerr << " ERROR" << endl;
}
env.end();
return 0;
}
Thanks in advance
#CPLEXOptimizers#DecisionOptimization