void LinearConstraint::addToSpace() const { int size = _varsIDs->size(); // vars is an array of the IntVar instances implicated in the constraint IntVarArgs vars(size); // coeffs is an array of integer coefficients for the variables IntArgs coeffs(size); for (int i=0; i<size; i++) { IntegerVariable *iv = _solver->varFromID(_varsIDs->at(i)); vars[i] = _solver->getSpace()->getIntVar(iv->getTotalIndex()); coeffs[i] = _varsCoeffs->at(i); } //TODO: avant "Gecode::linear((Space*)_solver->getSpace(), coeffs, vars, _relType, _val);" Gecode::linear(*(Space*)_solver->getSpace(), coeffs, vars, _relType, _val); }
// rebuild a Gecode space with the current variable and constraints void Solver::updateState() { if (_space) { delete _space; _space = NULL; } _space = new CustomSpace(); // create gecode variables for (map<int, IntegerVariable*>::iterator q = _integerVariablesMap->begin(); q != _integerVariablesMap->end(); q++) { IntegerVariable *v = q->second; if ((_suggest) && (_maxModification != NO_MAX_MODIFICATION)) { v->adjustMinMax(_suggest, _maxModification); } else { v->adjustMinMax(_suggest); } v->setIndex(_space->addVariable(v->getVal(), v->getVal())); v->setPosDeltaIndex(_space->addVariable(0, v->getMax() - v->getVal() + 1)); v->setNegDeltaIndex(_space->addVariable(0, v->getVal() - v->getMin() + 1)); v->setTotalIndex(_space->addVariable(v->getMin(), v->getMax())); } // add constraints to space for (map<int, LinearConstraint*>::iterator p = _constraintsMap->begin(); p != _constraintsMap->end(); p++) { (p->second)->addToSpace(); } LinExpr expr; bool init=false; // construct the linear combination of delta variables balanced by the weight associated with their type for (map<int, IntegerVariable*>::iterator q = _integerVariablesMap->begin(); q != _integerVariablesMap->end(); q++) { IntegerVariable *currVar = q->second; // add a delta variable for each beginning or length int multiplier = 1; bool isStrong = false; // give more weight to the edited variables if (_suggest) { for (unsigned int i=0; i<_strongVars->size(); i++) if (_strongVars->at(i) == q->first) { isStrong = true; //multiplier = 10; break; } } IntVarArgs vars(4); IntArgs coeffs(4); if (isStrong) { vars[0] = _space->getIntVar(currVar->getTotalIndex()); vars[1] = _space->getIntVar(currVar->getIndex()); vars[2] = _space->getIntVar(currVar->getTotalIndex()); vars[3] = _space->getIntVar(currVar->getIndex()); coeffs[0] = -1; coeffs[1] = 1; coeffs[2] = -1; coeffs[3] = 1; } else { // constraint : <initial or wanted value> + <positive delta> - <negative delta> = <optimal value> vars[0] = _space->getIntVar(currVar->getNegDeltaIndex()); vars[1] = _space->getIntVar(currVar->getPosDeltaIndex()); vars[2] = _space->getIntVar(currVar->getTotalIndex()); vars[3] = _space->getIntVar(currVar->getIndex()); coeffs[0] = -1; coeffs[1] = 1; coeffs[2] = 1; coeffs[3] = -1; } linear(*_space, coeffs, vars, IRT_EQ, 0); // construction of the objective function if (!init) { expr = LinExpr(vars[0], currVar->getWeight()*multiplier); init = true; LinExpr tmp(vars[1], currVar->getWeight()*multiplier); expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp); } else { LinExpr tmp(vars[0], currVar->getWeight()*multiplier); expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp); tmp = LinExpr(vars[1], currVar->getWeight()*multiplier); expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp); } } // the objective function is a linear combination of the delta variables (lengths are more important than beginnings) _space->setObjFunc(Gecode::expr(*_space, expr)); }