3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-12 12:08:18 +00:00

convert static random fields to non-static

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2017-05-11 16:46:07 -07:00
parent df1f7ee5e0
commit 29a49f4427
12 changed files with 46 additions and 55 deletions

View file

@ -22,7 +22,6 @@ Notes:
#include"solve_eqs_tactic.h" #include"solve_eqs_tactic.h"
#include"elim_uncnstr_tactic.h" #include"elim_uncnstr_tactic.h"
#include"smt_tactic.h" #include"smt_tactic.h"
// include"mip_tactic.h"
#include"recover_01_tactic.h" #include"recover_01_tactic.h"
#include"ctx_simplify_tactic.h" #include"ctx_simplify_tactic.h"
#include"probe_arith.h" #include"probe_arith.h"
@ -75,14 +74,15 @@ tactic * mk_qflra_tactic(ast_manager & m, params_ref const & p) {
#if 0 #if 0
params_ref simplex_0, simplex_1, simplex_2; params_ref simplex_0, simplex_1, simplex_2;
simplex_0.set("lp.simplex_strategy", 0); simplex_0.set_uint("lp.simplex_strategy", 0);
simplex_1.set("lp.simplex_strategy", 1); simplex_1.set_uint("lp.simplex_strategy", 1);
simplex_2.set("lp.simplex_strategy", 1); simplex_2.set_uint("lp.simplex_strategy", 2);
tactic* lp_par = par_or(using_params(mk_smt_tactic(), simplex_0), return par(using_params(mk_smt_tactic(), simplex_0),
using_params(mk_smt_tactic(), simplex_1), using_params(mk_smt_tactic(), simplex_1),
using_params(mk_smt_tactic(), simplex_2)); using_params(mk_smt_tactic(), simplex_2));
#else
#endif
return using_params(using_params(mk_smt_tactic(), pivot_p), p); return using_params(using_params(mk_smt_tactic(), pivot_p), p);
#endif
} }

View file

