Exemplo n.º 1
0
    int find(int n, vector <int> rows, vector <int> columns, vector <int> values) {
        m = rows.size();
        if (m < n) {
            return pow(10, (n - 1) * (n - 1) - m + 1);
        }
        // n <= 10
        if (n == 1) {
            return m == 0 ? 10 : 1;
        }
        memset(numbers, -1, sizeof(numbers));
        for (int i = 0; i < m; ++ i) {
            numbers[rows[i]][columns[i]] = values[i];
        }
        int answer = 1;
        int mods[2] = {2, 5};
#define get_id(i, j) ((i) * n + (j))
        for (int k = 0; k < 2; ++ k) {
            int mod = mods[k];
            Solver *solver = new Solver(mod);
            int vars = 1;
            for (int i = 0; i < n; ++ i) {
                vector <int> row(n * n + 2);
                for (int j = 0; j < n; ++ j) {
                    if (numbers[i][j] == -1) {
                        vars ++;
                        row[get_id(i, j)] = 1;
                    } else {
                        (row[n * n + 1] += numbers[i][j]) %= mod;
                    }
                }
                row[n * n] = 1;
                solver->add(row);
            }
            for (int j = 0; j < n; ++ j) {
                vector <int> row(n * n + 2);
                for (int i = 0; i < n; ++ i) {
                    if (numbers[i][j] == -1) {
                        row[get_id(i, j)] = 1;
                    } else {
                        (row[n * n + 1] += numbers[i][j]) %= mod;
                    }
                }
                row[n * n] = 1;
                solver->add(row);
            }
            int ret = solver->rank();
            if (ret == -1) {
                return 0;
            }
            answer = (long long)answer * pow(mod, vars - ret) % MOD;
        }
#undef get_id
        return answer;
    }
