Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Modifying facility.cpp example to demonstrate SOS1

    Posted 10/07/15 01:28 PM

    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


  • 2.  Re: Modifying facility.cpp example to demonstrate SOS1

    Posted 10/07/15 07:20 PM

    Originally posted by: Roni.Mohammad


    I have also following query relating to earlier post

     

     I have two dimensional variable  Xi,n. I know that for all i, at most one Xi,n will be one.
    How can create a loop so that I can add it the model?.
    Basically I want to add a loop following  way
     For all i 
    {
       add SOS in a cplex model   (  Xi1, Xi2, Xi3 .... Xi,n  (at least one of the X will be non zero )  ) 
    }
    Could you please show me with appropriate method used in cplex?

    In order implement SOS1 do I have to declare  Xi,n as binary first?

    I am using cplex concert with C++ .
    Any response is highly appreciated.
    Thanks in advance

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Modifying facility.cpp example to demonstrate SOS1

    Posted 10/09/15 04:48 PM

    Declaring a list of variables as SOS1 does not automatically make them binary (or even integer). If you want them to be binary, you have to declare them that way.

    If your variables are in a 2D IloNumVarArray (IloNumVarArray< IloNumVarArray >, or whatever that looks like in C++), you can loop over the index of the outer array and pass the value for that index (which will be an IloNumVarArray instance) as an argument to the IloSOS1 constructor.

    public IloSOS1(const IloEnv env, const IloNumVarArray vars, const char * name=0)
    

     


    #CPLEXOptimizers
    #DecisionOptimization