void GurobiBackend::initialize(unsigned int numVariables, VariableType variableType) { if (gurobilog.getLogLevel() >= Debug) setVerbose(true); else setVerbose(false); _numVariables = numVariables; // delete previous variables if (_variables) delete[] _variables; // add new variables to the model if (variableType == Binary) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " binary variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_BINARY); _model.update(); } else if (variableType == Continuous) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " continuous variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_CONTINUOUS); _model.update(); // remove default lower bound on variables for (int i = 0; i < _numVariables; i++) _variables[i].set(GRB_DoubleAttr_LB, -GRB_INFINITY); } else if (variableType == Integer) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " integer variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_INTEGER); _model.update(); // remove default lower bound on variables for (int i = 0; i < _numVariables; i++) _variables[i].set(GRB_DoubleAttr_LB, -GRB_INFINITY); } LOG_DEBUG(gurobilog) << "creating " << _numVariables << " ceofficients" << std::endl; }
void GurobiBackend::initialize( unsigned int numVariables, VariableType defaultVariableType, const std::map<unsigned int, VariableType>& specialVariableTypes) { if (gurobilog.getLogLevel() >= Debug) setVerbose(true); else setVerbose(false); setMIPGap(optionGurobiMIPGap); if (optionGurobiMIPFocus.as<unsigned int>() <= 3) setMIPGap(optionGurobiMIPGap); else LOG_ERROR(gurobilog) << "Invalid value for MPI focus!" << std::endl; setNumThreads(optionGurobiNumThreads); _numVariables = numVariables; // delete previous variables if (_variables) delete[] _variables; // add new variables to the model if (defaultVariableType == Binary) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " binary variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_BINARY); _model.update(); } else if (defaultVariableType == Continuous) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " continuous variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_CONTINUOUS); _model.update(); // remove default lower bound on variables for (unsigned int i = 0; i < _numVariables; i++) _variables[i].set(GRB_DoubleAttr_LB, -GRB_INFINITY); } else if (defaultVariableType == Integer) { LOG_DEBUG(gurobilog) << "creating " << _numVariables << " integer variables" << std::endl; _variables = _model.addVars(_numVariables, GRB_INTEGER); _model.update(); // remove default lower bound on variables for (unsigned int i = 0; i < _numVariables; i++) _variables[i].set(GRB_DoubleAttr_LB, -GRB_INFINITY); } LOG_DEBUG(gurobilog) << "setting special variable types" << std::endl; // handle special variable types unsigned int v; VariableType type; foreach (boost::tie(v, type), specialVariableTypes) { char t = (type == Binary ? 'B' : (type == Integer ? 'I' : 'C')); LOG_ALL(gurobilog) << "changing type of variable " << v << " to " << t << std::endl; _variables[v].set(GRB_CharAttr_VType, t); if (type == Integer || type == Continuous) { // remove bounds, which might have been set by the default variable // type being binary previously _variables[v].set(GRB_DoubleAttr_LB, -GRB_INFINITY); _variables[v].set(GRB_DoubleAttr_UB, GRB_INFINITY); } }