Esempio n. 1
0
ILOBRANCHCALLBACK1(MyBranch, IloNumVarArray, vars) {
   if ( getBranchType() != BranchOnVariable )
      return;

   // Branch on var with largest objective coefficient
   // among those with largest infeasibility

   IloNumArray x;
   IloNumArray obj;
   IntegerFeasibilityArray feas;

   try {
      x    = IloNumArray(getEnv());
      obj  = IloNumArray(getEnv());
      feas = IntegerFeasibilityArray(getEnv());
      getValues(x, vars);
      getObjCoefs(obj, vars);
      getFeasibilities(feas, vars);

      IloInt bestj  = -1;
      IloNum maxinf = 0.0;
      IloNum maxobj = 0.0;
      IloInt cols = vars.getSize();
      for (IloInt j = 0; j < cols; j++) {
         if ( feas[j] == Infeasible ) {
            IloNum xj_inf = x[j] - IloFloor (x[j]);
            if ( xj_inf > 0.5 )
               xj_inf = 1.0 - xj_inf;
            if ( xj_inf >= maxinf                              &&
                 (xj_inf > maxinf || IloAbs (obj[j]) >= maxobj)  ) {
               bestj  = j;
               maxinf = xj_inf;
               maxobj = IloAbs (obj[j]);
            }
         }
      }

      if ( bestj >= 0 ) {
         makeBranch(vars[bestj], x[bestj], IloCplex::BranchUp,   getObjValue());
         makeBranch(vars[bestj], x[bestj], IloCplex::BranchDown, getObjValue());
      }
   }
   catch (...) {
      x.end();
      obj.end();
      feas.end();
      throw;
   }
   x.end();
   obj.end();
   feas.end();
}
Esempio n. 2
0
int main(int argc, const char *argv[]){
  IloEnv env;
  try {
    IloModel model(env);
    IloInt nbTransmitters = GetTransmitterIndex(nbCell, 0);
    IloIntVarArray freq(env, nbTransmitters, 0, nbAvailFreq - 1);
    freq.setNames("freq");
    for (IloInt cell = 0; cell < nbCell; cell++)
      for (IloInt channel1 = 0; channel1 < nbChannel[cell]; channel1++)
        for (IloInt channel2= channel1+1; channel2 < nbChannel[cell]; channel2++)
          model.add(IloAbs(  freq[GetTransmitterIndex(cell, channel1)]
                             - freq[GetTransmitterIndex(cell, channel2)] )
                    >= 16);
    for (IloInt cell1 = 0; cell1 < nbCell; cell1++)
      for (IloInt cell2 = cell1+1; cell2 < nbCell; cell2++)
        if (dist[cell1][cell2] > 0)
          for (IloInt channel1 = 0; channel1 < nbChannel[cell1]; channel1++)
            for (IloInt channel2 = 0; channel2 < nbChannel[cell2]; channel2++)
              model.add(IloAbs(  freq[GetTransmitterIndex(cell1, channel1)]
                                 - freq[GetTransmitterIndex(cell2, channel2)] )
                        >= dist[cell1][cell2]);
    
    // Minimizing the total number of frequencies
    IloIntExpr nbFreq = IloCountDifferent(freq); 
    model.add(IloMinimize(env, nbFreq));
    
    IloCP cp(model);
    cp.setParameter(IloCP::CountDifferentInferenceLevel, IloCP::Extended);
    cp.setParameter(IloCP::FailLimit, 40000);
    cp.setParameter(IloCP::LogPeriod, 100000);

    if (cp.solve()) {
      for (IloInt cell = 0; cell < nbCell; cell++) {
        for (IloInt channel = 0; channel < nbChannel[cell]; channel++)
          cp.out() << cp.getValue(freq[GetTransmitterIndex(cell, channel)])
                   << "  " ;
        cp.out() << std::endl;
      }
      cp.out() << "Total # of sites       " << nbTransmitters << std::endl;
      cp.out() << "Total # of frequencies " << cp.getValue(nbFreq) << std::endl;
    } else
      cp.out() << "No solution found."  << std::endl;
    cp.end();
  } catch (IloException & ex) {
    env.out() << "Caught: " << ex << std::endl;
  }
  env.end();
  return 0;
}
ILOCPLEXGOAL1(MyBranchGoal, IloNumVarArray, vars) {
   IloNumArray x;
   IloNumArray obj;
   IntegerFeasibilityArray feas;

   x    = IloNumArray(getEnv());
   obj  = IloNumArray(getEnv());
   feas = IntegerFeasibilityArray(getEnv());
   getValues(x, vars);
   getObjCoefs(obj, vars);
   getFeasibilities(feas, vars);

   IloInt bestj  = -1;
   IloNum maxinf = 0.0;
   IloNum maxobj = 0.0;
   IloInt cols = vars.getSize();
   for (IloInt j = 0; j < cols; j++) {
      if ( feas[j] == Infeasible ) {
         IloNum xj_inf = x[j] - IloFloor (x[j]);
         if ( xj_inf > 0.5 )
            xj_inf = 1.0 - xj_inf;
         if ( xj_inf >= maxinf                             &&
             (xj_inf > maxinf || IloAbs (obj[j]) >= maxobj)  ) {
            bestj  = j;
            maxinf = xj_inf;
            maxobj = IloAbs (obj[j]);
         }
      }
   }

   IloCplex::Goal res;
   if ( bestj >= 0 ) {
      res = AndGoal(OrGoal(vars[bestj] >= IloFloor(x[bestj])+1,
                           vars[bestj] <= IloFloor(x[bestj])),
                    this);
   }

   x.end();
   obj.end();
   feas.end();

   return res;
}
Esempio n. 4
0
int main(int, const char * []) {
    IloEnv env;
    try {
        IloInt i,j;
        IloModel model(env);

        IloNumExpr cost(env);
        IloIntervalVarArray allTasks(env);
        IloIntervalVarArray joeTasks(env);
        IloIntervalVarArray jimTasks(env);
        IloIntArray joeLocations(env);
        IloIntArray jimLocations(env);

        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 0, 0,   120, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 1, 0,   212, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 2, 151, 304, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 3, 59,  181, 200.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 4, 243, 425, 100.0);

        IloTransitionDistance tt(env, 5);
        for (i=0; i<5; ++i)
            for (j=0; j<5; ++j)
                tt.setValue(i, j, IloAbs(i-j));

        IloIntervalSequenceVar joe(env, joeTasks, joeLocations, "Joe");
        IloIntervalSequenceVar jim(env, jimTasks, jimLocations, "Jim");

        model.add(IloNoOverlap(env, joe, tt));
        model.add(IloNoOverlap(env, jim, tt));

        model.add(IloMinimize(env, cost));

        /* EXTRACTING THE MODEL AND SOLVING. */
        IloCP cp(model);
        if (cp.solve()) {
            cp.out() << "Solution with objective " << cp.getObjValue() << ":" << std::endl;
            for (i=0; i<allTasks.getSize(); ++i) {
                cp.out() << cp.domain(allTasks[i]) << std::endl;
            }
        } else {
            cp.out() << "No solution found. " << std::endl;
        }
    } catch (IloException& ex) {
        env.out() << "Error: " << ex << std::endl;
    }
    env.end();
    return 0;
}
Esempio n. 5
0
 IloInt choose(IloCP cp, IloIntVarArray vars, IloInt i) {
   IloIntVar var = vars[i];
   IloNum best = IloInfinity;
   IloInt bestValue = cp.getMin(var);
   IloNum centroid = CalcCentroid(cp, var);
   for (IloCP::IntVarIterator it(cp, var); it.ok(); ++it) {
     IloNum eval = IloAbs(centroid - *it);
     if (eval < best) {
       best      = eval;
       bestValue = *it;
     }
   }
   return bestValue;
 }
