0

right now I am trying to implement a particle swarm optimization algorithm in CPLEX to solve a Vehicle Routing Problem with a large number of customers.

Firstly, I wrote an optimization model in OPL that is now able to get solutions for smaller instances. In order to now also be able to solve problems with a bigger number of customers, I want to do it using a heuristic I have from a paper (Particle Swarm Optimization).

For this, I am implementing each step of the algorithm in a main-block (control flow) in ILOG Script. In each iteration, the algorithm proposes a solution ( = values for the decision variables) that now needs to be checked if it is feasible and what the solution value is. In the next iteration, the solution then is tried to be improved. This is repeated until a certain number of iterations is reached.

I already got the algorithm to work. But now I don't know how I can do the feasibility check.

I basically now have values for the decision variables that I need to run in the model to check if all constraints hold for this solution. How does this work in CPLEX using control flow?

Obviously, I know how to trigger OPL to generate the model and run the CPLEX solver to get a solution for it, but how does it work when you want to give CPLEX the values for the decision variables and just want it to test the feasibility and not do any optimization?

Arctic_Skill
  • 43
  • 1
  • 1
  • 5

1 Answers1

1

In Making Optimization Simple you could check "check feasibility" that relies on fixed start.

    int nbKids=300;

    // a tuple is like a struct in C, a class in C++ or a record in Pascal
    tuple bus
    {
     key int nbSeats;
     float cost;
    }


    // This is a tuple set
    {bus} pricebuses={<40,500>,<30,400>};

    // asserts help make sure data is fine
    assert forall(b in pricebuses) b.nbSeats>0;assert forall(b in pricebuses) b.cost>0;

    // solutions we want to test
    range options=1..3;
    int testSolution[options][pricebuses]=[[5,2],[5,3],[5,4]];
      
    // decision variable array
    dvar int+ nbBus[pricebuses];

    // objective
    minimize
      sum(b in pricebuses) b.cost*nbBus[b];
         
    // constraints
    subject to
    {
      sum(b in pricebuses) b.nbSeats*nbBus[b]>=nbKids;
    }

    float cost=sum(b in pricebuses) b.cost*nbBus[b];
    execute DISPLAY_After_SOLVE
    {
     writeln("The minimum cost is ",cost);
     for(var b in pricebuses) writeln(nbBus[b]," buses ",b.nbSeats, " seats");

    }


    main
    {
    thisOplModel.generate();
    // Test feasibility through fixed start
    
    for(var o in thisOplModel.options)
    {
       for(var b in thisOplModel.pricebuses)
       {
          thisOplModel.nbBus[b].UB=thisOplModel.testSolution[o][b];
          thisOplModel.nbBus[b].LB=thisOplModel.testSolution[o][b];
       }
       if (cplex.solve())
       {
         write(thisOplModel.testSolution[o]," is feasible");
         writeln(" and the cost is ",cplex.getObjValue());
       }
       else writeln(thisOplModel.testSolution[o]," is not feasible");
    }
    }    

/*

which gives

[5 2] is not feasible
[5 3] is not feasible
[5 4] is feasible and the cost is 4100

*/

but if you prefer asserts you also can rely on that.

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks for your advice, that looks like an elegant solution. I'll try that on my data right now. One more question though, what exactly does assert do? I haven't seen that keyword in CPEX yet. – Arctic_Skill Dec 01 '21 at 09:01