Originally posted by: Ghizlane
Hello,
I am traying to code a mixed integer lineair mathematical program in Cplex using C++ in VS2012. The problem is that the programm give me a solution that do not satisfy certain constraints.
The C++ code is:
#include <ilcplex\ilocplex.h>
#include <stdlib.h>
#include <time.h >
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <math.h>
using namespace std ;
typedef IloArray<IloNumVarArray> NumVarMatrix;
typedef IloArray<IloNumArray> NumMatrix;
const int max_entier = std::numeric_limits<int>::max();
int m=4,n=4,M=max_entier;
int P[16]={54,34,61,2,9,15,89,70,38,19,28,87,95,34,7,29};
ILOSTLBEGIN
int main(int argc, char** argv) {
IloEnv env;
srand( time( NULL ) );
double debut, fin;
debut = clock();
IloInt nbjobs=n;
IloInt nbmachines=m;
try {
IloModel model(env);
// creation of variables
IloNumVarArray D(env,n*n,0,1,ILOINT);
IloNumVarArray S(env,m*n+1,0,max_entier,ILOFLOAT);
NumVarMatrix q(env, nbmachines);
// variable q
for (int j = 0; j <nbmachines; j++)
{
q[j]=IloNumVarArray(env, nbjobs*nbjobs,0,max_entier,ILOFLOAT);
}
for (int r = 0; r < nbmachines; r++)
{
for(int k=0;k<nbjobs;++k)
{
for(int i=0;i<k;++i)
{
q[r][k*n+i] = IloNumVar(env,0,M-P[r*n+i]-P[r*n+k],ILOFLOAT);
}
}
}
//contrainte 1:
for (int r=0;r<nbmachines-1;++r)
{
for(int i=0;i<nbjobs;++i)
{
IloExpr expr1(env);
IloExpr expr2(env);
expr1=S[r*n+i]+P[r*n+i];
expr2=S[(r+1)*n+i];
model.add(expr1<=expr2);
expr1.end();
expr2.end();
}
}
//contrainte 2:
for (int r=0;r<nbmachines;++r)
{
for(int k=0;k<nbjobs;++k)
{
for(int i=0;i<k;++i)
{
IloExpr expr1(env);
IloExpr expr2(env);
expr1=S[r*n+i]-S[r*n+k]-P[r*n+k]+M*D[k*n+i];
expr2=q[r][k*n+i];
model.add(expr1==expr2);
expr1.end();
expr2.end();
}
}
}
//constraint 3:
for (int i=0;i<nbjobs;++i)
{
IloExpr expr1(env);
IloExpr expr2(env);
expr1=S[(m-1)*n+n];
expr2=S[(m-1)*n+i]+P[(m-1)*n+i];
model.add(expr1>=expr2);
expr1.end();
expr2.end();
}
//objective function :
IloExpr expr1(env);
expr1=S[(m-1)*nbjobs+n];
model.add(IloMinimize(env, expr1));
expr1.end();
////////solvind the problem
IloCplex cplex(model);
cplex.solve ();
IloNum objval = cplex.getObjValue ();
cout <<"objective value"<< objval << "\n" ;
// Values of variables :
IloArray<IloNum> D1(env ,nbjobs*nbjobs) ;
for(int k=0;k<nbjobs;++k)
{
for(int i=0;i<k;++i)
{
D1[k*nbjobs+i]=cplex.getValue(D[k*nbjobs+i]);
}
}
cout << D1 << "\n" ;
IloArray<IloNum> S1(env ,m*n+1) ;
for (int r=0;r<nbmachines;++r)
{
for(int i=0;i<nbjobs;++i)
{
S1[r*nbjobs+i]= cplex.getValue(S[r*nbjobs+i]);
}
}
cout << S1 << "\n" ;
NumMatrix q1(env,nbmachines);
for (int j = 0; j <nbmachines; j++)
{
q1[j]=IloNumArray(env, nbjobs*nbjobs);
}
for (int r = 0; r < nbmachines; r++)
{
for(int k=0;k<nbjobs;++k)
{
for(int i=0;i<k;++i)
{
q1[r][k*n+i] = cplex.getValue(q[r][k*n+i]);
}
}
}
cout << q1 << "\n" ;
//verify the values of constraints 2
for (int r=0;r<nbmachines;++r)
{
for(int k=0;k<nbjobs;++k)
{
for(int i=0;i<k;++i)
{
cout<<" Constraints2: expr1= "<<S1[r*n+i]-S1[r*n+k]-P[r*n+k]+M*D1[k*n+i];
cout<<"--------- expr2= "<<q1[r][k*n+i]<<endl;
}
}
}
fin = clock();
double temps_cpu = (fin-debut) / CLOCKS_PER_SEC ;
}
catch (IloException& e) {
cerr << "Concert Exception: " << e << endl;
} catch (...) {
cerr << "Other Exception" << endl;
}
env.end();
system ("pause");
return 0 ;
} // END main
So when we solve this problem, we get solutions that dont verify the constraints 2. I cannot find where the error is exactelly, could you help me please.
Best regards,
#CPLEXOptimizers#DecisionOptimization