示例#1
0
int QPosition::CheckWPiece(const U64& loc){
  if(loc & woccupancy){
    if(loc & wpawns())   return 0;
    if(loc & wknights()) return 1;
    if(loc & wbishops()) return 2;
    if(loc & wrooks())   return 3;
    if(loc & wqueens())  return 4;
    if(loc & wking())    return 5;
  }
  return -1;
}
示例#2
0
int QPosition::FindQueenMoves(void){
  QBitBoard q(turnFlag ? bqueens() : wqueens());
  const int nq = q.PopCount();
  for(int i=0; i<nq; i++){
    const int from_loc = q.PopFirst();
    const U64& occ = turnFlag ? boccupancy : woccupancy;
    QBitBoard qmov(RookMovement(Piece[from_loc],occ));
    const int nqmov = qmov.PopCount();
    for(int j=0; j<nqmov; j++){
      const int to_loc = qmov.PopFirst();
      const int cpiece = turnFlag ? CheckWPiece(Piece[to_loc]) : CheckBPiece(Piece[to_loc]);
      moves.push_back(QMove(Piece[from_loc],Piece[to_loc],4,turnFlag,cpiece));
      if(cpiece != -1) cap_moves.push_back(moves.size()-1);
    }
  }
  return moves.size();
}
示例#3
0
// Usage: iloglue problem_name.wcsp [verbosity]
int main(int argc, char** argv)
{
    string pbname;
    int nbvar, nbval, nbconstr;
    IloEnv env;
    IloTimer timer(env);

    if (argc >= 3)
        ToulBar2::verbose = atoi(argv[2]);

    try {
        IloModel model(env);

        // open the file
        ifstream file(argv[1]);
        if (!file) {
            cerr << "Could not open file " << argv[1] << endl;
            exit(EXIT_FAILURE);
        }

        // reads problem name and sizes
        file >> pbname;
        file >> nbvar;
        file >> nbval;
        file >> nbconstr;

        // creates the objective function
        IloIntVar obj(env, 0, MAX_COST, "objective");

        // creates the problem variables
        IloIntVarArray vars(env, nbvar, 0, MAX_DOMAIN_SIZE - 1);
        model.add(vars);
        for (int i = 0; i < nbvar; i++) {
            char* name = new char[16];
            sprintf(name, "x%d", i);
            vars[i].setName(name);
        }

        // creates a global weighted CSP constraint
        model.add(IloWeightedCSP(env, obj, vars, argv[1]));

        if (strstr(argv[1], "zebra"))
            zebra(env, model, vars);
        if (strstr(argv[1], "wqueens"))
            wqueens(env, model, vars);
        if (strstr(argv[1], "quasi"))
            quasi(env, model, vars);
        if (strstr(argv[1], "alldiff"))
            alldiff(env, model, vars);

        //     model.add(IloMinimize(env, obj)); DOES NOT WORK???

        IloSolver solver(model);
        IlogSolver = solver;

        // creates a goal to store the best solution found DOES NOT WORK???
        //     IloSolution solution(env);
        //     solution.add(obj);
        //     solution.add(vars);
        //     IloGoal storeSolution = IloStoreSolution(env, solution);

        Objective = solver.getIntVar(obj);
        ProblemSize = nbvar;
        ProblemVars = solver.getIntVarArray(vars);
        BestSol = new int[nbvar];
        BestSol[0] = -1;

        timer.start();

        //    IlcPrintTrace trace(solver, IlcTraceConstraint);
        //    solver.setTraceMode(IlcTrue);
        //    solver.setTrace(trace);

        // chooses high propagation level for AllDiff
        solver.setDefaultFilterLevel(IloAllDiffCt, IloExtendedLevel);

        // finds an optimal solution
        solver.solve(IloGenerateVars(env, vars) && IloNewSolution(env));
        Store::restore(0);
        // restores the best solution and shows it
        //     solver.solve(IloRestoreSolution(env,solution));
        //     cout << solver.getStatus() << " Solution" << endl;
        //     cout << "Optimum: " << solver.getValue(obj) << " in " << solver.getNumberOfFails() << " fails and " << solver.getTime() << " seconds." << endl;
        cout << "Optimum: " << UpperBound << " in " << solver.getNumberOfFails() << " fails and " << solver.getTime() << " seconds." << endl;
        if (ToulBar2::verbose >= 0 && BestSol[0] != -1) {
            cout << "Optimal solution:";
            for (int i = 0; i < nbvar; i++) {
                cout << " " << BestSol[i];
            }
            cout << endl;
        }
        solver.printInformation();
    } catch (IloException& ex) {
        cout << "Error: " << ex << endl;
    }
    env.end();
    return 0;
}
示例#4
0
void QPosition::SetWOcc(void){
  woccupancy = wpawns() | wknights() | wbishops() | wrooks() | wqueens() | wking();
}