Exemplo n.º 2
0
template<class F> std::tuple<double,double> solve(double y, double x, std::vector<double> obs)
{
  Solver<F> solver;
  for(double o : obs)
    solver.add(F(o),&x,&y);
  solver.solve(DENSE,minimal_verbose());
  return std::make_tuple(x,y);
}
Exemplo n.º 3
0
bool ClauseHead::toImplication(Solver& s) {
	ConstraintType t  = ClauseHead::type();
	uint32 sz         = isSentinel(head_[1]) ? 1 : 2 + (!s.isFalse(head_[2]) || s.level(head_[2].var()) > 0);
	ClauseRep rep     = ClauseRep::create(head_, sz, ClauseInfo(t).setLbd(2).setTagged(tagged()));
	bool   implicit   = s.allowImplicit(rep);
	bool   locked     = ClauseHead::locked(s) && s.decisionLevel() > 0;
	rep.prep          = 1;
	if ((locked || !implicit) && sz > 1) { return false; }
	s.add(rep, false);
	detach(s);
	return true;
}
void addHeuristic( Solver& s, string Heu, const int rdz ) {

  if(ValueO == "lex") {
    for(int i=0; i<s.length; ++i)
      s.sequence[i]->branch = new ValSelectorMin( s.sequence[i] );
  } else if(ValueO == "rand") {
    for(int i=0; i<s.length; ++i)
      s.sequence[i]->branch = new ValSelectorRand( s.sequence[i] );
  } else if(ValueO == "antilex") {
    for(int i=0; i<s.length; ++i)
      s.sequence[i]->branch = new ValSelectorMax( s.sequence[i] );
  } 
  
  if( Heu == "dom" ) {
    MinDomain h(abs(rdz));
    s.add( h );
  }
  if( Heu == "lex" ) {
    Lexicographic h;
    s.add( h );
  } 
  else if( Heu == "deg") {
    MaxDegree h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "rand") {
    Random h;
    s.add( h );
  } 
  else if( Heu == "dom+deg") {
    MinDomMaxDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "domodeg") {
    DomOverDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "domowldeg") {
    DomOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "domowdeg") {
    DomOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "neighbor") {
    Neighbor h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "impact") {
    Impact h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impactodeg") {
    ImpactOverDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impactowdeg") {
    ImpactOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impactowldeg") {
    ImpactOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "fpp") {
    FPP h(abs(rdz));
    s.add( h );
  }
  else {
    NoOrder h;
    s.add( h );
  }
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{  

  // double time, start = get_run_time();
  // for(int i=0; i<10000000; ++i) {
  //   time = get_run_time();
  // }
  // std::cout << (get_run_time() - start) << std::endl;


  // // double time, start = cpu_time();
  // // for(int i=0; i<10000000; ++i) {
  // //   time = cpu_time();
  // // }
  // // std::cout << (cpu_time() - start) << std::endl;

  // exit(1);


  int i, j, N=8;
  if(argc>1) N = atoi(argv[1]);

  VarArray X(N, 1, N);
  Vector< VarArray > differences;
  Solver s;

  s.add( AllDiff(X) );

  VarArray scope;
  for(i=1; i<N-1; ++i) {
    scope.clear();
    for(j=0; j<N-i; ++j) {
      scope.add(X[j]-X[j+i]);
    }
    s.add( AllDiff(scope, FORWARD_CHECKING) );
    differences.add(scope);
  }


  cout << s << endl;

  s.rewrite();

  cout << s << endl;

  s.consolidate();
  
  cout << s << endl;

  s.parameters.verbosity = 2;

  if(s.depth_first_search(X, 
			  new GenericHeuristic< 
			    // GenericNeighborDVO< 
			    //   FailureCountManager, 
			    //   //PruningCountManager, 

			    //   SelfPlusAverage,

			    //   MinDomainOverWeight 
			    //   //MinNeighborDomainOverNeighborWeight
			    //   , 1

			    GenericDVO< 
			      MinDomainOverWeight, 1,
			      FailureCountManager
			      >,
			    MinValue >(&s), 
			  new Geometric()) == SAT) {
    for(i=0; i<N; ++i)
      cout << setw(3) << X[i].get_solution_int_value() << " " ;
    cout << endl;
    for(i=0; i<N-2; ++i) {
      for(j=0; j<N-i-1; ++j)
	cout << setw(3) << differences[i][j].get_solution_int_value() << " " ;
      cout << endl;
    }
    cout << setw(3) << (X[0].get_solution_int_value() - X[N-1].get_solution_int_value()) << endl << endl;
  }
  //std::cout << s.statistics << std::endl;
}
Exemplo n.º 6
0
Arquivo: main-fz.cpp Projeto: siala/SM
int main(int argc, char *argv[])
{
#ifdef _FLATZINC_OUTPUT
  cout << "%";
#endif
  
  SolverCmdLine cmd("Mistral (fzn)", ' ', "2.0");      
  
  TCLAP::SwitchArg annotationArg("","follow_annotations","Uses the annotations", false);
  cmd.add( annotationArg );

  TCLAP::ValueArg<int> parityArg("","parity","Uses parity processing", false, 0, "int");
  cmd.add( parityArg );

  TCLAP::SwitchArg simple_rewriteArg("","simple_rewrite","Uses simple rewriting", false);
  cmd.add( simple_rewriteArg );

#ifdef _PARALLEL
  //std::cout << "PARALLEL \n \n \n " << std::endl;
  TCLAP::ValueArg<int> threadsArg("p","number-threads","Use multithreading with this option.", false, 4, "int");
  cmd.add( threadsArg );
#endif

  cmd.parse(argc, argv);

#ifdef _PARALLEL
  int total = threadsArg.getValue();
  if (cmd.enumerate_solutions())
	  total=1;
  else{
	  //std::cout << "Available  threads : " << omp_get_max_threads() << std::endl;
	  //int recommended= floor((double) (omp_get_max_threads()*3) / 4.0 )   ;
	  int recommended= floor((double) (omp_get_max_threads()) / 2.0 )   ;
	  if (total >recommended){
		  //std::cout << " % " << " high value of -p. The solver will use only " << recommended << " threads (recommended) " << std::endl;
		  total=recommended;
	  }
  }
//  else
//	  std::cout << " % " << " will use " << total << " threads " << std::endl;
  omp_set_num_threads(total);
  long int global_obj =std::numeric_limits<int>::max();
  bool solution_found_elsewhere= false;

#endif



  #ifdef _PARALLEL
#pragma omp parallel
  {
	  //printf("multicore user! %d \n" , omp_get_thread_num());
	  int id = omp_get_thread_num();
#endif


  int thread_seed = cmd.get_seed();

#ifdef _PARALLEL
  thread_seed+=id;
#endif
  usrand(thread_seed);

  Solver s;

  cmd.set_parameters(s);

  std::string policy;
#ifdef _PARALLEL
  if ( id%2 != 0)
	  if (cmd.get_restart_policy()=="geom")
		  policy="luby";
	  else
		  policy="geom";
  else
#endif
	  policy =cmd.get_restart_policy();


#ifdef _PARALLEL
  bool branch_on_auxilary = true;
  if ( id%4 < 2 )
	  branch_on_auxilary=false;
//#pragma omp critical
//  std::cout << " " << s.parameters.prefix_statistics << "ID:  " << id  << " " << policy << " branch_on_auxilary " << branch_on_auxilary <<  "  "<< thread_seed <<  std::endl;
#endif

  double cpu_time = get_run_time() ;

#ifdef _VERBOSE_PARSER
  std::cout << " " << s.parameters.prefix_comment << " Parse: ";
#endif

  FlatZinc::Printer p;
  FlatZinc::FlatZincModel *fm = 0L;

  fm = parse(cmd.get_filename(), s, p);


  if( !fm )
#ifdef _PARALLEL
	  exit(1);
#else
  return 0;
#endif

  fm->set_enumeration(cmd.enumerate_solutions());
  double parse_time = get_run_time() - cpu_time;
#ifdef _VERBOSE_PARSER
  std::cout << std::endl;
#endif

#ifdef _PARALLEL
 //#pragma omp critical
  //cout << " " << s.parameters.prefix_statistics << " PARSETIME " << parse_time << std::endl;
#else
  if(s.parameters.verbosity >0)
	  cout << " " << s.parameters.prefix_statistics << " PARSETIME " << parse_time << std::endl;
#endif

  FlatZinc::SolutionPrinter *sp = new FlatZinc::SolutionPrinter(&p, fm, &s);
  s.add(sp);


  if(s.parameters.time_limit>0) std::cout << " " << s.parameters.prefix_statistics 
					  << " CUTOFF " << s.parameters.time_limit << std::endl;


  // set flatzinc model options
#ifdef _PARALLEL
  fm->branch_on_auxilary=branch_on_auxilary;
#endif

  fm->set_strategy(cmd.get_variable_ordering(), cmd.get_value_ordering(), cmd.get_randomization(), policy);
  fm->set_display_model(cmd.print_model());
  fm->set_display_solution(cmd.print_solution());
  fm->set_annotations(annotationArg.getValue());
  //fm->set_annotations(false);
  fm->set_rewriting(cmd.use_rewrite());
  fm->set_simple_rewriting(simple_rewriteArg.getValue());
  fm->set_parity_processing(parityArg.getValue());
  fm->encode_clauses();

#ifdef _PARALLEL
  if (fm->method() == FlatZinc::FlatZincModel::MAXIMIZATION)
	  global_obj = std::numeric_limits<int>::min();
  fm->best_kown_objective = &global_obj;
  fm->set_solution_found_elsewhere(&solution_found_elsewhere);
#endif

  fm->run(cout , p);


#ifdef _PARALLEL
#pragma omp critical
  {
  if (!solution_found_elsewhere){
	  solution_found_elsewhere=true;
	  //Secure shared memory
#pragma omp flush
 // }
 // else
#endif

  if(cmd.print_solution())
    fm->print_final(cout , p);


#ifdef _PARALLEL
}
  }
#endif

  if(cmd.print_statistics())
    s.statistics.print_full(std::cout);



#ifdef _VERIFICATION
  write_solution(fm, args.back());
#endif


  delete fm;
  delete sp;
  //exit(1);

#ifdef _PARALLEL
  }
#endif

  return 0;
}
Exemplo n.º 7
0
void addHeuristic( Solver& s ) {
  if( Heu == "dom" ) {
    MinDomain h(abs(rdz));
    s.add( h );
  }
  if( Heu == "lex" ) {
    Lexicographic h;
    s.add( h );
  } 
  else if( Heu == "deg") {
    MaxDegree h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "rand") {
    Random h;
    s.add( h );
  } 
  else if( Heu == "dom+deg") {
    MinDomMaxDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "dom/deg") {
    DomOverDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "dom/wldeg") {
    DomOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "dom/wdeg") {
    DomOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "neighbor") {
    Neighbor h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "impact") {
    Impact h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/deg") {
    ImpactOverDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/wdeg") {
    ImpactOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/wldeg") {
    ImpactOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else {
    NoOrder h;
    s.add( h );
  }
  
  if(ValHeu == "anti") {
    s.setAntiLex();
  } else if(ValHeu == "rand") {
    s.setRandMinMax();
  } else if(ValHeu == "split") {
    s.setSplit();
  } else if(ValHeu == "rsplit") {
    s.setRandSplit();
  } else if(ValHeu != "lex") {
    std::cerr << "Warning: unknown value selection heuristic" << std::endl;
  } 

}
Exemplo n.º 8
0
void addHeuristic( Solver& s ) {
  if( Heu == "dom" ) {
    MinDomain h(abs(rdz));
    s.add( h );
  }
  if( Heu == "lex" ) {
    Lexicographic h;
    s.add( h );
  } 
  else if( Heu == "deg") {
    MaxDegree h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "rand") {
    Random h;
    s.add( h );
  } 
  else if( Heu == "dom+deg") {
    MinDomMaxDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "dom/deg") {
    DomOverDeg h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "dom/wldeg") {
    DomOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "dom/wdeg") {
    DomOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "neighbor") {
    Neighbor h(abs(rdz));
    s.add( h );
  } 
  else if( Heu == "impact") {
    Impact h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/deg") {
    ImpactOverDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/wdeg") {
    ImpactOverWDeg h(abs(rdz));
    s.add( h );
  }
  else if( Heu == "impact/wldeg") {
    ImpactOverWLDeg h(abs(rdz));
    s.add( h );
  }
  else {
    NoOrder h;
    s.add( h );
  }
}