Example #1
0
Move* ConflictOnlyNE::next() {
    if (firstMove) {
        iter++;
        firstMove = false;
        lastSuggested = suggested;
        moveCounter = 0;
        suggested = 0;
        moveIterator = model->getViolatedConstraints().begin();


    }

    if (model->getViolatedConstraints().empty()) {
        return NULL;
    }
    while (moveIterator != model->getViolatedConstraints().end()) {
        while (moveCounter < (*moveIterator).second->getVariablePointers().size()) {
            int id = (*moveIterator).second->getVariablePointers().at(moveCounter)->getID();
            //            if(model->getVariable(id)->isFixed())

            if (calculated.at(id) != iter) {
                //                int id = (*moveIterator).second->getVariablePointers().at(moveCounter)->getID();
                calculated.at(id) = iter;
                Variable* var = model->getVariable(id);
                if (var->isFixed()) {
                    continue;
                }
                suggested++;

                moveCounter++;
                Move* mv = new Move(var, (1 - var->getCurrentValue()) - var->getCurrentValue());
                //                std::cout << mv << std::endl;
                mv->deltaVector.resize(state->getEvaluation().size(), 0);
                //                mv->setID(mv->var->getID());
                return mv;


            } else {
                moveCounter++;
            }
        }
        moveIterator++;
        moveCounter = 0;
    }
    firstMove = true;
    return NULL;



    //    Variable* var = model->getVariable(this->var);
    //
    //    moveCounter++;
    //    Move* mv = new Move(var, (1 - var->getCurrentValue()) - var->getCurrentValue());
    //    mv->deltaVector.resize(state->getEvaluation().size(), 0);
    //    return mv;
}
Example #2
0
Move* ConflictOnlyNE::nextRandom() {
    moveIterator = model->getViolatedConstraints().begin();
    int rand = Random::Integer(0, model->getViolatedConstraints().size() - 1);
    for (int i = 0; i < rand; i++) {
        moveIterator++;
    }
    int rand2 = Random::Integer(0, (*moveIterator).second->getVariablePointers().size() - 1);
    Variable* var = (*moveIterator).second->getVariablePointers().at(rand2);


    //        Variable* iv = model->getMaskAt(Random::Integer(0, (int) model->getMask().size() - 1));
    //    randomCounter++;
    //    //    Move* mv = new Move(iv, (1 - iv->getCurrentValue()) - iv->getCurrentValue());
    Move* mv = new Move(var, (1 - var->getCurrentValue()) - var->getCurrentValue());
    //    //    mv->deltaVector.resize(state->getEvaluation().size());
    mv->deltaVector.resize(state->getEvaluation().size(), 0);
    return mv;
}
Example #3
0
Move* IntSwapNeighborhood::next() {
    Variable* iv;
    Variable* iv2;
    if (moveCounter2 == model->getMask().size() - 1) {
        iv2 = model->getMaskAt(moveCounter2);
        moveCounter2 = 0;
        iv = model->getMaskAt(moveCounter);
        moveCounter++;
    } else {
        iv = model->getMaskAt(moveCounter);
        iv2 = model->getMaskAt(moveCounter2);
        moveCounter2++;
    }
    while (iv->getCurrentValue() == iv2->getCurrentValue() && moveCounter != model->getMask().size() - 1) {
        if (moveCounter2 == model->getMask().size() - 1) {
            iv2 = model->getMaskAt(moveCounter2);
            moveCounter2 = 0;
            iv = model->getMaskAt(moveCounter);
            moveCounter++;
        } else {
            iv = model->getMaskAt(moveCounter);
            iv2 = model->getMaskAt(moveCounter2);
            moveCounter2++;
        }
    }
    std::vector<Variable*> vars;
    vars.push_back(iv);
    vars.push_back(iv2);

    std::vector<int> delta;
    delta.push_back(iv2->getCurrentValue() - iv->getCurrentValue());
    delta.push_back(iv->getCurrentValue() - iv2->getCurrentValue());
    //    IntegerVariable* iv = model->getMaskAt(moveCounter);
    //    moveCounter++;
    Move* mv = new Move(vars, delta);
    //    Move* mv = new Move(iv, (1 - iv->getCurrentValue()) - iv->getCurrentValue());
    //    Move* mv = new Move(iv, (1 - iv->getCurrentValue()) - iv->getCurrentValue());
    //    mv->deltaVector.resize(state->getEvaluation().size());
    mv->deltaVector.resize(state->getEvaluation().size(), 0);
    return mv;
}
Example #4
0
bool ConflictOnlyNE::commitMove(Move* mv) {
    firstMove = true;
    lastSuggested = suggested;
    Variable* var = mv->getVar();

    std::vector<int>& evaluation = state->getEvaluation();

    // Skal genberegne!!!!!
    bool legal = calculateDelta(mv);
    if (!legal) {
        return false;
    }
    var->setCurrentValue(1 - var->getCurrentValue());

    propagation_queue queue = model->getPropagationQueue(var);
    for (updateType invar : queue) {

        invar->updateValue();
        if (invar->representConstraint()) {
            if (invar->getCurrentValue() == 0) {
                if (invar->getInvariantPointers().back()->inViolatedConstraints()) {
                    //                    std::unordered_map<unsigned, invariant>& vioCons = model->getViolatedConstraints();
                    model->removeViolatedConstraint(invar->getInvariantPointers().back());
                }
            } else {
                if (!invar->getInvariantPointers().back()->inViolatedConstraints()) {
                    //                    std::unordered_map<unsigned, invariant>& vioCons = model->getViolatedConstraints();
                    model->addViolatedConstraint(invar->getInvariantPointers().back());
                    //                    vioCons[invar->getInvariantPointers().back()->getID()] = invar->getInvariantPointers().back();
                    //                    invar->getInvariantPointers().back()->setInViolatedConstraints(true);

                }
            }
        }
    }
    for (unsigned i = 0; i < model->getEvaluationInvariants().size(); i++) {
        evaluation[i] = model->getEvaluationInvariantNr(i)->getCurrentValue();
    }
    return true;
}