@ -34,6 +34,10 @@ Author: Lev Nachmanson
namespace lean { namespace lean {
unsigned seed = 1; unsigned seed = 1;
random_gen g_rand;
static unsigned my_random() {
return g_rand();
}
struct simple_column_namer:public column_namer struct simple_column_namer:public column_namer
{ {
std::string get_column_name(unsigned j) const override { std::string get_column_name(unsigned j) const override {
@ -1104,7 +1108,7 @@ void update_settings(argument_parser & args_parser, lp_settings& settings) {
settings.harris_feasibility_tolerance = d; settings.harris_feasibility_tolerance = d;
} }
if (get_int_from_args_parser("--random_seed", args_parser, n)) { if (get_int_from_args_parser("--random_seed", args_parser, n)) {
settings.random_seed = n; settings.random_seed(n);
} }
if (get_int_from_args_parser("--simplex_strategy", args_parser, n)) { if (get_int_from_args_parser("--simplex_strategy", args_parser, n)) {
settings.simplex_strategy() = static_cast<simplex_strategy_enum>(n); settings.simplex_strategy() = static_cast<simplex_strategy_enum>(n);

View file

@ -367,7 +367,7 @@ public:
s.m_x[j] = s.m_low_bounds[j]; s.m_x[j] = s.m_low_bounds[j];
break; break;
case column_type::boxed: case column_type::boxed:
if (my_random() % 2) { if (settings().random_next() % 2) {
s.m_x[j] = s.m_low_bounds[j]; s.m_x[j] = s.m_low_bounds[j];
} else { } else {
s.m_x[j] = s.m_upper_bounds[j]; s.m_x[j] = s.m_upper_bounds[j];

View file

@ -348,7 +348,7 @@ public:
if (x_is_at_bound(j)) if (x_is_at_bound(j))
break; // we should preserve x if possible break; // we should preserve x if possible
// snap randomly // snap randomly
if (my_random() % 2 == 1) if (m_settings.random_next() % 2 == 1)
m_x[j] = m_low_bounds[j]; m_x[j] = m_low_bounds[j];
else else
m_x[j] = m_upper_bounds[j]; m_x[j] = m_upper_bounds[j];

View file

@ -64,8 +64,7 @@ allocate_basis_heading() { // the rest of initilization will be handled by the f
lean_assert(basis_heading_is_correct()); lean_assert(basis_heading_is_correct());
} }
template <typename T, typename X> void lp_core_solver_base<T, X>:: template <typename T, typename X> void lp_core_solver_base<T, X>::
init() { init() {
my_random_init(m_settings.random_seed);
allocate_basis_heading(); allocate_basis_heading();
if (m_settings.use_lu()) if (m_settings.use_lu())
init_factorization(m_factorization, m_A, m_basis, m_settings); init_factorization(m_factorization, m_A, m_basis, m_settings);

View file

@ -537,7 +537,7 @@ template <typename T, typename X> unsigned lp_dual_core_solver<T, X>::get_number
if (this->m_m() > 300) { if (this->m_m() > 300) {
s = (unsigned)((s / 100.0) * this->m_settings.percent_of_entering_to_check); s = (unsigned)((s / 100.0) * this->m_settings.percent_of_entering_to_check);
} }
return my_random() % s + 1; return m_settings.random_next() % s + 1;
} }
template <typename T, typename X> bool lp_dual_core_solver<T, X>::delta_keeps_the_sign(int initial_delta_sign, const T & delta) { template <typename T, typename X> bool lp_dual_core_solver<T, X>::delta_keeps_the_sign(int initial_delta_sign, const T & delta) {
@ -715,7 +715,7 @@ template <typename T, typename X> void lp_dual_core_solver<T, X>::update_xb_afte
template <typename T, typename X> void lp_dual_core_solver<T, X>::one_iteration() { template <typename T, typename X> void lp_dual_core_solver<T, X>::one_iteration() {
unsigned number_of_rows_to_try = get_number_of_rows_to_try_for_leaving(); unsigned number_of_rows_to_try = get_number_of_rows_to_try_for_leaving();
unsigned offset_in_rows = my_random() % this->m_m(); unsigned offset_in_rows = m_settings.random_next() % this->m_m();
if (this->get_status() == TENTATIVE_DUAL_UNBOUNDED) { if (this->get_status() == TENTATIVE_DUAL_UNBOUNDED) {
number_of_rows_to_try = this->m_m(); number_of_rows_to_try = this->m_m();
} else { } else {

View file

@ -76,10 +76,10 @@ public:
// choices.clear(); // choices.clear();
// choices.push_back(i); // choices.push_back(i);
// len = row_len; // len = row_len;
// if (my_random() % 10) break; // if (m_settings.random_next() % 10) break;
// } else if (row_len == len) { // } else if (row_len == len) {
// choices.push_back(i); // choices.push_back(i);
// if (my_random() % 10) break; // if (m_settings.random_next() % 10) break;
// } // }
// } // }
@ -89,7 +89,7 @@ public:
// if (choices.size() == 1) // if (choices.size() == 1)
// return choices[0]; // return choices[0];
// unsigned k = my_random() % choices.size(); // unsigned k = m_settings.random_next() % choices.size();
// return choices[k]; // return choices[k];
// #endif // #endif
// } // }
@ -287,7 +287,7 @@ public:
choices.clear(); choices.clear();
choices.push_back(&rc); choices.push_back(&rc);
} else if (damage == num_of_non_free_basics && } else if (damage == num_of_non_free_basics &&
this->m_A.m_columns[j].size() <= len && (my_random() % 2)) { this->m_A.m_columns[j].size() <= len && (m_settings.random_next() % 2)) {
choices.push_back(&rc); choices.push_back(&rc);
len = this->m_A.m_columns[j].size(); len = this->m_A.m_columns[j].size();
} }
@ -299,7 +299,7 @@ public:
return -1; return -1;
} }
const row_cell<T>* rc = choices.size() == 1? choices[0] : const row_cell<T>* rc = choices.size() == 1? choices[0] :
choices[my_random() % choices.size()]; choices[m_settings.random_next() % choices.size()];
a_ent = rc->m_value; a_ent = rc->m_value;
return rc->m_j; return rc->m_j;

View file

@ -199,7 +199,7 @@ int lp_primal_core_solver<T, X>::choose_entering_column_presize(unsigned number_
entering_iter = non_basis_iter; entering_iter = non_basis_iter;
if (number_of_benefitial_columns_to_go_over) if (number_of_benefitial_columns_to_go_over)
number_of_benefitial_columns_to_go_over--; number_of_benefitial_columns_to_go_over--;
} else if (t == j_nz && my_random() % 2 == 0) { } else if (t == j_nz && m_settings.random_next() % 2 == 0) {
entering_iter = non_basis_iter; entering_iter = non_basis_iter;
} }
}// while (number_of_benefitial_columns_to_go_over && initial_offset_in_non_basis != offset_in_nb); }// while (number_of_benefitial_columns_to_go_over && initial_offset_in_non_basis != offset_in_nb);
@ -268,7 +268,7 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::advance_on_so
if (slope_at_entering * m_sign_of_entering_delta > - m_epsilon_of_reduced_cost) { // the slope started to increase infeasibility if (slope_at_entering * m_sign_of_entering_delta > - m_epsilon_of_reduced_cost) { // the slope started to increase infeasibility
break; break;
} else { } else {
if ((numeric_traits<T>::precise() == false) || ( numeric_traits<T>::is_zero(slope_at_entering) && my_random() % 2 == 0)) { if ((numeric_traits<T>::precise() == false) || ( numeric_traits<T>::is_zero(slope_at_entering) && m_settings.random_next() % 2 == 0)) {
// it is not cost benefitial to advance the delta more, so just break to increas the randomness // it is not cost benefitial to advance the delta more, so just break to increas the randomness
break; break;
} }
@ -307,7 +307,7 @@ find_leaving_on_harris_theta(X const & harris_theta, X & t) {
// we also know that harris_theta is limited, so we will find a leaving // we also know that harris_theta is limited, so we will find a leaving
zero_harris_eps(); zero_harris_eps();
unsigned steps = this->m_ed.m_index.size(); unsigned steps = this->m_ed.m_index.size();
unsigned k = my_random() % steps; unsigned k = m_settings.random_next() % steps;
unsigned initial_k = k; unsigned initial_k = k;
do { do {
unsigned i = this->m_ed.m_index[k]; unsigned i = this->m_ed.m_index[k];
@ -398,7 +398,7 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::find_leaving_
return find_leaving_and_t_with_breakpoints(entering, t); return find_leaving_and_t_with_breakpoints(entering, t);
bool unlimited = true; bool unlimited = true;
unsigned steps = this->m_ed.m_index.size(); unsigned steps = this->m_ed.m_index.size();
unsigned k = my_random() % steps; unsigned k = m_settings.random_next() % steps;
unsigned initial_k = k; unsigned initial_k = k;
unsigned row_min_nz = this->m_n() + 1; unsigned row_min_nz = this->m_n() + 1;
m_leaving_candidates.clear(); m_leaving_candidates.clear();
@ -454,7 +454,7 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::find_leaving_
t = ratio; t = ratio;
return entering; return entering;
} }
k = my_random() % m_leaving_candidates.size(); k = m_settings.random_next() % m_leaving_candidates.size();
return m_leaving_candidates[k]; return m_leaving_candidates[k];
} }
@ -833,7 +833,7 @@ template <typename T, typename X> unsigned lp_primal_core_solver<T, X>::get_num
if (ret == 0) { if (ret == 0) {
return 0; return 0;
} }
return std::max(static_cast<unsigned>(my_random() % ret), 1u); return std::max(static_cast<unsigned>(m_settings.random_next() % ret), 1u);
} }
template <typename T, typename X> void lp_primal_core_solver<T, X>::print_column_norms(std::ostream & out) { template <typename T, typename X> void lp_primal_core_solver<T, X>::print_column_norms(std::ostream & out) {
@ -961,7 +961,7 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::init_column_
for (unsigned j = 0; j < this->m_n(); j++) { for (unsigned j = 0; j < this->m_n(); j++) {
this->m_column_norms[j] = T(static_cast<int>(this->m_A.m_columns[j].size() + 1)) this->m_column_norms[j] = T(static_cast<int>(this->m_A.m_columns[j].size() + 1))
+ T(static_cast<int>(my_random() % 10000)) / T(100000); + T(static_cast<int>(m_settings.random_next() % 10000)) / T(100000);
} }
} }

View file

@ -62,7 +62,7 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::choose_enteri
if (number_of_benefitial_columns_to_go_over) if (number_of_benefitial_columns_to_go_over)
number_of_benefitial_columns_to_go_over--; number_of_benefitial_columns_to_go_over--;
} }
else if (t == j_nz && my_random() % 2 == 0) { else if (t == j_nz && m_settings.random_next() % 2 == 0) {
entering_iter = non_basis_iter; entering_iter = non_basis_iter;
} }
}// while (number_of_benefitial_columns_to_go_over && initial_offset_in_non_basis != offset_in_nb); }// while (number_of_benefitial_columns_to_go_over && initial_offset_in_non_basis != offset_in_nb);
@ -293,7 +293,7 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::find_leaving_
} }
if (m_leaving_candidates.size() == 1) if (m_leaving_candidates.size() == 1)
return m_leaving_candidates[0]; return m_leaving_candidates[0];
k = my_random() % m_leaving_candidates.size(); k = m_settings.random_next() % m_leaving_candidates.size();
return m_leaving_candidates[k]; return m_leaving_candidates[k];
} }
template <typename T, typename X> void lp_primal_core_solver<T, X>::init_run_tableau() { template <typename T, typename X> void lp_primal_core_solver<T, X>::init_run_tableau() {

View file

@ -71,8 +71,6 @@ template <typename X> bool is_epsilon_small(const X & v, const double& eps);
int get_millisecond_count(); int get_millisecond_count();
int get_millisecond_span(int start_time); int get_millisecond_span(int start_time);
unsigned my_random();
void my_random_init(long unsigned seed);
class lp_resource_limit { class lp_resource_limit {
@ -111,6 +109,7 @@ private:
std::ostream* m_message_out; std::ostream* m_message_out;
stats m_stats; stats m_stats;
random_gen m_rand;
public: public:
unsigned reps_in_scaler; unsigned reps_in_scaler;
@ -194,16 +193,15 @@ public:
m_bound_propagation ( true), m_bound_propagation ( true),
presolve_with_double_solver_for_lar(true), presolve_with_double_solver_for_lar(true),
m_simplex_strategy(simplex_strategy_enum::tableau_rows), m_simplex_strategy(simplex_strategy_enum::tableau_rows),
report_frequency(1000), report_frequency(1000),
print_statistics(false), print_statistics(false),
column_norms_update_frequency(12000), column_norms_update_frequency(12000),
scale_with_ratio(true), scale_with_ratio(true),
density_threshold(0.7), density_threshold(0.7),
use_breakpoints_in_feasibility_search(false), use_breakpoints_in_feasibility_search(false),
random_seed(1), max_row_length_for_bound_propagation(300),
max_row_length_for_bound_propagation(300), backup_costs(true),
backup_costs(true), column_number_threshold_for_using_lu_in_lar_solver(4000)
column_number_threshold_for_using_lu_in_lar_solver(4000)
{} {}
void set_resource_limit(lp_resource_limit& lim) { m_resource_limit = &lim; } void set_resource_limit(lp_resource_limit& lim) { m_resource_limit = &lim; }
@ -305,8 +303,8 @@ public:
static unsigned ddd; // used for debugging static unsigned ddd; // used for debugging
#endif #endif
bool use_breakpoints_in_feasibility_search; bool use_breakpoints_in_feasibility_search;
unsigned random_seed; unsigned random_next() { return m_rand(); }
static unsigned long random_next; void random_seed(unsigned s) { m_rand.set_seed(s); }
unsigned max_row_length_for_bound_propagation; unsigned max_row_length_for_bound_propagation;
bool backup_costs; bool backup_costs;
unsigned column_number_threshold_for_using_lu_in_lar_solver; unsigned column_number_threshold_for_using_lu_in_lar_solver;

View file

@ -67,15 +67,6 @@ int get_millisecond_span(int start_time) {
void my_random_init(long unsigned seed) {
lp_settings::random_next = seed;
}
unsigned my_random() {
lp_settings::random_next = lp_settings::random_next * 1103515245 + 12345;
return((unsigned)(lp_settings::random_next/65536) % 32768);
}
template <typename T> template <typename T>
bool vectors_are_equal(T * a, vector<T> &b, unsigned n) { bool vectors_are_equal(T * a, vector<T> &b, unsigned n) {
if (numeric_traits<T>::precise()) { if (numeric_traits<T>::precise()) {
@ -126,7 +117,6 @@ bool vectors_are_equal(const vector<T> & a, const vector<T> &b) {
} }
return true; return true;
} }
unsigned long lp_settings::random_next = 1;
#ifdef LEAN_DEBUG #ifdef LEAN_DEBUG
unsigned lp_settings::ddd = 0; unsigned lp_settings::ddd = 0;
#endif #endif

View file

@ -136,7 +136,7 @@ void random_updater::shift_var(unsigned j, interval & r) {
} }
numeric_pair<mpq> random_updater::get_random_from_interval(interval & r) { numeric_pair<mpq> random_updater::get_random_from_interval(interval & r) {
unsigned rand = my_random(); unsigned rand = m_core_solver.settings().random_next();
if ((!r.low_bound_is_set) && (!r.upper_bound_is_set)) if ((!r.low_bound_is_set) && (!r.upper_bound_is_set))
return numeric_pair<mpq>(rand % range, 0); return numeric_pair<mpq>(rand % range, 0);
if (r.low_bound_is_set && (!r.upper_bound_is_set)) if (r.low_bound_is_set && (!r.upper_bound_is_set))