Originally posted by: UteWW
Hi Daniel,
sorry, that this entry is a bit lengthy. I summarized the structure of my code. But since I'm not sure, where the problem exactly is, I was not sure, what to skip and what to leave....
*************************************************************************************
CALLBACK1:
> I think we should still try to figure out what goes wrong and why it does not work as expected.
> It is not completely clear to me how your code currently looks like. Could you post the relevant
> part of your callback here?
The callback for the first MIP looks like this:
ILOHEURISTICCALLBACK2(create_solution, TLModel*, model, Network<sLFRoad>*, net)
{
//TLModel is a class in which several NumVarArrays and BoolVarArrays, Constraints and Objective Function of the MIP is stored.
// The only binary variables are given by IloArray<BoolVarArray> (I call i BoolVarMatrix) _A, _eta, _beta and IloArray<BoolVarMatrix> _kappa. Besides them many Matrices of NumVars a contained in model.
//Network is a class containing some other parameters
typedef IloArray<IntegerFeasibilityArray> FeasArray;
typedef IloArray<FeasArray> FeasMatrix;
IloEnv env = getEnv();
try{
int i,t,k;
FeasArray feas(env,ne);
FeasArray feasbeta(env,ne);
FeasArray feaseta(env,ne);
FeasMatrix feaskappa(env,ne);
for(i=0; i<ne; i++)
{
feas[i] = FeasArray(env);
feasbeta[i] = FeasArray(env);
feaseta[i] = FeasArray(env);
feaskappa[i] = FeasArray(env,nk[i]);
}
//in vals werden unter anderem die Lösungswerte für die Variablen aus "model" gespeichert
TLValues* vals = new
TLValues(env,ne,nt,&nk,net->_dx,net->_dt, net->_nS, &(net->_nnS), &(net->_S));
for(i=0; i<ne;i++)
{
getValues(vals->_A[i], model->_A[i]);
getFeasibilities(feas[i], model->_A[i]);
getFeasibilities(feasbeta[i], model->_beta[i]);
getFeasibilities(feaseta[i], model->_eta[i]);
for(k=0; k<nk[i];k++)
{
feaskappa[i][k] = IntegerFeasibilityArray(env);
}
for(k=0; k<nk[i];k++)
{
getFeasibilities(feaskappa[i][k], model->_kappa[i][k]);
}
http://... }
if(vals->create_tlsetting()==0) //create_tlsetting sets all entries of _A, which are not integer feasible, to a binary value.
{
//Now, the other values of the remaining variables are computed and stored in vals. Furthermore, the objective function value objval of the instance is computed.
http://... //now comes the setBound-part to check the found solution
for(i=0; i<ne; i++)
{
for(t=0; t<nt; t++)
{
if(feas[i][t]!=ImpliedFeasible)
{
setBounds(model->_A[i][t],vals->_A[i][t],vals->_A[i][t]);
}
}
}
feas.end();
//The same is done for model->_beta, model->_eta and model->_kappa
http://... IloNumVarArray varsarray(env);
IloNumArray valsarray(env);
//x for(i=0;i<ne;i++)
//x {
//x for(t=0;t<nt;t++)
//x {
//x varsarray.add(model->_A[i][t]);
//x valsarray.add(vals->_A[i][t]);
//x }
//x }
//x
http://... //followed by all the other variables
if(solve()){
env.out() << "object value = " << getObjValue() << ", obvjal=" << objval << endl;
//The following part is commented out because even though getObjValue() is equal to objval in the output, the if-clause is never entered
//x if(getObjValue()==objval)
//x {
//x env.out() << "use heuristic solution1";
//x setSolution(varsarray, valsarray, objval);
//x }
//x else
//x {
//x env.out() << "use heuristic solution2";
//x setSolution(varsarray, valsarray);
//x }
//x env.out() << endl;
}
else{
env.out() << "x";
}
setSolution(varsarray, valsarray);
//setSolution only works, when it is used with empty arguments, like here. When I comment out that line and use the lines marked with //x, the solution is not used by cplex.
varsarray.end();
valsarray.end();
}//end if _A is valid
}
catch (...) {
throw;
}
}
*******************************************************************
CALLBACK2:
> Could you describe in more detail what your problem is?
I think, the best is, to describe the problem along with the code.
The callback for the second MIP create an IP which itself is solved by cplex. Here, I use a different IloEnvironment for the inner IP. But I'm not too sure, if I do it in the correct way. At the moment, I have the feeling, that the problem might have to do with the inner IP.
The structure of this callback looks like this:
ILOHEURISTICCALLBACK2(create_solution2, TLModel*, model, Network<sLFRoad>*, net)
{
//the beginning is almost the same as in the first callback
IloEnv env = getEnv();
IloEnv env2; //second environment for the inner IP
try{
int i,t,k;
FeasArray feas(env2,ne); //as well feasbeta, feaseta and feaskappa as in the first callback
http://.. for(i=0; i<ne; i++)
{
feas[i] = IntegerFeasibilityArray(env2);
http://... //and the other feas...
}
TLValues* vals = new
TLValues(env2,ne,nt,&nk,net->_dx,net->_dt, net->_nS, &(net->_nnS), &(net->_S));
for(i=0; i<ne;i++)
{
getValues(vals->_A[i], model->_A[i]);
getFeasibilities(feas[i], model->_A[i]);
http://... //and the other feas...
int s,l,j;
//create IP
IloModel model2(env2);
IloArray<IloBoolVarArray> C(env2,_ne);
for(i=0;i<_ne;i++)
{
C[i] = IloBoolVarArray (env2, _nt);
for(t=0; t<_nt; t++)
{
C[i][t] = IloBoolVar(env2);
}
model2.add(C[i]);
}
//objective
IloExpr so(env2);
for(i=0;i<_ne;i++){
for(t=0; t<_nt; t++){
so += vals->_A[i][t]*C[i][t];
}}
model2.add(IloMaximize(env2, so));
so.end();
´
//constraints
for(t=0; t<_nt; t++)
{
for(i=0;i<_ne;i++)
{
//Here I check, whether variable _A[i][t] has been fixed during the branch and bound and fix them also for the IP (is that correct?)
int ub = getUB(model->_A[i][t]);
int lb = getLB(model->_A[i][t]);
if(vals->_If[i][t]==1 && ub==lb)
{
IloRange rg(env2,vals->_A[i][t],C[i][t],vals->_A[i][t],rgnm);
model2.add(rg);
}
}
}
//also some other constraints follow
http://... IloCplex cplex2(env2);
cplex2.extract(model2);
cplex2.setOut(env2.getNullStream());
if(cplex2.solve()){
cout << "inner IP solved" << endl;
//according to the solution all values of variables are computed and stored in vals->_... and objval is computed
http://... //then setBounds and SetSolution routines follows as in the upper version.
}
}
} //end try
catch (...) {
throw;
}
}
#CPLEXOptimizers#DecisionOptimization