Esempio n. 6
0
 IloNum eval(IloCP cp, IloIntVar var, IloInt value) {
   return IloAbs(CalcCentroid(cp, var) - value);
 }
Esempio n. 7
0
ILOBRANCHCALLBACK1(SOSbranch, IloSOS1Array, sos) {
    IloNumArray    x;
    IloNumVarArray var;

    try {
        IloInt i;
        x   = IloNumArray(getEnv());
        var = IloNumVarArray(getEnv());
        IloNum bestx = EPS;
        IloInt besti = -1;
        IloInt bestj = -1;
        IloInt num = sos.getSize();

        for (i = 0; i < num; i++) {
            if ( getFeasibility(sos[i]) == Infeasible ) {
                var.clear();
                sos[i].getVariables(var);
                getValues(x, var);
                IloInt n = var.getSize();
                for (IloInt j = 0; j < n; j++) {
                    IloNum inf = IloAbs(x[j] - IloRound(x[j]));
                    if ( inf > bestx ) {
                        bestx = inf;
                        besti = i;
                        bestj = j;
                    }
                }
            }
        }

        if ( besti >= 0 ) {
            IloCplex::BranchDirectionArray dir;
            IloNumArray                    val;
            try {
                dir = IloCplex::BranchDirectionArray(getEnv());
                val = IloNumArray(getEnv());
                var.clear();
                sos[besti].getVariables(var);
                IloInt n = var.getSize();
                for (IloInt j = 0; j < n; j++) {
                    if ( j != bestj ) {
                        dir.add(IloCplex::BranchDown);
                        val.add(0.0);
                    } else {
                        dir.add(IloCplex::BranchUp);
                        val.add(1.0);
                    }
                }
                makeBranch(var,        val, dir,                  getObjValue());
                makeBranch(var[bestj], 0.0, IloCplex::BranchDown, getObjValue());
            }
            catch (...) {
                dir.end();
                val.end();
                throw;
            }
            dir.end();
            val.end();
        }
    }
    catch (...) {
        var.end();
        x.end();
        throw;
    }

    var.end();
    x.end();
}