mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 17:15:31 +00:00
initial pass
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
748ada2acc
commit
43ddad0ecd
5 changed files with 139 additions and 148 deletions
|
@ -1,18 +1,18 @@
|
|||
#include "sat_local_search.h"
|
||||
#include "sat_solver.h"
|
||||
|
||||
static int build_instance(char *filename, sat::solver& s, sat::local_search& ls)
|
||||
static bool build_instance(char const * filename, sat::solver& s, sat::local_search& local_search)
|
||||
{
|
||||
char line[16383];
|
||||
int cur_term;
|
||||
// for temperally storage
|
||||
int temp[16383];
|
||||
int temp_count;
|
||||
|
||||
std::ifstream infile(filename);
|
||||
//if (infile == NULL) //linux
|
||||
if (!infile)
|
||||
return 0;
|
||||
if (!infile) {
|
||||
std::cout << "File not found " << filename << "\n";
|
||||
return false;
|
||||
}
|
||||
infile.getline(line, 16383);
|
||||
int num_vars, num_constraints;
|
||||
sscanf_s(line, "%d %d", &num_vars, &num_constraints);
|
||||
|
@ -20,44 +20,36 @@ static int build_instance(char *filename, sat::solver& s, sat::local_search& ls)
|
|||
//cout << "number of constraints: " << num_constraints << endl;
|
||||
|
||||
|
||||
// write in the objective function
|
||||
temp_count = 0;
|
||||
infile >> cur_term;
|
||||
while (cur_term != 0) {
|
||||
temp[temp_count++] = cur_term;
|
||||
infile >> cur_term;
|
||||
}
|
||||
int ob_num_terms = temp_count;
|
||||
|
||||
#if 0
|
||||
TBD make this compile:
|
||||
ob_constraint = new ob_term[ob_num_terms + 1];
|
||||
// coefficient
|
||||
ob_constraint[0].coefficient = 0; // virtual var: all variables not in ob are pointed to this var
|
||||
for (i = 1; i <= ob_num_terms; ++i) {
|
||||
ob_constraint[i].coefficient = temp[i - 1];
|
||||
}
|
||||
|
||||
unsigned_vector coefficients;
|
||||
sat::literal_vector lits;
|
||||
// ob variable
|
||||
temp_count = 0;
|
||||
|
||||
// process objective function:
|
||||
// read coefficents
|
||||
infile >> cur_term;
|
||||
while (cur_term != 0) {
|
||||
temp[temp_count++] = cur_term;
|
||||
coefficients.push_back(cur_term);
|
||||
infile >> cur_term;
|
||||
}
|
||||
if (temp_count != ob_num_terms) {
|
||||
cout << "Objective function format error." << endl;
|
||||
exit(-1);
|
||||
|
||||
// read variables
|
||||
infile >> cur_term;
|
||||
while (cur_term != 0) {
|
||||
lits.push_back(sat::literal(abs(cur_term), cur_term < 0));
|
||||
infile >> cur_term;
|
||||
}
|
||||
for (i = 1; i <= ob_num_terms; ++i) {
|
||||
ob_constraint[i].var_id = temp[i - 1];
|
||||
coefficient_in_ob_constraint[ob_constraint[i].var_id] = ob_constraint[i].coefficient;
|
||||
|
||||
if (lits.size() != coefficients.size()) {
|
||||
std::cout << "Objective function format error. They have different lenghts.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < lits.size(); ++i) {
|
||||
local_search.add_soft(lits[i], coefficients[i]);
|
||||
}
|
||||
|
||||
// read the constraints, one at a time
|
||||
card_extension* ext = 0;
|
||||
int k;
|
||||
for (c = 1; c <= num_constraints; ++c) {
|
||||
for (int c = 1; c <= num_constraints; ++c) {
|
||||
lits.reset();
|
||||
infile >> cur_term;
|
||||
while (cur_term != 0) {
|
||||
|
@ -65,63 +57,11 @@ static int build_instance(char *filename, sat::solver& s, sat::local_search& ls)
|
|||
infile >> cur_term;
|
||||
}
|
||||
infile >> k;
|
||||
ext->add_at_least(null_bool_var, lits, lits.size() - k);
|
||||
local_search.add_cardinality(lits.size(), lits.c_ptr(), static_cast<unsigned>(lits.size() - k));
|
||||
}
|
||||
#endif
|
||||
|
||||
infile.close();
|
||||
|
||||
|
||||
#if 0
|
||||
Move all of this to initialization code for local search solver:
|
||||
|
||||
// create var_term array
|
||||
for (v = 1; v <= num_vars; ++v) {
|
||||
var_term[v] = new term[var_term_count[v]];
|
||||
var_term_count[v] = 0; // reset to 0, for building up the array
|
||||
}
|
||||
// scan all constraints to build up var term arrays
|
||||
for (c = 1; c <= num_constraints; ++c) {
|
||||
for (i = 0; i < constraint_term_count[c]; ++i) {
|
||||
v = constraint_term[c][i].var_id;
|
||||
var_term[v][var_term_count[v]++] = constraint_term[c][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// build neighborhood relationship
|
||||
bool *is_neighbor;
|
||||
is_neighbor = new bool[num_vars + 1];
|
||||
for (v = 1; v <= num_vars; ++v) {
|
||||
// init as not neighbor
|
||||
for (i = 1; i <= num_vars; ++i) {
|
||||
is_neighbor[i] = false;
|
||||
}
|
||||
temp_count = 0;
|
||||
// for each constraint v appears
|
||||
for (i = 0; i < var_term_count[v]; ++i) {
|
||||
c = var_term[v][i].constraint_id;
|
||||
for (j = 0; j < constraint_term_count[c]; ++j) {
|
||||
if (constraint_term[c][j].var_id == v)
|
||||
continue;
|
||||
// not neighbor yet
|
||||
if (!is_neighbor[constraint_term[c][j].var_id]) {
|
||||
is_neighbor[constraint_term[c][j].var_id] = true;
|
||||
temp[temp_count++] = constraint_term[c][j].var_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
// create and build neighbor
|
||||
var_neighbor_count[v] = temp_count;
|
||||
var_neighbor[v] = new int[var_neighbor_count[v]];
|
||||
for (i = 0; i < var_neighbor_count[v]; ++i) {
|
||||
var_neighbor[v][i] = temp[i];
|
||||
}
|
||||
}
|
||||
delete[] is_neighbor;
|
||||
|
||||
#endif
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tst_sat_local_search(char ** argv, int argc, int& i) {
|
||||
|
@ -129,6 +69,20 @@ void tst_sat_local_search(char ** argv, int argc, int& i) {
|
|||
std::cout << "require dimacs file name\n";
|
||||
return;
|
||||
}
|
||||
reslimit limit;
|
||||
params_ref params;
|
||||
sat::solver solver(params, limit);
|
||||
sat::local_search local_search(solver);
|
||||
char const* file_name = argv[i + 1];
|
||||
++i;
|
||||
|
||||
if (!build_instance(file_name, solver, local_search)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "local instance built\n";
|
||||
local_search();
|
||||
|
||||
// sat::solver s;
|
||||
// populate the sat solver with clauses and cardinality consrtaints from the input
|
||||
// call the lookahead solver.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue