3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-23 22:33:40 +00:00

adapt to vectors

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2017-02-27 19:03:32 -08:00
parent c22359820d
commit ba0ec79375
2 changed files with 176 additions and 195 deletions

View file

@ -37,11 +37,10 @@ namespace sat {
var_neighbor.push_back(bool_var_vector()); var_neighbor.push_back(bool_var_vector());
for (bool_var v = 1; v <= num_vars(); ++v) { for (bool_var v = 1; v <= num_vars(); ++v) {
//std::cout << pos_var_term[v].size() << '\t' << neg_var_term[v].size() << '\n';
bool_vector is_neighbor(num_vars() + 1, false); bool_vector is_neighbor(num_vars() + 1, false);
var_neighbor.push_back(bool_var_vector()); var_neighbor.push_back(bool_var_vector());
for (unsigned i = 0; i < pos_var_term[v].size(); ++i) { for (unsigned i = 0; i < m_vars[v].m_watch[true].size(); ++i) {
unsigned c = pos_var_term[v][i]; unsigned c = m_vars[v].m_watch[true][i];
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
bool_var w = constraint_term[c][j].var_id; bool_var w = constraint_term[c][j].var_id;
if (w == v || is_neighbor[w]) continue; if (w == v || is_neighbor[w]) continue;
@ -49,15 +48,15 @@ namespace sat {
var_neighbor.back().push_back(w); var_neighbor.back().push_back(w);
} }
} }
for (unsigned i = 0; i < neg_var_term[v].size(); ++i) { for (unsigned i = 0; i < m_vars[v].m_watch[false].size(); ++i) {
unsigned c = neg_var_term[v][i]; unsigned c = m_vars[v].m_watch[false][i];
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
bool_var w = constraint_term[c][j].var_id; bool_var w = constraint_term[c][j].var_id;
if (w == v || is_neighbor[w]) continue; if (w == v || is_neighbor[w]) continue;
is_neighbor[w] = true; is_neighbor[w] = true;
var_neighbor.back().push_back(w); var_neighbor.back().push_back(w);
} }
} }
} }
for (unsigned i = 0; i < ob_constraint.size(); ++i) for (unsigned i = 0; i < ob_constraint.size(); ++i)
coefficient_in_ob_constraint[ob_constraint[i].var_id] = ob_constraint[i].coefficient; coefficient_in_ob_constraint[ob_constraint[i].var_id] = ob_constraint[i].coefficient;
@ -91,35 +90,30 @@ namespace sat {
} }
} }
// figure out variables scores and sscores // figure out variables scores and slack_scores
void local_search::init_scores() { void local_search::init_scores() {
for (unsigned v = 1; v <= num_vars(); ++v) { for (unsigned v = 1; v <= num_vars(); ++v) {
int_vector truep, falsep; bool is_true = cur_solution[v];
if (cur_solution[v]) { int_vector& truep = m_vars[v].m_watch[is_true];
truep = pos_var_term[v]; int_vector& falsep = m_vars[v].m_watch[!is_true];
falsep = neg_var_term[v];
} for (unsigned i = 0; i < falsep.size(); ++i) {
else { int c = falsep[i];
truep = neg_var_term[v]; // will --slack
falsep = pos_var_term[v]; if (constraint_slack[c] <= 0) {
} dec_slack_score(v);
for (unsigned i = 0; i < falsep.size(); ++i) { if (constraint_slack[c] == 0)
int c = falsep[i]; dec_score(v);
// will --slack }
if (constraint_slack[c] <= 0) { }
--sscore[v]; for (unsigned i = 0; i < truep.size(); ++i) {
if (constraint_slack[c] == 0) int c = truep[i];
--m_var_info[v].m_score;
}
}
for (unsigned i = 0; i < truep.size(); ++i) {
int c = truep[i];
// will --true_terms_count[c] // will --true_terms_count[c]
// will ++slack // will ++slack
if (constraint_slack[c] <= -1) { if (constraint_slack[c] <= -1) {
++sscore[v]; inc_slack_score(v);
if (constraint_slack[c] == -1) if (constraint_slack[c] == -1)
++m_var_info[v].m_score; inc_score(v);
} }
} }
} }
@ -130,7 +124,7 @@ namespace sat {
goodvar_stack.reset(); goodvar_stack.reset();
for (unsigned v = 1; v <= num_vars(); ++v) { for (unsigned v = 1; v <= num_vars(); ++v) {
if (score(v) > 0) { // && conf_change[v] == true if (score(v) > 0) { // && conf_change[v] == true
m_var_info[v].m_in_goodvar_stack = true; m_vars[v].m_in_goodvar_stack = true;
goodvar_stack.push_back(v); goodvar_stack.push_back(v);
} }
} }
@ -148,23 +142,24 @@ namespace sat {
// init varibale information // init varibale information
// variable 0 is the virtual variable // variable 0 is the virtual variable
m_var_info.reset(); time_stamp.reserve(m_vars.size());
sscore.reset(); cscc.reserve(m_vars.size());
time_stamp.reset(); m_vars[0].m_score = INT_MIN;
cscc.reset(); m_vars[0].m_conf_change = false;
m_vars[0].m_slack_score = INT_MIN;
m_var_info.resize(num_vars() + 1, var_info()); cscc[0] = 0;
m_var_info[0].m_score = INT_MIN; time_stamp[0] = max_steps + 1;
m_var_info[0].m_conf_change = false; for (unsigned i = 1; i < m_vars.size(); ++i) {
sscore.resize(num_vars() + 1, 0); sscore[0] = INT_MIN; time_stamp[i] = 0;
time_stamp.resize(num_vars() + 1, 0); time_stamp[0] = max_steps + 1; cscc[i] = 1;
cscc.resize(num_vars() + 1, 1); cscc[0] = 0; m_vars[i].m_conf_change = true;
m_vars[i].m_in_goodvar_stack = false;
m_vars[i].m_score = 0;
m_vars[i].m_slack_score = 0;
}
init_slack(); init_slack();
init_scores(); init_scores();
init_goodvars(); init_goodvars();
} }
void local_search::calculate_and_update_ob() { void local_search::calculate_and_update_ob() {
@ -196,20 +191,14 @@ namespace sat {
} }
void local_search::add_cardinality(unsigned sz, literal const* c, unsigned k) { void local_search::add_cardinality(unsigned sz, literal const* c, unsigned k) {
unsigned id = m_num_constraints; unsigned id = num_constraints();
++m_num_constraints; constraint_term.push_back(svector<term>());
// constraint_term.push_back(svector<term>());
for (unsigned i = 0; i < sz; ++i) { for (unsigned i = 0; i < sz; ++i) {
m_num_vars = std::max(c[i].var() + 1, m_num_vars); m_vars.reserve(c[i].var() + 1);
// var_term.reserve(c[i].var() + 1);
term t; term t;
t.var_id = c[i].var(); t.var_id = c[i].var();
t.sense = c[i].sign(); t.sense = c[i].sign();
if (t.sense) m_vars[t.var_id].m_watch[t.sense].push_back(id);
pos_var_term[t.var_id].push_back(id);
else
neg_var_term[t.var_id].push_back(id);
//var_term[t.var_id].push_back(t);
constraint_term[id].push_back(t); constraint_term[id].push_back(t);
} }
constraint_k.push_back(k); constraint_k.push_back(k);
@ -217,9 +206,6 @@ namespace sat {
local_search::local_search(solver& s) { local_search::local_search(solver& s) {
m_num_vars = 0;
m_num_constraints = 0;
// copy units // copy units
unsigned trail_sz = s.init_trail_size(); unsigned trail_sz = s.init_trail_size();
for (unsigned i = 0; i < trail_sz; ++i) { for (unsigned i = 0; i < trail_sz; ++i) {
@ -325,17 +311,17 @@ namespace sat {
reach_known_best_value = true; reach_known_best_value = true;
break; break;
} }
} }
flipvar = pick_var(); flipvar = pick_var();
flip(flipvar); flip(flipvar);
time_stamp[flipvar] = step; time_stamp[flipvar] = step;
} }
if (tries % 10 == 0) { if (tries % 10 == 0) {
// take a look at watch // take a look at watch
stop = clock(); stop = clock();
elapsed_time = (double)(stop - start) / CLOCKS_PER_SEC; elapsed_time = (double)(stop - start) / CLOCKS_PER_SEC;
std::cout << tries << ": " << elapsed_time << '\n'; std::cout << tries << ": " << elapsed_time << '\n';
} }
if (elapsed_time > cutoff_time) if (elapsed_time > cutoff_time)
reach_cutoff_time = true; reach_cutoff_time = true;
if (reach_known_best_value || reach_cutoff_time) if (reach_known_best_value || reach_cutoff_time)
@ -360,107 +346,102 @@ namespace sat {
unsigned v, c; unsigned v, c;
int org_flipvar_score = score(flipvar); int org_flipvar_score = score(flipvar);
int org_flipvar_sscore = sscore[flipvar]; int org_flipvar_slack_score = slack_score(flipvar);
int_vector truep, falsep; bool is_true = cur_solution[flipvar];
if (cur_solution[flipvar]) { int_vector& truep = m_vars[flipvar].m_watch[is_true];
truep = pos_var_term[flipvar]; int_vector& falsep = m_vars[flipvar].m_watch[!is_true];
falsep = neg_var_term[flipvar];
}
else {
truep = neg_var_term[flipvar];
falsep = pos_var_term[flipvar];
}
// update related clauses and neighbor vars // update related clauses and neighbor vars
for (unsigned i = 0; i < truep.size(); ++i) { for (unsigned i = 0; i < truep.size(); ++i) {
c = truep[i]; c = truep[i];
//++true_terms_count[c]; //++true_terms_count[c];
--constraint_slack[c]; --constraint_slack[c];
switch (constraint_slack[c]) { switch (constraint_slack[c]) {
case -2: // from -1 to -2 case -2: // from -1 to -2
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
v = constraint_term[c][j].var_id; v = constraint_term[c][j].var_id;
// flipping the slack increasing var will no long sat this constraint // flipping the slack increasing var will no long sat this constraint
if (cur_solution[v] == constraint_term[c][j].sense) if (cur_solution[v] == constraint_term[c][j].sense) {
//score[v] -= constraint_weight[c]; //score[v] -= constraint_weight[c];
--m_var_info[v].m_score; dec_score(v);
} }
break; }
case -1: // from 0 to -1: sat -> unsat break;
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { case -1: // from 0 to -1: sat -> unsat
v = constraint_term[c][j].var_id; for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
++cscc[v]; v = constraint_term[c][j].var_id;
//score[v] += constraint_weight[c]; ++cscc[v];
++m_var_info[v].m_score; //score[v] += constraint_weight[c];
// slack increasing var inc_score(v);
if (cur_solution[v] == constraint_term[c][j].sense) // slack increasing var
++sscore[v]; if (cur_solution[v] == constraint_term[c][j].sense)
} inc_slack_score(v);
unsat(c); }
break; unsat(c);
case 0: // from 1 to 0 break;
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { case 0: // from 1 to 0
v = constraint_term[c][j].var_id; for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
// flip the slack decreasing var will falsify this constraint v = constraint_term[c][j].var_id;
if (cur_solution[v] != constraint_term[c][j].sense) { // flip the slack decreasing var will falsify this constraint
//score[v] -= constraint_weight[c]; if (cur_solution[v] != constraint_term[c][j].sense) {
--m_var_info[v].m_score; //score[v] -= constraint_weight[c];
--sscore[v]; dec_score(v);
} dec_slack_score(v);
} }
break; }
default: break;
break; default:
} break;
} }
for (unsigned i = 0; i < falsep.size(); ++i) { }
c = falsep[i]; for (unsigned i = 0; i < falsep.size(); ++i) {
//--true_terms_count[c]; c = falsep[i];
++constraint_slack[c]; //--true_terms_count[c];
switch (constraint_slack[c]) { ++constraint_slack[c];
case 1: // from 0 to 1 switch (constraint_slack[c]) {
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { case 1: // from 0 to 1
v = constraint_term[c][j].var_id; for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
// flip the slack decreasing var will no long falsify this constraint v = constraint_term[c][j].var_id;
if (cur_solution[v] != constraint_term[c][j].sense) { // flip the slack decreasing var will no long falsify this constraint
//score[v] += constraint_weight[c]; if (cur_solution[v] != constraint_term[c][j].sense) {
++m_var_info[v].m_score; //score[v] += constraint_weight[c];
++sscore[v]; inc_score(v);
} inc_slack_score(v);
} }
break; }
case 0: // from -1 to 0: unsat -> sat break;
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { case 0: // from -1 to 0: unsat -> sat
v = constraint_term[c][j].var_id; for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
++cscc[v]; v = constraint_term[c][j].var_id;
//score[v] -= constraint_weight[c]; ++cscc[v];
--m_var_info[v].m_score; //score[v] -= constraint_weight[c];
// slack increasing var no longer sat this var dec_score(v);
if (cur_solution[v] == constraint_term[c][j].sense) // slack increasing var no longer sat this var
--sscore[v]; if (cur_solution[v] == constraint_term[c][j].sense)
} dec_slack_score(v);
sat(c); }
break; sat(c);
case -1: // from -2 to -1 break;
for (unsigned j = 0; j < constraint_term[c].size(); ++j) { case -1: // from -2 to -1
v = constraint_term[c][j].var_id; for (unsigned j = 0; j < constraint_term[c].size(); ++j) {
// flip the slack increasing var will satisfy this constraint v = constraint_term[c][j].var_id;
if (cur_solution[v] == constraint_term[c][j].sense) { // flip the slack increasing var will satisfy this constraint
//score[v] += constraint_weight[c]; if (cur_solution[v] == constraint_term[c][j].sense) {
++m_var_info[v].m_score; //score[v] += constraint_weight[c];
} inc_score(v);
} }
break; }
default: break;
break; default:
} break;
}
} }
m_var_info[flipvar].m_score = -org_flipvar_score; m_vars[flipvar].m_score = -org_flipvar_score;
sscore[flipvar] = -org_flipvar_sscore; m_vars[flipvar].m_slack_score = -org_flipvar_slack_score;
m_var_info[flipvar].m_conf_change = false; m_vars[flipvar].m_conf_change = false;
cscc[flipvar] = 0; cscc[flipvar] = 0;
/* update CCD */ /* update CCD */
@ -471,7 +452,7 @@ namespace sat {
if (score(v) <= 0) { if (score(v) <= 0) {
goodvar_stack[i] = goodvar_stack.back(); goodvar_stack[i] = goodvar_stack.back();
goodvar_stack.pop_back(); goodvar_stack.pop_back();
m_var_info[v].m_in_goodvar_stack = false; m_vars[v].m_in_goodvar_stack = false;
} }
} }
// update all flipvar's neighbor's conf_change to true, add goodvar/okvar // update all flipvar's neighbor's conf_change to true, add goodvar/okvar
@ -479,10 +460,10 @@ namespace sat {
unsigned sz = var_neighbor[flipvar].size(); unsigned sz = var_neighbor[flipvar].size();
for (unsigned i = 0; i < sz; ++i) { for (unsigned i = 0; i < sz; ++i) {
v = var_neighbor[flipvar][i]; v = var_neighbor[flipvar][i];
m_var_info[v].m_conf_change = true; m_vars[v].m_conf_change = true;
if (score(v) > 0 && !already_in_goodvar_stack(v)) { if (score(v) > 0 && !already_in_goodvar_stack(v)) {
goodvar_stack.push_back(v); goodvar_stack.push_back(v);
m_var_info[v].m_in_goodvar_stack = true; m_vars[v].m_in_goodvar_stack = true;
} }
} }
@ -507,14 +488,14 @@ namespace sat {
bool local_search::tie_breaker_ccd(bool_var v, bool_var best_var) { bool local_search::tie_breaker_ccd(bool_var v, bool_var best_var) {
// break tie 1: max score // break tie 1: max score
// break tie 2: max sscore // break tie 2: max slack_score
// break tie 3: cscc // break tie 3: cscc
// break tie 4: oldest one // break tie 4: oldest one
return return
((score(v) > score(best_var)) || ((score(v) > score(best_var)) ||
((score(v) == score(best_var)) && ((score(v) == score(best_var)) &&
((sscore[v] > sscore[best_var]) || ((slack_score(v) > slack_score(best_var)) ||
((sscore[v] == sscore[best_var]) && ((slack_score(v) == slack_score(best_var)) &&
((cscc[v] > cscc[best_var]) || ((cscc[v] > cscc[best_var]) ||
((cscc[v] == cscc[best_var]) && ((cscc[v] == cscc[best_var]) &&
(time_stamp[v] < time_stamp[best_var]))))))); (time_stamp[v] < time_stamp[best_var])))))));
@ -586,7 +567,7 @@ namespace sat {
void local_search::print_info() { void local_search::print_info() {
for (unsigned v = 1; v <= num_vars(); ++v) { for (unsigned v = 1; v <= num_vars(); ++v) {
std::cout << var_neighbor[v].size() << '\t' << cur_solution[v] << '\t' << conf_change(v) << '\t' << score(v) << '\t' << sscore[v] << '\n'; std::cout << var_neighbor[v].size() << '\t' << cur_solution[v] << '\t' << conf_change(v) << '\t' << score(v) << '\t' << slack_score(v) << '\n';
} }
} }
} }

View file

@ -49,8 +49,6 @@ namespace sat {
void set_best_known_value(unsigned v) { m_best_known_value = v; } void set_best_known_value(unsigned v) { m_best_known_value = v; }
}; };
#define MAX_VARS 5000
#define MAX_CONSTRAINTS 100
class local_search { class local_search {
@ -58,14 +56,13 @@ namespace sat {
// data structure for a term in objective function // data structure for a term in objective function
struct ob_term { struct ob_term {
int var_id; // variable id, begin with 1 bool_var var_id; // variable id, begin with 1
int coefficient; // non-zero integer int coefficient; // non-zero integer
}; };
// data structure for a term in constraint // data structure for a term in constraint
struct term { struct term {
//int constraint_id; // constraint it belongs to bool_var var_id; // variable id, begin with 1
int var_id; // variable id, begin with 1
bool sense; // 1 for positive, 0 for negative bool sense; // 1 for positive, 0 for negative
//int coefficient; // all constraints are cardinality: coefficient=1 //int coefficient; // all constraints are cardinality: coefficient=1
}; };
@ -76,42 +73,46 @@ namespace sat {
// terms arrays // terms arrays
//svector<term> var_term[MAX_VARS]; // var_term[i][j] means the j'th term of var i vector<svector<term> > constraint_term; // constraint_term[i][j] means the j'th term of constraint i
int_vector pos_var_term[MAX_VARS];
int_vector neg_var_term[MAX_VARS];
svector<term> constraint_term[MAX_CONSTRAINTS]; // constraint_term[i][j] means the j'th term of constraint i
unsigned m_num_vars, m_num_constraints;
// parameters of the instance // parameters of the instance
unsigned num_vars() const { return m_num_vars - 1; } // var index from 1 to num_vars unsigned num_vars() const { return m_vars.size() - 1; } // var index from 1 to num_vars
unsigned num_constraints() const { return m_num_constraints; } // constraint index from 1 to num_constraint unsigned num_constraints() const { return constraint_term.size(); } // constraint index from 1 to num_constraint
// information about the variable // information about the variable
int_vector coefficient_in_ob_constraint; // initialized to be 0 int_vector coefficient_in_ob_constraint; // initialized to be 0
// int_vector score; // int_vector sscore; // slack score
int_vector sscore; // slack score
struct var_info { struct var_info {
bool m_conf_change; // whether its configure changes since its last flip bool m_conf_change; // whether its configure changes since its last flip
bool m_in_goodvar_stack; bool m_in_goodvar_stack;
int m_score; int m_score;
int m_slack_score;
int_vector m_watch[2];
var_info(): var_info():
m_conf_change(true), m_conf_change(true),
m_in_goodvar_stack(false), m_in_goodvar_stack(false),
m_score(0) m_score(0),
m_slack_score(0)
{} {}
}; };
svector<var_info> m_var_info; svector<var_info> m_vars;
inline int score(unsigned v) const { return m_var_info[v].m_score; } inline int score(unsigned v) const { return m_vars[v].m_score; }
inline bool already_in_goodvar_stack(unsigned v) const { return m_var_info[v].m_in_goodvar_stack; } inline void inc_score(bool_var v) { m_vars[v].m_score++; }
inline bool conf_change(unsigned v) const { return m_var_info[v].m_conf_change; } inline void dec_score(bool_var v) { m_vars[v].m_score--; }
int_vector time_stamp; // the flip time stamp inline int slack_score(unsigned v) const { return m_vars[v].m_slack_score; }
// bool_vector conf_change; inline void inc_slack_score(bool_var v) { m_vars[v].m_slack_score++; }
int_vector cscc; // how many times its constraint state configure changes since its last flip inline void dec_slack_score(bool_var v) { m_vars[v].m_slack_score--; }
vector<bool_var_vector> var_neighbor; // all of its neighborhoods variable
inline bool already_in_goodvar_stack(unsigned v) const { return m_vars[v].m_in_goodvar_stack; }
inline bool conf_change(unsigned v) const { return m_vars[v].m_conf_change; }
int_vector time_stamp; // the flip time stamp
int_vector cscc; // how many times its constraint state configure changes since its last flip
vector<bool_var_vector> var_neighbor; // all of its neighborhoods variable
/* TBD: other scores */ /* TBD: other scores */
// information about the constraints // information about the constraints
@ -126,7 +127,6 @@ namespace sat {
// configuration changed decreasing variables (score>0 and conf_change==true) // configuration changed decreasing variables (score>0 and conf_change==true)
int_vector goodvar_stack; int_vector goodvar_stack;
// bool_vector already_in_goodvar_stack;
// information about solution // information about solution
bool_vector cur_solution; // the current solution bool_vector cur_solution; // the current solution