mirror of
https://github.com/Z3Prover/z3
synced 2025-10-30 11:12:28 +00:00
Merge branch 'master' into polysat
This commit is contained in:
commit
f54f33551e
308 changed files with 6606 additions and 18485 deletions
|
|
@ -72,6 +72,7 @@ public:
|
|||
void set_upper_is_open(interval & a, bool v) { a.m_upper_open = v; }
|
||||
void set_lower_is_inf(interval & a, bool v) { a.m_lower_inf = v; }
|
||||
void set_upper_is_inf(interval & a, bool v) { a.m_upper_inf = v; }
|
||||
|
||||
|
||||
// Reference to numeral manager
|
||||
numeral_manager & m() const { return m_manager; }
|
||||
|
|
@ -184,6 +185,14 @@ public:
|
|||
bool upper_is_open(interval const & a) const { return m_c.upper_is_open(a); }
|
||||
bool lower_is_inf(interval const & a) const { return m_c.lower_is_inf(a); }
|
||||
bool upper_is_inf(interval const & a) const { return m_c.upper_is_inf(a); }
|
||||
bool is_empty(interval const& a) const {
|
||||
if (lower_is_inf(a) || upper_is_inf(a))
|
||||
return false;
|
||||
ext_numeral_kind lk = lower_kind(a), uk = upper_kind(a);
|
||||
if (lower_is_open(a) || upper_is_open(a))
|
||||
return !(::lt(m(), lower(a), lk, upper(a), uk));
|
||||
return ::lt(m(), upper(a), uk, lower(a), lk);
|
||||
}
|
||||
|
||||
bool lower_is_neg(interval const & a) const { return ::is_neg(m(), lower(a), lower_kind(a)); }
|
||||
bool lower_is_pos(interval const & a) const { return ::is_pos(m(), lower(a), lower_kind(a)); }
|
||||
|
|
|
|||
|
|
@ -681,7 +681,7 @@ void interval_manager<C>::set(interval & t, interval const & s) {
|
|||
}
|
||||
set_lower_is_open(t, lower_is_open(s));
|
||||
set_upper_is_open(t, upper_is_open(s));
|
||||
SASSERT(check_invariant(t));
|
||||
SASSERT(is_empty(t) || check_invariant(t));
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
|
|
@ -813,7 +813,7 @@ void interval_manager<C>::add(interval const & a, interval const & b, interval &
|
|||
set_upper_is_inf(c, new_u_kind == EN_PLUS_INFINITY);
|
||||
set_lower_is_open(c, lower_is_open(a) || lower_is_open(b));
|
||||
set_upper_is_open(c, upper_is_open(a) || upper_is_open(b));
|
||||
SASSERT(check_invariant(c));
|
||||
SASSERT(is_empty(a) || is_empty(b) || check_invariant(c));
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
z3_add_component(lp
|
||||
SOURCES
|
||||
binary_heap_priority_queue.cpp
|
||||
binary_heap_upair_queue.cpp
|
||||
core_solver_pretty_printer.cpp
|
||||
dense_matrix.cpp
|
||||
eta_matrix.cpp
|
||||
emonics.cpp
|
||||
factorization.cpp
|
||||
factorization_factory_imp.cpp
|
||||
|
|
@ -19,14 +16,8 @@ z3_add_component(lp
|
|||
lar_solver.cpp
|
||||
lar_core_solver.cpp
|
||||
lp_core_solver_base.cpp
|
||||
lp_dual_core_solver.cpp
|
||||
lp_dual_simplex.cpp
|
||||
lp_primal_core_solver.cpp
|
||||
lp_primal_simplex.cpp
|
||||
lp_settings.cpp
|
||||
lp_solver.cpp
|
||||
lu.cpp
|
||||
lp_utils.cpp
|
||||
matrix.cpp
|
||||
mon_eq.cpp
|
||||
monomial_bounds.cpp
|
||||
|
|
@ -45,10 +36,6 @@ z3_add_component(lp
|
|||
nra_solver.cpp
|
||||
permutation_matrix.cpp
|
||||
random_updater.cpp
|
||||
row_eta_matrix.cpp
|
||||
scaler.cpp
|
||||
square_dense_submatrix.cpp
|
||||
square_sparse_matrix.cpp
|
||||
static_matrix.cpp
|
||||
COMPONENT_DEPENDENCIES
|
||||
util
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/binary_heap_priority_queue_def.h"
|
||||
namespace lp {
|
||||
template binary_heap_priority_queue<int>::binary_heap_priority_queue(unsigned int);
|
||||
template unsigned binary_heap_priority_queue<int>::dequeue();
|
||||
template void binary_heap_priority_queue<int>::enqueue(unsigned int, int const&);
|
||||
template void binary_heap_priority_queue<double>::enqueue(unsigned int, double const&);
|
||||
template void binary_heap_priority_queue<mpq>::enqueue(unsigned int, mpq const&);
|
||||
template void binary_heap_priority_queue<int>::remove(unsigned int);
|
||||
template unsigned binary_heap_priority_queue<numeric_pair<mpq> >::dequeue();
|
||||
template unsigned binary_heap_priority_queue<double>::dequeue();
|
||||
template unsigned binary_heap_priority_queue<mpq>::dequeue();
|
||||
template void binary_heap_priority_queue<numeric_pair<mpq> >::enqueue(unsigned int, numeric_pair<mpq> const&);
|
||||
template void binary_heap_priority_queue<numeric_pair<mpq> >::resize(unsigned int);
|
||||
template void lp::binary_heap_priority_queue<double>::resize(unsigned int);
|
||||
template binary_heap_priority_queue<unsigned int>::binary_heap_priority_queue(unsigned int);
|
||||
template void binary_heap_priority_queue<unsigned>::resize(unsigned int);
|
||||
template unsigned binary_heap_priority_queue<unsigned int>::dequeue();
|
||||
template void binary_heap_priority_queue<unsigned int>::enqueue(unsigned int, unsigned int const&);
|
||||
template void binary_heap_priority_queue<unsigned int>::remove(unsigned int);
|
||||
template void lp::binary_heap_priority_queue<mpq>::resize(unsigned int);
|
||||
}
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
|
||||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "util/debug.h"
|
||||
#include "math/lp/lp_utils.h"
|
||||
namespace lp {
|
||||
// the elements with the smallest priority are dequeued first
|
||||
template <typename T>
|
||||
class binary_heap_priority_queue {
|
||||
vector<T> m_priorities;
|
||||
|
||||
// indexing for A starts from 1
|
||||
vector<unsigned> m_heap; // keeps the elements of the queue
|
||||
vector<int> m_heap_inverse; // o = m_heap[m_heap_inverse[o]]
|
||||
unsigned m_heap_size;
|
||||
// is is the child place in heap
|
||||
void swap_with_parent(unsigned i);
|
||||
void put_at(unsigned i, unsigned h);
|
||||
void decrease_priority(unsigned o, T newPriority);
|
||||
public:
|
||||
#ifdef Z3DEBUG
|
||||
bool is_consistent() const;
|
||||
#endif
|
||||
public:
|
||||
void remove(unsigned o);
|
||||
unsigned size() const { return m_heap_size; }
|
||||
binary_heap_priority_queue(): m_heap(1), m_heap_size(0) {} // the empty constructror
|
||||
// n is the initial queue capacity.
|
||||
// The capacity will be enlarged each time twice if needed
|
||||
binary_heap_priority_queue(unsigned n);
|
||||
|
||||
void clear() {
|
||||
for (unsigned i = 0; i < m_heap_size; i++) {
|
||||
unsigned o = m_heap[i+1];
|
||||
m_heap_inverse[o] = -1;
|
||||
}
|
||||
m_heap_size = 0;
|
||||
}
|
||||
|
||||
void resize(unsigned n);
|
||||
void put_to_heap(unsigned i, unsigned o);
|
||||
|
||||
void enqueue_new(unsigned o, const T& priority);
|
||||
|
||||
// This method can work with an element that is already in the queue.
|
||||
// In this case the priority will be changed and the queue adjusted.
|
||||
void enqueue(unsigned o, const T & priority);
|
||||
void change_priority_for_existing(unsigned o, const T & priority);
|
||||
T get_priority(unsigned o) const { return m_priorities[o]; }
|
||||
bool is_empty() const { return m_heap_size == 0; }
|
||||
|
||||
/// return the first element of the queue and removes it from the queue
|
||||
unsigned dequeue_and_get_priority(T & priority);
|
||||
void fix_heap_under(unsigned i);
|
||||
void put_the_last_at_the_top_and_fix_the_heap();
|
||||
/// return the first element of the queue and removes it from the queue
|
||||
unsigned dequeue();
|
||||
unsigned peek() const {
|
||||
lp_assert(m_heap_size > 0);
|
||||
return m_heap[1];
|
||||
}
|
||||
void print(std::ostream & out);
|
||||
};
|
||||
}
|
||||
|
|
@ -1,214 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/binary_heap_priority_queue.h"
|
||||
namespace lp {
|
||||
// "i" is the child's place in the heap
|
||||
template <typename T> void binary_heap_priority_queue<T>::swap_with_parent(unsigned i) {
|
||||
unsigned parent = m_heap[i >> 1];
|
||||
put_at(i >> 1, m_heap[i]);
|
||||
put_at(i, parent);
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::put_at(unsigned i, unsigned h) {
|
||||
m_heap[i] = h;
|
||||
m_heap_inverse[h] = i;
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::decrease_priority(unsigned o, T newPriority) {
|
||||
m_priorities[o] = newPriority;
|
||||
int i = m_heap_inverse[o];
|
||||
while (i > 1) {
|
||||
if (m_priorities[m_heap[i]] < m_priorities[m_heap[i >> 1]])
|
||||
swap_with_parent(i);
|
||||
else
|
||||
break;
|
||||
i >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T> bool binary_heap_priority_queue<T>::is_consistent() const {
|
||||
for (int i = 0; i < m_heap_inverse.size(); i++) {
|
||||
int i_index = m_heap_inverse[i];
|
||||
lp_assert(i_index <= static_cast<int>(m_heap_size));
|
||||
lp_assert(i_index == -1 || m_heap[i_index] == i);
|
||||
}
|
||||
for (unsigned i = 1; i < m_heap_size; i++) {
|
||||
unsigned ch = i << 1;
|
||||
for (int k = 0; k < 2; k++) {
|
||||
if (ch > m_heap_size) break;
|
||||
if (!(m_priorities[m_heap[i]] <= m_priorities[m_heap[ch]])){
|
||||
return false;
|
||||
}
|
||||
ch++;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::remove(unsigned o) {
|
||||
T priority_of_o = m_priorities[o];
|
||||
int o_in_heap = m_heap_inverse[o];
|
||||
if (o_in_heap == -1) {
|
||||
return; // nothing to do
|
||||
}
|
||||
lp_assert(static_cast<unsigned>(o_in_heap) <= m_heap_size);
|
||||
if (static_cast<unsigned>(o_in_heap) < m_heap_size) {
|
||||
put_at(o_in_heap, m_heap[m_heap_size--]);
|
||||
if (m_priorities[m_heap[o_in_heap]] > priority_of_o) {
|
||||
fix_heap_under(o_in_heap);
|
||||
} else { // we need to propagate the m_heap[o_in_heap] up
|
||||
unsigned i = o_in_heap;
|
||||
while (i > 1) {
|
||||
unsigned ip = i >> 1;
|
||||
if (m_priorities[m_heap[i]] < m_priorities[m_heap[ip]])
|
||||
swap_with_parent(i);
|
||||
else
|
||||
break;
|
||||
i = ip;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lp_assert(static_cast<unsigned>(o_in_heap) == m_heap_size);
|
||||
m_heap_size--;
|
||||
}
|
||||
m_heap_inverse[o] = -1;
|
||||
// lp_assert(is_consistent());
|
||||
}
|
||||
// n is the initial queue capacity.
|
||||
// The capacity will be enlarged two times automatically if needed
|
||||
template <typename T> binary_heap_priority_queue<T>::binary_heap_priority_queue(unsigned n) :
|
||||
m_priorities(n),
|
||||
m_heap(n + 1), // because the indexing for A starts from 1
|
||||
m_heap_inverse(n, -1),
|
||||
m_heap_size(0)
|
||||
{ }
|
||||
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::resize(unsigned n) {
|
||||
m_priorities.resize(n);
|
||||
m_heap.resize(n + 1);
|
||||
m_heap_inverse.resize(n, -1);
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::put_to_heap(unsigned i, unsigned o) {
|
||||
m_heap[i] = o;
|
||||
m_heap_inverse[o] = i;
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::enqueue_new(unsigned o, const T& priority) {
|
||||
m_heap_size++;
|
||||
int i = m_heap_size;
|
||||
lp_assert(o < m_priorities.size());
|
||||
m_priorities[o] = priority;
|
||||
put_at(i, o);
|
||||
while (i > 1 && m_priorities[m_heap[i >> 1]] > priority) {
|
||||
swap_with_parent(i);
|
||||
i >>= 1;
|
||||
}
|
||||
}
|
||||
// This method can work with an element that is already in the queue.
|
||||
// In this case the priority will be changed and the queue adjusted.
|
||||
template <typename T> void binary_heap_priority_queue<T>::enqueue(unsigned o, const T & priority) {
|
||||
if (o >= m_priorities.size()) {
|
||||
if (o == 0)
|
||||
resize(2);
|
||||
else
|
||||
resize(o << 1); // make the size twice larger
|
||||
}
|
||||
|
||||
if (m_heap_inverse[o] == -1)
|
||||
enqueue_new(o, priority);
|
||||
else
|
||||
change_priority_for_existing(o, priority);
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::change_priority_for_existing(unsigned o, const T & priority) {
|
||||
if (m_priorities[o] > priority) {
|
||||
decrease_priority(o, priority);
|
||||
} else {
|
||||
m_priorities[o] = priority;
|
||||
fix_heap_under(m_heap_inverse[o]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// return the first element of the queue and removes it from the queue
|
||||
template <typename T> unsigned binary_heap_priority_queue<T>::dequeue_and_get_priority(T & priority) {
|
||||
lp_assert(m_heap_size != 0);
|
||||
int ret = m_heap[1];
|
||||
priority = m_priorities[ret];
|
||||
put_the_last_at_the_top_and_fix_the_heap();
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::fix_heap_under(unsigned i) {
|
||||
while (true) {
|
||||
unsigned smallest = i;
|
||||
unsigned l = i << 1;
|
||||
if (l <= m_heap_size && m_priorities[m_heap[l]] < m_priorities[m_heap[i]])
|
||||
smallest = l;
|
||||
unsigned r = l + 1;
|
||||
if (r <= m_heap_size && m_priorities[m_heap[r]] < m_priorities[m_heap[smallest]])
|
||||
smallest = r;
|
||||
if (smallest != i)
|
||||
swap_with_parent(smallest);
|
||||
else
|
||||
break;
|
||||
i = smallest;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_priority_queue<T>::put_the_last_at_the_top_and_fix_the_heap() {
|
||||
if (m_heap_size > 1) {
|
||||
put_at(1, m_heap[m_heap_size--]);
|
||||
fix_heap_under(1);
|
||||
} else {
|
||||
m_heap_size--;
|
||||
}
|
||||
}
|
||||
/// return the first element of the queue and removes it from the queue
|
||||
template <typename T> unsigned binary_heap_priority_queue<T>::dequeue() {
|
||||
lp_assert(m_heap_size > 0);
|
||||
int ret = m_heap[1];
|
||||
put_the_last_at_the_top_and_fix_the_heap();
|
||||
m_heap_inverse[ret] = -1;
|
||||
return ret;
|
||||
}
|
||||
template <typename T> void binary_heap_priority_queue<T>::print(std::ostream & out) {
|
||||
vector<int> index;
|
||||
vector<T> prs;
|
||||
while (size()) {
|
||||
T prior;
|
||||
int j = dequeue_and_get_priority(prior);
|
||||
index.push_back(j);
|
||||
prs.push_back(prior);
|
||||
out << "(" << j << ", " << prior << ")";
|
||||
}
|
||||
out << std::endl;
|
||||
// restore the queue
|
||||
for (int i = 0; i < index.size(); i++)
|
||||
enqueue(index[i], prs[i]);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include "math/lp/binary_heap_upair_queue_def.h"
|
||||
namespace lp {
|
||||
template binary_heap_upair_queue<int>::binary_heap_upair_queue(unsigned int);
|
||||
template binary_heap_upair_queue<unsigned int>::binary_heap_upair_queue(unsigned int);
|
||||
template unsigned binary_heap_upair_queue<int>::dequeue_available_spot();
|
||||
template unsigned binary_heap_upair_queue<unsigned int>::dequeue_available_spot();
|
||||
template void binary_heap_upair_queue<int>::enqueue(unsigned int, unsigned int, int const&);
|
||||
template void binary_heap_upair_queue<int>::remove(unsigned int, unsigned int);
|
||||
template void binary_heap_upair_queue<unsigned int>::remove(unsigned int, unsigned int);
|
||||
template void binary_heap_upair_queue<int>::dequeue(unsigned int&, unsigned int&);
|
||||
template void binary_heap_upair_queue<unsigned int>::enqueue(unsigned int, unsigned int, unsigned int const&);
|
||||
template void binary_heap_upair_queue<unsigned int>::dequeue(unsigned int&, unsigned int&);
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <queue>
|
||||
#include "util/vector.h"
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include "math/lp/binary_heap_priority_queue.h"
|
||||
|
||||
|
||||
typedef std::pair<unsigned, unsigned> upair;
|
||||
|
||||
namespace lp {
|
||||
template <typename T>
|
||||
class binary_heap_upair_queue {
|
||||
binary_heap_priority_queue<T> m_q;
|
||||
std::unordered_map<upair, unsigned> m_pairs_to_index;
|
||||
svector<upair> m_pairs; // inverse to index
|
||||
svector<unsigned> m_available_spots;
|
||||
public:
|
||||
binary_heap_upair_queue(unsigned size);
|
||||
|
||||
unsigned dequeue_available_spot();
|
||||
bool is_empty() const { return m_q.is_empty(); }
|
||||
|
||||
unsigned size() const {return m_q.size(); }
|
||||
|
||||
bool contains(unsigned i, unsigned j) const { return m_pairs_to_index.find(std::make_pair(i, j)) != m_pairs_to_index.end();
|
||||
}
|
||||
|
||||
void remove(unsigned i, unsigned j);
|
||||
bool ij_index_is_new(unsigned ij_index) const;
|
||||
void enqueue(unsigned i, unsigned j, const T & priority);
|
||||
void dequeue(unsigned & i, unsigned &j);
|
||||
T get_priority(unsigned i, unsigned j) const;
|
||||
#ifdef Z3DEBUG
|
||||
bool pair_to_index_is_a_bijection() const;
|
||||
bool available_spots_are_correct() const;
|
||||
bool is_correct() const {
|
||||
return m_q.is_consistent() && pair_to_index_is_a_bijection() && available_spots_are_correct();
|
||||
}
|
||||
#endif
|
||||
void resize(unsigned size) { m_q.resize(size); }
|
||||
};
|
||||
}
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/binary_heap_upair_queue.h"
|
||||
namespace lp {
|
||||
template <typename T> binary_heap_upair_queue<T>::binary_heap_upair_queue(unsigned size) : m_q(size), m_pairs(size) {
|
||||
for (unsigned i = 0; i < size; i++)
|
||||
m_available_spots.push_back(i);
|
||||
}
|
||||
|
||||
template <typename T> unsigned
|
||||
binary_heap_upair_queue<T>::dequeue_available_spot() {
|
||||
lp_assert(m_available_spots.empty() == false);
|
||||
unsigned ret = m_available_spots.back();
|
||||
m_available_spots.pop_back();
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_upair_queue<T>::remove(unsigned i, unsigned j) {
|
||||
upair p(i, j);
|
||||
auto it = m_pairs_to_index.find(p);
|
||||
if (it == m_pairs_to_index.end())
|
||||
return; // nothing to do
|
||||
m_q.remove(it->second);
|
||||
m_available_spots.push_back(it->second);
|
||||
m_pairs_to_index.erase(it);
|
||||
}
|
||||
|
||||
|
||||
template <typename T> bool binary_heap_upair_queue<T>::ij_index_is_new(unsigned ij_index) const {
|
||||
for (auto it : m_pairs_to_index) {
|
||||
if (it.second == ij_index)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_upair_queue<T>::enqueue(unsigned i, unsigned j, const T & priority) {
|
||||
upair p(i, j);
|
||||
auto it = m_pairs_to_index.find(p);
|
||||
unsigned ij_index;
|
||||
if (it == m_pairs_to_index.end()) {
|
||||
// it is a new pair, let us find a spot for it
|
||||
if (m_available_spots.empty()) {
|
||||
// we ran out of empty spots
|
||||
unsigned size_was = static_cast<unsigned>(m_pairs.size());
|
||||
unsigned new_size = size_was << 1;
|
||||
for (unsigned i = size_was; i < new_size; i++)
|
||||
m_available_spots.push_back(i);
|
||||
m_pairs.resize(new_size);
|
||||
}
|
||||
ij_index = dequeue_available_spot();
|
||||
// lp_assert(ij_index<m_pairs.size() && ij_index_is_new(ij_index));
|
||||
m_pairs[ij_index] = p;
|
||||
m_pairs_to_index[p] = ij_index;
|
||||
} else {
|
||||
ij_index = it->second;
|
||||
}
|
||||
m_q.enqueue(ij_index, priority);
|
||||
}
|
||||
|
||||
template <typename T> void binary_heap_upair_queue<T>::dequeue(unsigned & i, unsigned &j) {
|
||||
lp_assert(!m_q.is_empty());
|
||||
unsigned ij_index = m_q.dequeue();
|
||||
upair & p = m_pairs[ij_index];
|
||||
i = p.first;
|
||||
j = p.second;
|
||||
m_available_spots.push_back(ij_index);
|
||||
m_pairs_to_index.erase(p);
|
||||
}
|
||||
|
||||
|
||||
template <typename T> T binary_heap_upair_queue<T>::get_priority(unsigned i, unsigned j) const {
|
||||
auto it = m_pairs_to_index.find(std::make_pair(i, j));
|
||||
if (it == m_pairs_to_index.end())
|
||||
return T(0xFFFFFF); // big number
|
||||
return m_q.get_priority(it->second);
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T> bool binary_heap_upair_queue<T>::pair_to_index_is_a_bijection() const {
|
||||
std::set<int> tmp;
|
||||
for (auto p : m_pairs_to_index) {
|
||||
unsigned j = p.second;
|
||||
unsigned size = tmp.size();
|
||||
tmp.insert(j);
|
||||
if (tmp.size() == size)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T> bool binary_heap_upair_queue<T>::available_spots_are_correct() const {
|
||||
std::set<int> tmp;
|
||||
for (auto p : m_available_spots){
|
||||
tmp.insert(p);
|
||||
}
|
||||
if (tmp.size() != m_available_spots.size())
|
||||
return false;
|
||||
for (auto it : m_pairs_to_index)
|
||||
if (tmp.find(it.second) != tmp.end())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
@ -54,32 +54,40 @@ public :
|
|||
{}
|
||||
|
||||
|
||||
static void analyze_row(const C & row,
|
||||
static unsigned analyze_row(const C & row,
|
||||
unsigned bj, // basis column for the row
|
||||
const numeric_pair<mpq>& rs,
|
||||
unsigned row_or_term_index,
|
||||
B & bp) {
|
||||
bound_analyzer_on_row a(row, bj, rs, row_or_term_index, bp);
|
||||
a.analyze();
|
||||
return a.analyze();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void analyze() {
|
||||
unsigned analyze() {
|
||||
unsigned num_prop = 0;
|
||||
for (const auto & c : m_row) {
|
||||
if ((m_column_of_l == -2) && (m_column_of_u == -2))
|
||||
return;
|
||||
return 0;
|
||||
analyze_bound_on_var_on_coeff(c.var(), c.coeff());
|
||||
}
|
||||
++num_prop;
|
||||
if (m_column_of_u >= 0)
|
||||
limit_monoid_u_from_below();
|
||||
else if (m_column_of_u == -1)
|
||||
limit_all_monoids_from_below();
|
||||
else
|
||||
--num_prop;
|
||||
|
||||
++num_prop;
|
||||
if (m_column_of_l >= 0)
|
||||
limit_monoid_l_from_above();
|
||||
else if (m_column_of_l == -1)
|
||||
limit_all_monoids_from_above();
|
||||
else
|
||||
--num_prop;
|
||||
return num_prop;
|
||||
}
|
||||
|
||||
bool bound_is_available(unsigned j, bool lower_bound) {
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace lp {
|
||||
enum breakpoint_type {
|
||||
low_break, upper_break, fixed_break
|
||||
};
|
||||
template <typename X>
|
||||
struct breakpoint {
|
||||
unsigned m_j; // the basic column
|
||||
breakpoint_type m_type;
|
||||
X m_delta;
|
||||
breakpoint(){}
|
||||
breakpoint(unsigned j, X delta, breakpoint_type type):m_j(j), m_type(type), m_delta(delta) {}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
namespace lp {
|
||||
template <typename V>
|
||||
struct conversion_helper {
|
||||
static V get_lower_bound(const column_info<mpq> & ci) {
|
||||
return V(ci.get_lower_bound(), ci.lower_bound_is_strict()? 1 : 0);
|
||||
}
|
||||
|
||||
static V get_upper_bound(const column_info<mpq> & ci) {
|
||||
return V(ci.get_upper_bound(), ci.upper_bound_is_strict()? -1 : 0);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct conversion_helper <double> {
|
||||
static double get_upper_bound(const column_info<mpq> & ci) {
|
||||
if (!ci.upper_bound_is_strict())
|
||||
return ci.get_upper_bound().get_double();
|
||||
double eps = 0.00001;
|
||||
if (!ci.lower_bound_is_set())
|
||||
return ci.get_upper_bound().get_double() - eps;
|
||||
eps = std::min((ci.get_upper_bound() - ci.get_lower_bound()).get_double() / 1000, eps);
|
||||
return ci.get_upper_bound().get_double() - eps;
|
||||
}
|
||||
|
||||
static double get_lower_bound(const column_info<mpq> & ci) {
|
||||
if (!ci.lower_bound_is_strict())
|
||||
return ci.get_lower_bound().get_double();
|
||||
double eps = 0.00001;
|
||||
if (!ci.upper_bound_is_set())
|
||||
return ci.get_lower_bound().get_double() + eps;
|
||||
eps = std::min((ci.get_upper_bound() - ci.get_lower_bound()).get_double() / 1000, eps);
|
||||
return ci.get_lower_bound().get_double() + eps;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -19,9 +19,6 @@ Revision History:
|
|||
--*/
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/core_solver_pretty_printer_def.h"
|
||||
template lp::core_solver_pretty_printer<double, double>::core_solver_pretty_printer(const lp::lp_core_solver_base<double, double> &, std::ostream & out);
|
||||
template void lp::core_solver_pretty_printer<double, double>::print();
|
||||
template lp::core_solver_pretty_printer<double, double>::~core_solver_pretty_printer();
|
||||
template lp::core_solver_pretty_printer<lp::mpq, lp::mpq>::core_solver_pretty_printer(const lp::lp_core_solver_base<lp::mpq, lp::mpq> &, std::ostream & out);
|
||||
template void lp::core_solver_pretty_printer<lp::mpq, lp::mpq>::print();
|
||||
template lp::core_solver_pretty_printer<lp::mpq, lp::mpq>::~core_solver_pretty_printer();
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ class core_solver_pretty_printer {
|
|||
unsigned m_artificial_start;
|
||||
indexed_vector<T> m_w_buff;
|
||||
indexed_vector<T> m_ed_buff;
|
||||
vector<T> m_exact_column_norms;
|
||||
|
||||
|
||||
public:
|
||||
core_solver_pretty_printer(const lp_core_solver_base<T, X > & core_solver, std::ostream & out);
|
||||
|
|
@ -85,14 +85,7 @@ public:
|
|||
}
|
||||
|
||||
unsigned get_column_width(unsigned column);
|
||||
|
||||
unsigned regular_cell_width(unsigned row, unsigned column, const std::string & name) {
|
||||
return regular_cell_string(row, column, name).size();
|
||||
}
|
||||
|
||||
std::string regular_cell_string(unsigned row, unsigned column, std::string name);
|
||||
|
||||
|
||||
|
||||
void set_coeff(vector<string>& row, vector<string> & row_signs, unsigned col, const T & t, string name);
|
||||
|
||||
void print_x();
|
||||
|
|
@ -105,13 +98,7 @@ public:
|
|||
void print_lows();
|
||||
|
||||
void print_upps();
|
||||
|
||||
string get_exact_column_norm_string(unsigned col) {
|
||||
return T_to_string(m_exact_column_norms[col]);
|
||||
}
|
||||
|
||||
void print_exact_norms();
|
||||
|
||||
|
||||
void print_approx_norms();
|
||||
|
||||
void print();
|
||||
|
|
|
|||
|
|
@ -37,9 +37,8 @@ core_solver_pretty_printer<T, X>::core_solver_pretty_printer(const lp_core_solve
|
|||
m_signs(core_solver.m_A.row_count(), vector<string>(core_solver.m_A.column_count(), " ")),
|
||||
m_costs(ncols(), ""),
|
||||
m_cost_signs(ncols(), " "),
|
||||
m_rs(ncols(), zero_of_type<X>()),
|
||||
m_w_buff(core_solver.m_w),
|
||||
m_ed_buff(core_solver.m_ed) {
|
||||
m_rs(ncols(), zero_of_type<X>())
|
||||
{
|
||||
m_lower_bounds_title = "low";
|
||||
m_upp_bounds_title = "upp";
|
||||
m_exact_norm_title = "exact cn";
|
||||
|
|
@ -59,22 +58,13 @@ core_solver_pretty_printer<T, X>::core_solver_pretty_printer(const lp_core_solve
|
|||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::init_costs() {
|
||||
if (!m_core_solver.use_tableau()) {
|
||||
vector<T> local_y(m_core_solver.m_m());
|
||||
m_core_solver.solve_yB(local_y);
|
||||
for (unsigned i = 0; i < ncols(); i++) {
|
||||
if (m_core_solver.m_basis_heading[i] < 0) {
|
||||
T t = m_core_solver.m_costs[i] - m_core_solver.m_A.dot_product_with_column(local_y, i);
|
||||
set_coeff(m_costs, m_cost_signs, i, t, m_core_solver.column_name(i));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
for (unsigned i = 0; i < ncols(); i++) {
|
||||
if (m_core_solver.m_basis_heading[i] < 0) {
|
||||
set_coeff(m_costs, m_cost_signs, i, m_core_solver.m_d[i], m_core_solver.column_name(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <typename T, typename X> core_solver_pretty_printer<T, X>::~core_solver_pretty_printer() {
|
||||
|
|
@ -89,15 +79,7 @@ template <typename T, typename X> void core_solver_pretty_printer<T, X>::init_rs
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> T core_solver_pretty_printer<T, X>::current_column_norm() {
|
||||
T ret = zero_of_type<T>();
|
||||
for (auto i : m_core_solver.m_ed.m_index)
|
||||
ret += m_core_solver.m_ed[i] * m_core_solver.m_ed[i];
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::init_m_A_and_signs() {
|
||||
if (numeric_traits<T>::precise() && m_core_solver.m_settings.use_tableau()) {
|
||||
for (unsigned column = 0; column < ncols(); column++) {
|
||||
vector<T> t(nrows(), zero_of_type<T>());
|
||||
for (const auto & c : m_core_solver.m_A.m_columns[column]){
|
||||
|
|
@ -124,24 +106,7 @@ template <typename T, typename X> void core_solver_pretty_printer<T, X>::init_m_
|
|||
name);
|
||||
m_rs[row] += t[row] * m_core_solver.m_x[column];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (unsigned column = 0; column < ncols(); column++) {
|
||||
m_core_solver.solve_Bd(column, m_ed_buff, m_w_buff); // puts the result into m_core_solver.m_ed
|
||||
string name = m_core_solver.column_name(column);
|
||||
for (unsigned row = 0; row < nrows(); row ++) {
|
||||
set_coeff(
|
||||
m_A[row],
|
||||
m_signs[row],
|
||||
column,
|
||||
m_ed_buff[row],
|
||||
name);
|
||||
m_rs[row] += m_ed_buff[row] * m_core_solver.m_x[column];
|
||||
}
|
||||
if (!m_core_solver.use_tableau())
|
||||
m_exact_column_norms.push_back(current_column_norm() + T(1)); // a conversion missing 1 -> T
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::init_column_widths() {
|
||||
|
|
@ -174,7 +139,7 @@ template <typename T, typename X> void core_solver_pretty_printer<T, X>::adjust_
|
|||
case column_type::free_column:
|
||||
break;
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -190,21 +155,10 @@ template <typename T, typename X> unsigned core_solver_pretty_printer<T, X>:: ge
|
|||
w = cellw;
|
||||
}
|
||||
}
|
||||
if (!m_core_solver.use_tableau()) {
|
||||
w = std::max(w, (unsigned)T_to_string(m_exact_column_norms[column]).size());
|
||||
if (!m_core_solver.m_column_norms.empty())
|
||||
w = std::max(w, (unsigned)T_to_string(m_core_solver.m_column_norms[column]).size());
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
template <typename T, typename X> std::string core_solver_pretty_printer<T, X>::regular_cell_string(unsigned row, unsigned /* column */, std::string name) {
|
||||
T t = fabs(m_core_solver.m_ed[row]);
|
||||
if ( t == 1) return name;
|
||||
return T_to_string(t) + name;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::set_coeff(vector<string>& row, vector<string> & row_signs, unsigned col, const T & t, string name) {
|
||||
if (numeric_traits<T>::is_zero(t)) {
|
||||
return;
|
||||
|
|
@ -315,41 +269,15 @@ template <typename T, typename X> void core_solver_pretty_printer<T, X>::print_u
|
|||
m_out << std::endl;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::print_exact_norms() {
|
||||
if (m_core_solver.use_tableau()) return;
|
||||
int blanks = m_title_width + 1 - static_cast<int>(m_exact_norm_title.size());
|
||||
m_out << m_exact_norm_title;
|
||||
print_blanks_local(blanks, m_out);
|
||||
for (unsigned i = 0; i < ncols(); i++) {
|
||||
string s = get_exact_column_norm_string(i);
|
||||
int blanks = m_column_widths[i] - static_cast<int>(s.size());
|
||||
print_blanks_local(blanks, m_out);
|
||||
m_out << s << " ";
|
||||
}
|
||||
m_out << std::endl;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::print_approx_norms() {
|
||||
if (m_core_solver.use_tableau()) return;
|
||||
int blanks = m_title_width + 1 - static_cast<int>(m_approx_norm_title.size());
|
||||
m_out << m_approx_norm_title;
|
||||
print_blanks_local(blanks, m_out);
|
||||
for (unsigned i = 0; i < ncols(); i++) {
|
||||
string s = T_to_string(m_core_solver.m_column_norms[i]);
|
||||
int blanks = m_column_widths[i] - static_cast<int>(s.size());
|
||||
print_blanks_local(blanks, m_out);
|
||||
m_out << s << " ";
|
||||
}
|
||||
m_out << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void core_solver_pretty_printer<T, X>::print() {
|
||||
for (unsigned i = 0; i < nrows(); i++) {
|
||||
print_row(i);
|
||||
}
|
||||
print_exact_norms();
|
||||
if (!m_core_solver.m_column_norms.empty())
|
||||
print_approx_norms();
|
||||
m_out << std::endl;
|
||||
if (m_core_solver.inf_set().size()) {
|
||||
m_out << "inf columns: ";
|
||||
|
|
|
|||
|
|
@ -21,11 +21,6 @@ Revision History:
|
|||
#include "math/lp/dense_matrix_def.h"
|
||||
#ifdef Z3DEBUG
|
||||
#include "util/vector.h"
|
||||
template lp::dense_matrix<double, double> lp::operator*<double, double>(lp::matrix<double, double>&, lp::matrix<double, double>&);
|
||||
template void lp::dense_matrix<double, double>::apply_from_left(vector<double> &);
|
||||
template lp::dense_matrix<double, double>::dense_matrix(lp::matrix<double, double> const*);
|
||||
template lp::dense_matrix<double, double>::dense_matrix(unsigned int, unsigned int);
|
||||
template lp::dense_matrix<double, double>& lp::dense_matrix<double, double>::operator=(lp::dense_matrix<double, double> const&);
|
||||
template lp::dense_matrix<lp::mpq, lp::mpq>::dense_matrix(unsigned int, unsigned int);
|
||||
template lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::dense_matrix(lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> > const*);
|
||||
template void lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_left(vector<lp::mpq>&);
|
||||
|
|
@ -35,6 +30,5 @@ template lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::dense_matrix(uns
|
|||
template lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >& lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::operator=(lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> > const&);
|
||||
template lp::dense_matrix<lp::mpq, lp::numeric_pair<lp::mpq> > lp::operator*<lp::mpq, lp::numeric_pair<lp::mpq> >(lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&, lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template void lp::dense_matrix<lp::mpq, lp::numeric_pair< lp::mpq> >::apply_from_right( vector< lp::mpq> &);
|
||||
template void lp::dense_matrix<double,double>::apply_from_right(vector<double> &);
|
||||
template void lp::dense_matrix<lp::mpq, lp::mpq>::apply_from_left(vector<lp::mpq>&);
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -90,11 +90,7 @@ public:
|
|||
|
||||
void set_elem(unsigned i, unsigned j, const T& val) { m_values[i * m_n + j] = val; }
|
||||
|
||||
// This method pivots row i to row i0 by muliplying row i by
|
||||
// alpha and adding it to row i0.
|
||||
void pivot_row_to_row(unsigned i, const T& alpha, unsigned i0,
|
||||
const double & pivot_epsilon);
|
||||
|
||||
// This method pivots
|
||||
void swap_columns(unsigned a, unsigned b);
|
||||
|
||||
void swap_rows(unsigned a, unsigned b);
|
||||
|
|
|
|||
|
|
@ -150,17 +150,6 @@ template <typename T, typename X> void dense_matrix<T, X>::apply_from_left_to_X(
|
|||
}
|
||||
}
|
||||
|
||||
// This method pivots row i to row i0 by muliplying row i by
|
||||
// alpha and adding it to row i0.
|
||||
template <typename T, typename X> void dense_matrix<T, X>::pivot_row_to_row(unsigned i, const T& alpha, unsigned i0,
|
||||
const double & pivot_epsilon) {
|
||||
for (unsigned j = 0; j < m_n; j++) {
|
||||
m_values[i0 * m_n + j] += m_values[i * m_n + j] * alpha;
|
||||
if (fabs(m_values[i0 + m_n + j]) < pivot_epsilon) {
|
||||
m_values[i0 + m_n + j] = numeric_traits<T>::zero();;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void dense_matrix<T, X>::swap_columns(unsigned a, unsigned b) {
|
||||
for (unsigned i = 0; i < m_m; i++) {
|
||||
|
|
|
|||
|
|
@ -68,8 +68,8 @@ void emonics::pop(unsigned n) {
|
|||
TRACE("nla_solver_mons", tout << "pop: " << n << "\n";);
|
||||
SASSERT(invariant());
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
m_u_f_stack.pop_scope(1);
|
||||
m_ve.pop(1);
|
||||
m_u_f_stack.pop_scope(1);
|
||||
}
|
||||
SASSERT(invariant());
|
||||
SASSERT(monics_are_canonized());
|
||||
|
|
|
|||
|
|
@ -81,8 +81,8 @@ class emonics {
|
|||
}
|
||||
};
|
||||
|
||||
union_find<emonics> m_u_f;
|
||||
trail_stack m_u_f_stack;
|
||||
union_find<emonics> m_u_f;
|
||||
mutable svector<lpvar> m_find_key; // the key used when looking for a monic with the specific variables
|
||||
var_eqs<emonics>& m_ve;
|
||||
mutable vector<monic> m_monics; // set of monics
|
||||
|
|
@ -125,8 +125,8 @@ public:
|
|||
other calls to push/pop to the var_eqs should take place.
|
||||
*/
|
||||
emonics(var_eqs<emonics>& ve):
|
||||
m_u_f(*this),
|
||||
m_u_f_stack(),
|
||||
m_u_f(*this),
|
||||
m_ve(ve),
|
||||
m_visited(0),
|
||||
m_cg_hash(*this),
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <memory>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/eta_matrix_def.h"
|
||||
#ifdef Z3DEBUG
|
||||
template double lp::eta_matrix<double, double>::get_elem(unsigned int, unsigned int) const;
|
||||
template lp::mpq lp::eta_matrix<lp::mpq, lp::mpq>::get_elem(unsigned int, unsigned int) const;
|
||||
template lp::mpq lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::get_elem(unsigned int, unsigned int) const;
|
||||
#endif
|
||||
template void lp::eta_matrix<double, double>::apply_from_left(vector<double>&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<double, double>::apply_from_right(vector<double>&);
|
||||
template void lp::eta_matrix<double, double>::conjugate_by_permutation(lp::permutation_matrix<double, double>&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::mpq>::apply_from_left(vector<lp::mpq>&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::mpq>::apply_from_right(vector<lp::mpq>&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::mpq>::conjugate_by_permutation(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_left(vector<lp::numeric_pair<lp::mpq> >&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_right(vector<lp::mpq>&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::conjugate_by_permutation(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template void lp::eta_matrix<double, double>::apply_from_left_local<double>(lp::indexed_vector<double>&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::mpq>::apply_from_left_local<lp::mpq>(lp::indexed_vector<lp::mpq>&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_left_local<lp::mpq>(lp::indexed_vector<lp::mpq>&, lp::lp_settings&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_right(lp::indexed_vector<lp::mpq>&);
|
||||
template void lp::eta_matrix<lp::mpq, lp::mpq>::apply_from_right(lp::indexed_vector<lp::mpq>&);
|
||||
template void lp::eta_matrix<double, double>::apply_from_right(lp::indexed_vector<double>&);
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/tail_matrix.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
namespace lp {
|
||||
|
||||
// This is the sum of a unit matrix and a one-column matrix
|
||||
template <typename T, typename X>
|
||||
class eta_matrix
|
||||
: public tail_matrix<T, X> {
|
||||
#ifdef Z3DEBUG
|
||||
unsigned m_length;
|
||||
#endif
|
||||
unsigned m_column_index;
|
||||
public:
|
||||
sparse_vector<T> m_column_vector;
|
||||
T m_diagonal_element;
|
||||
#ifdef Z3DEBUG
|
||||
eta_matrix(unsigned column_index, unsigned length):
|
||||
#else
|
||||
eta_matrix(unsigned column_index):
|
||||
#endif
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
m_length(length),
|
||||
#endif
|
||||
m_column_index(column_index) {}
|
||||
|
||||
bool is_dense() const override { return false; }
|
||||
|
||||
void print(std::ostream & out) {
|
||||
print_matrix(*this, out);
|
||||
}
|
||||
|
||||
bool is_unit() {
|
||||
return m_column_vector.size() == 0 && m_diagonal_element == 1;
|
||||
}
|
||||
|
||||
bool set_diagonal_element(T const & diagonal_element) {
|
||||
m_diagonal_element = diagonal_element;
|
||||
return !lp_settings::is_eps_small_general(diagonal_element, 1e-12);
|
||||
}
|
||||
|
||||
const T & get_diagonal_element() const {
|
||||
return m_diagonal_element;
|
||||
}
|
||||
|
||||
void apply_from_left(vector<X> & w, lp_settings & ) override;
|
||||
|
||||
template <typename L>
|
||||
void apply_from_left_local(indexed_vector<L> & w, lp_settings & settings);
|
||||
|
||||
void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) override {
|
||||
apply_from_left_local(w, settings);
|
||||
}
|
||||
|
||||
|
||||
void push_back(unsigned row_index, T val ) {
|
||||
lp_assert(row_index != m_column_index);
|
||||
m_column_vector.push_back(row_index, val);
|
||||
}
|
||||
|
||||
void apply_from_right(vector<T> & w) override;
|
||||
void apply_from_right(indexed_vector<T> & w) override;
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
T get_elem(unsigned i, unsigned j) const override;
|
||||
unsigned row_count() const override { return m_length; }
|
||||
unsigned column_count() const override { return m_length; }
|
||||
void set_number_of_rows(unsigned m) override { m_length = m; }
|
||||
void set_number_of_columns(unsigned n) override { m_length = n; }
|
||||
#endif
|
||||
void divide_by_diagonal_element() {
|
||||
m_column_vector.divide(m_diagonal_element);
|
||||
}
|
||||
void conjugate_by_permutation(permutation_matrix<T, X> & p);
|
||||
};
|
||||
}
|
||||
|
|
@ -1,151 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/eta_matrix.h"
|
||||
namespace lp {
|
||||
|
||||
// This is the sum of a unit matrix and a one-column matrix
|
||||
template <typename T, typename X>
|
||||
void eta_matrix<T, X>::apply_from_left(vector<X> & w, lp_settings & ) {
|
||||
auto & w_at_column_index = w[m_column_index];
|
||||
for (auto & it : m_column_vector.m_data) {
|
||||
w[it.first] += w_at_column_index * it.second;
|
||||
}
|
||||
w_at_column_index /= m_diagonal_element;
|
||||
}
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
void eta_matrix<T, X>::
|
||||
apply_from_left_local(indexed_vector<L> & w, lp_settings & settings) {
|
||||
const L w_at_column_index = w[m_column_index];
|
||||
if (is_zero(w_at_column_index)) return;
|
||||
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(w[m_column_index] /= m_diagonal_element)) {
|
||||
w[m_column_index] = zero_of_type<L>();
|
||||
w.erase_from_index(m_column_index);
|
||||
}
|
||||
|
||||
for (auto & it : m_column_vector.m_data) {
|
||||
unsigned i = it.first;
|
||||
if (is_zero(w[i])) {
|
||||
L v = w[i] = w_at_column_index * it.second;
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(v)) {
|
||||
w[i] = zero_of_type<L>();
|
||||
continue;
|
||||
}
|
||||
w.m_index.push_back(i);
|
||||
} else {
|
||||
L v = w[i] += w_at_column_index * it.second;
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(v)) {
|
||||
w[i] = zero_of_type<L>();
|
||||
w.erase_from_index(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T, typename X>
|
||||
void eta_matrix<T, X>::apply_from_right(vector<T> & w) {
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T, X> deb(*this);
|
||||
// auto clone_w = clone_vector<T>(w, get_number_of_rows());
|
||||
// deb.apply_from_right(clone_w);
|
||||
#endif
|
||||
T t = w[m_column_index] / m_diagonal_element;
|
||||
for (auto & it : m_column_vector.m_data) {
|
||||
t += w[it.first] * it.second;
|
||||
}
|
||||
w[m_column_index] = t;
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<T>(clone_w, w, get_number_of_rows()));
|
||||
// delete clone_w;
|
||||
#endif
|
||||
}
|
||||
template <typename T, typename X>
|
||||
void eta_matrix<T, X>::apply_from_right(indexed_vector<T> & w) {
|
||||
if (w.m_index.empty())
|
||||
return;
|
||||
#ifdef Z3DEBUG
|
||||
// vector<T> wcopy(w.m_data);
|
||||
// apply_from_right(wcopy);
|
||||
#endif
|
||||
T & t = w[m_column_index];
|
||||
t /= m_diagonal_element;
|
||||
bool was_in_index = (!numeric_traits<T>::is_zero(t));
|
||||
|
||||
for (auto & it : m_column_vector.m_data) {
|
||||
t += w[it.first] * it.second;
|
||||
}
|
||||
|
||||
if (numeric_traits<T>::precise() ) {
|
||||
if (!numeric_traits<T>::is_zero(t)) {
|
||||
if (!was_in_index)
|
||||
w.m_index.push_back(m_column_index);
|
||||
} else {
|
||||
if (was_in_index)
|
||||
w.erase_from_index(m_column_index);
|
||||
}
|
||||
} else {
|
||||
if (!lp_settings::is_eps_small_general(t, 1e-14)) {
|
||||
if (!was_in_index)
|
||||
w.m_index.push_back(m_column_index);
|
||||
} else {
|
||||
if (was_in_index)
|
||||
w.erase_from_index(m_column_index);
|
||||
t = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(w.is_OK());
|
||||
// lp_assert(vectors_are_equal<T>(wcopy, w.m_data));
|
||||
#endif
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T, typename X>
|
||||
T eta_matrix<T, X>::get_elem(unsigned i, unsigned j) const {
|
||||
if (j == m_column_index){
|
||||
if (i == j) {
|
||||
return 1 / m_diagonal_element;
|
||||
}
|
||||
return m_column_vector[i];
|
||||
}
|
||||
|
||||
return i == j ? numeric_traits<T>::one() : numeric_traits<T>::zero();
|
||||
}
|
||||
#endif
|
||||
template <typename T, typename X>
|
||||
void eta_matrix<T, X>::conjugate_by_permutation(permutation_matrix<T, X> & p) {
|
||||
// this = p * this * p(-1)
|
||||
#ifdef Z3DEBUG
|
||||
// auto rev = p.get_reverse();
|
||||
// auto deb = ((*this) * rev);
|
||||
// deb = p * deb;
|
||||
#endif
|
||||
m_column_index = p.get_rev(m_column_index);
|
||||
for (auto & pair : m_column_vector.m_data) {
|
||||
pair.first = p.get_rev(pair.first);
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(deb == *this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
@ -114,9 +114,6 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void copy_column_to_indexed_vector(unsigned entering, indexed_vector<mpq> &w ) const {
|
||||
lp_assert(false); // not implemented
|
||||
}
|
||||
general_matrix operator*(const general_matrix & m) const {
|
||||
lp_assert(m.row_count() == column_count());
|
||||
general_matrix ret(row_count(), m.column_count());
|
||||
|
|
@ -172,24 +169,7 @@ public:
|
|||
return r;
|
||||
}
|
||||
|
||||
// bool create_upper_triangle(general_matrix& m, vector<mpq>& x) {
|
||||
// for (unsigned i = 1; i < m.row_count(); i++) {
|
||||
// lp_assert(false); // to be continued
|
||||
// }
|
||||
// }
|
||||
|
||||
// bool solve_A_x_equal_b(const general_matrix& m, vector<mpq>& x, const vector<mpq>& b) const {
|
||||
// auto m_copy = m;
|
||||
// // for square matrices
|
||||
// lp_assert(row_count() == b.size());
|
||||
// lp_assert(x.size() == column_count());
|
||||
// lp_assert(row_count() == column_count());
|
||||
// x = b;
|
||||
// create_upper_triangle(copy_of_m, x);
|
||||
// solve_on_triangle(copy_of_m, x);
|
||||
// }
|
||||
//
|
||||
|
||||
|
||||
void transpose_rows(unsigned i, unsigned l) {
|
||||
lp_assert(i != l);
|
||||
m_row_permutation.transpose_from_right(i, l);
|
||||
|
|
|
|||
|
|
@ -377,10 +377,21 @@ bool gomory::is_gomory_cut_target(const row_strip<mpq>& row) {
|
|||
}
|
||||
|
||||
int gomory::find_basic_var() {
|
||||
int result = -1;
|
||||
unsigned n = 0;
|
||||
int result = -1;
|
||||
unsigned min_row_size = UINT_MAX;
|
||||
// Prefer smaller row size
|
||||
|
||||
#if 0
|
||||
result = lia.select_int_infeasible_var();
|
||||
|
||||
if (result == -1)
|
||||
return result;
|
||||
|
||||
const row_strip<mpq>& row = lra.get_row(lia.row_of_basic_column(result));
|
||||
if (is_gomory_cut_target(row))
|
||||
return result;
|
||||
result = -1;
|
||||
#endif
|
||||
|
||||
for (unsigned j : lra.r_basis()) {
|
||||
if (!lia.column_is_int_inf(j))
|
||||
|
|
@ -389,6 +400,7 @@ int gomory::find_basic_var() {
|
|||
if (!is_gomory_cut_target(row))
|
||||
continue;
|
||||
IF_VERBOSE(20, lia.display_row_info(verbose_stream(), lia.row_of_basic_column(j)));
|
||||
// Prefer smaller row size
|
||||
if (min_row_size == UINT_MAX ||
|
||||
2*row.size() < min_row_size ||
|
||||
(4*row.size() < 5*min_row_size && lia.random() % (++n) == 0)) {
|
||||
|
|
|
|||
|
|
@ -248,9 +248,8 @@ branch y_i >= ceil(y0_i) is impossible.
|
|||
|
||||
bool hnf_cutter::init_terms_for_hnf_cut() {
|
||||
clear();
|
||||
for (unsigned i = 0; i < lra.terms().size() && !is_full(); i++) {
|
||||
for (unsigned i = 0; i < lra.terms().size() && !is_full(); i++)
|
||||
try_add_term_to_A_for_hnf(tv::term(i));
|
||||
}
|
||||
return hnf_has_var_with_non_integral_value();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,15 +43,4 @@ public:
|
|||
m_value = val;
|
||||
}
|
||||
};
|
||||
#ifdef Z3DEBUG
|
||||
template <typename X>
|
||||
bool check_vector_for_small_values(indexed_vector<X> & w, lp_settings & settings) {
|
||||
for (unsigned i : w.m_index) {
|
||||
const X & v = w[i];
|
||||
if ((!is_zero(v)) && settings.abs_val_is_smaller_than_drop_tolerance(v))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,6 @@ Revision History:
|
|||
#include "util/vector.h"
|
||||
#include "math/lp/indexed_vector_def.h"
|
||||
namespace lp {
|
||||
template void indexed_vector<double>::clear();
|
||||
template void indexed_vector<double>::clear_all();
|
||||
template void indexed_vector<double>::erase_from_index(unsigned int);
|
||||
template void indexed_vector<double>::set_value(const double&, unsigned int);
|
||||
template void indexed_vector<mpq>::clear();
|
||||
template void indexed_vector<unsigned>::clear();
|
||||
template void indexed_vector<mpq>::clear_all();
|
||||
|
|
@ -32,22 +28,8 @@ template void indexed_vector<mpq>::resize(unsigned int);
|
|||
template void indexed_vector<unsigned>::resize(unsigned int);
|
||||
template void indexed_vector<mpq>::set_value(const mpq&, unsigned int);
|
||||
template void indexed_vector<unsigned>::set_value(const unsigned&, unsigned int);
|
||||
#ifdef Z3DEBUG
|
||||
template bool indexed_vector<unsigned>::is_OK() const;
|
||||
template bool indexed_vector<double>::is_OK() const;
|
||||
template bool indexed_vector<mpq>::is_OK() const;
|
||||
template bool indexed_vector<lp::numeric_pair<mpq> >::is_OK() const;
|
||||
#endif
|
||||
template void lp::indexed_vector< lp::mpq>::print(std::basic_ostream<char,struct std::char_traits<char> > &);
|
||||
template void lp::indexed_vector<double>::print(std::basic_ostream<char,struct std::char_traits<char> > &);
|
||||
template void lp::indexed_vector<lp::numeric_pair<lp::mpq> >::print(std::ostream&);
|
||||
}
|
||||
// template void lp::print_vector<double, vectro>(vector<double> const&, std::ostream&);
|
||||
// template void lp::print_vector<unsigned int>(vector<unsigned int> const&, std::ostream&);
|
||||
// template void lp::print_vector<std::string>(vector<std::string> const&, std::ostream&);
|
||||
// template void lp::print_vector<lp::numeric_pair<lp::mpq> >(vector<lp::numeric_pair<lp::mpq>> const&, std::ostream&);
|
||||
template void lp::indexed_vector<double>::resize(unsigned int);
|
||||
// template void lp::print_vector< lp::mpq>(vector< lp::mpq> const &, std::basic_ostream<char, std::char_traits<char> > &);
|
||||
// template void lp::print_vector<std::pair<lp::mpq, unsigned int> >(vector<std::pair<lp::mpq, unsigned int>> const&, std::ostream&);
|
||||
template void lp::indexed_vector<lp::numeric_pair<lp::mpq> >::erase_from_index(unsigned int);
|
||||
|
||||
|
|
|
|||
|
|
@ -99,47 +99,9 @@ public:
|
|||
return m_data[i];
|
||||
}
|
||||
|
||||
void clean_up() {
|
||||
#if 0==1
|
||||
for (unsigned k = 0; k < m_index.size(); k++) {
|
||||
unsigned i = m_index[k];
|
||||
T & v = m_data[i];
|
||||
if (lp_settings::is_eps_small_general(v, 1e-14)) {
|
||||
v = zero_of_type<T>();
|
||||
m_index.erase(m_index.begin() + k--);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
vector<unsigned> index_copy;
|
||||
for (unsigned i : m_index) {
|
||||
T & v = m_data[i];
|
||||
if (!lp_settings::is_eps_small_general(v, 1e-14)) {
|
||||
index_copy.push_back(i);
|
||||
} else if (!numeric_traits<T>::is_zero(v)) {
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
m_index = index_copy;
|
||||
}
|
||||
|
||||
|
||||
void erase_from_index(unsigned j);
|
||||
|
||||
void add_value_at_index_with_drop_tolerance(unsigned j, const T& val_to_add) {
|
||||
T & v = m_data[j];
|
||||
bool was_zero = is_zero(v);
|
||||
v += val_to_add;
|
||||
if (lp_settings::is_eps_small_general(v, 1e-14)) {
|
||||
v = zero_of_type<T>();
|
||||
if (!was_zero) {
|
||||
erase_from_index(j);
|
||||
}
|
||||
} else {
|
||||
if (was_zero)
|
||||
m_index.push_back(j);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void add_value_at_index(unsigned j, const T& val_to_add) {
|
||||
T & v = m_data[j];
|
||||
bool was_zero = is_zero(v);
|
||||
|
|
@ -153,18 +115,6 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void restore_index_and_clean_from_data() {
|
||||
m_index.resize(0);
|
||||
for (unsigned i = 0; i < m_data.size(); i++) {
|
||||
T & v = m_data[i];
|
||||
if (lp_settings::is_eps_small_general(v, 1e-14)) {
|
||||
v = zero_of_type<T>();
|
||||
} else {
|
||||
m_index.push_back(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ival {
|
||||
unsigned m_var;
|
||||
const T & m_coeff;
|
||||
|
|
@ -215,9 +165,6 @@ public:
|
|||
}
|
||||
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
bool is_OK() const;
|
||||
#endif
|
||||
void print(std::ostream & out);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,14 +24,6 @@ Revision History:
|
|||
#include "math/lp/lp_settings.h"
|
||||
namespace lp {
|
||||
|
||||
template <typename T>
|
||||
void print_sparse_vector(const vector<T> & t, std::ostream & out) {
|
||||
for (unsigned i = 0; i < t.size(); i++) {
|
||||
if (is_zero(t[i]))continue;
|
||||
out << "[" << i << "] = " << t[i] << ", ";
|
||||
}
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
void print_vector_as_doubles(const vector<mpq> & t, std::ostream & out) {
|
||||
for (unsigned i = 0; i < t.size(); i++)
|
||||
|
|
@ -43,7 +35,7 @@ template <typename T>
|
|||
void indexed_vector<T>::resize(unsigned data_size) {
|
||||
clear();
|
||||
m_data.resize(data_size, numeric_traits<T>::zero());
|
||||
lp_assert(is_OK());
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
|
@ -72,33 +64,6 @@ void indexed_vector<T>::erase_from_index(unsigned j) {
|
|||
m_index.erase(it);
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T>
|
||||
bool indexed_vector<T>::is_OK() const {
|
||||
return true;
|
||||
const double drop_eps = 1e-14;
|
||||
for (unsigned i = 0; i < m_data.size(); i++) {
|
||||
if (!is_zero(m_data[i]) && lp_settings::is_eps_small_general(m_data[i], drop_eps)) {
|
||||
return false;
|
||||
}
|
||||
if (lp_settings::is_eps_small_general(m_data[i], drop_eps) != (std::find(m_index.begin(), m_index.end(), i) == m_index.end())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_set<unsigned> s;
|
||||
for (unsigned i : m_index) {
|
||||
//no duplicates!!!
|
||||
if (s.find(i) != s.end())
|
||||
return false;
|
||||
s.insert(i);
|
||||
if (i >= m_data.size())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
template <typename T>
|
||||
void indexed_vector<T>::print(std::ostream & out) {
|
||||
out << "m_index " << std::endl;
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
Nikolaj Bjorner (nbjorner)
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "math/lp/binary_heap_priority_queue.h"
|
||||
namespace lp {
|
||||
|
||||
class indexer_of_constraints {
|
||||
binary_heap_priority_queue<unsigned> m_queue_of_released_indices;
|
||||
unsigned m_max;
|
||||
public:
|
||||
indexer_of_constraints() :m_max(0) {}
|
||||
unsigned get_new_index() {
|
||||
unsigned ret;
|
||||
if (m_queue_of_released_indices.is_empty()) {
|
||||
ret = m_max++;
|
||||
}
|
||||
else {
|
||||
ret = m_queue_of_released_indices.dequeue();
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
void release_index(unsigned i) {
|
||||
m_queue_of_released_indices.enqueue(i, i);
|
||||
};
|
||||
unsigned max() const { return m_max; }
|
||||
};
|
||||
}
|
||||
|
|
@ -52,16 +52,22 @@ lia_move int_branch::create_branch_on_column(int j) {
|
|||
|
||||
|
||||
int int_branch::find_inf_int_base_column() {
|
||||
|
||||
#if 0
|
||||
return lia.select_int_infeasible_var();
|
||||
#endif
|
||||
|
||||
int result = -1;
|
||||
mpq range;
|
||||
mpq new_range;
|
||||
mpq small_range_thresold(1024);
|
||||
mpq small_value(1024);
|
||||
unsigned n = 0;
|
||||
lar_core_solver & lcs = lra.m_mpq_lar_core_solver;
|
||||
unsigned prev_usage = 0; // to quiet down the compile
|
||||
unsigned k = 0;
|
||||
unsigned usage;
|
||||
unsigned j;
|
||||
|
||||
// this loop looks for a column with the most usages, but breaks when
|
||||
// a column with a small span of bounds is found
|
||||
for (; k < lra.r_basis().size(); k++) {
|
||||
|
|
@ -69,12 +75,13 @@ int int_branch::find_inf_int_base_column() {
|
|||
if (!lia.column_is_int_inf(j))
|
||||
continue;
|
||||
usage = lra.usage_in_terms(j);
|
||||
if (lia.is_boxed(j) && (range = lcs.m_r_upper_bounds()[j].x - lcs.m_r_lower_bounds()[j].x - rational(2*usage)) <= small_range_thresold) {
|
||||
if (lia.is_boxed(j) && (range = lcs.m_r_upper_bounds()[j].x - lcs.m_r_lower_bounds()[j].x - rational(2*usage)) <= small_value) {
|
||||
result = j;
|
||||
k++;
|
||||
n = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (n == 0 || usage > prev_usage) {
|
||||
result = j;
|
||||
prev_usage = usage;
|
||||
|
|
|
|||
|
|
@ -344,7 +344,6 @@ bool int_solver::get_freedom_interval_for_column(unsigned j, bool & inf_l, impq
|
|||
set_upper(u, inf_u, upper_bound(j) - xj);
|
||||
|
||||
|
||||
lp_assert(settings().use_tableau());
|
||||
const auto & A = lra.A_r();
|
||||
TRACE("random_update", tout << "m = " << m << "\n";);
|
||||
|
||||
|
|
@ -633,4 +632,73 @@ bool int_solver::non_basic_columns_are_at_bounds() const {
|
|||
return true;
|
||||
}
|
||||
|
||||
int int_solver::select_int_infeasible_var() {
|
||||
int result = -1;
|
||||
mpq range;
|
||||
mpq new_range;
|
||||
mpq small_value(1024);
|
||||
unsigned n = 0;
|
||||
lar_core_solver & lcs = lra.m_mpq_lar_core_solver;
|
||||
unsigned prev_usage = 0; // to quiet down the compile
|
||||
unsigned k = 0;
|
||||
unsigned usage;
|
||||
unsigned j;
|
||||
|
||||
enum state { small_box, is_small_value, any_value, not_found };
|
||||
state st = not_found;
|
||||
|
||||
// 1. small box
|
||||
// 2. small value
|
||||
// 3. any value
|
||||
for (; k < lra.r_basis().size(); k++) {
|
||||
j = lra.r_basis()[k];
|
||||
if (!column_is_int_inf(j))
|
||||
continue;
|
||||
usage = lra.usage_in_terms(j);
|
||||
if (is_boxed(j) && (new_range = lcs.m_r_upper_bounds()[j].x - lcs.m_r_lower_bounds()[j].x - rational(2*usage)) <= small_value) {
|
||||
SASSERT(!is_fixed(j));
|
||||
if (st != small_box) {
|
||||
n = 0;
|
||||
st = small_box;
|
||||
}
|
||||
if (n == 0 || new_range < range) {
|
||||
result = j;
|
||||
range = new_range;
|
||||
n = 1;
|
||||
}
|
||||
else if (new_range == range && (random() % (++n) == 0)) {
|
||||
result = j;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (st == small_box)
|
||||
continue;
|
||||
impq const& value = get_value(j);
|
||||
if (abs(value.x) < small_value ||
|
||||
(has_upper(j) && small_value > upper_bound(j).x - value.x) ||
|
||||
(has_lower(j) && small_value > value.x - lower_bound(j).x)) {
|
||||
if (st != is_small_value) {
|
||||
n = 0;
|
||||
st = is_small_value;
|
||||
}
|
||||
if (random() % (++n) == 0)
|
||||
result = j;
|
||||
}
|
||||
if (st == is_small_value)
|
||||
continue;
|
||||
SASSERT(st == not_found || st == any_value);
|
||||
st = any_value;
|
||||
if (n == 0 /*|| usage > prev_usage*/) {
|
||||
result = j;
|
||||
prev_usage = usage;
|
||||
n = 1;
|
||||
}
|
||||
else if (usage > 0 && /*usage == prev_usage && */ (random() % (++n) == 0))
|
||||
result = j;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,5 +129,8 @@ public:
|
|||
void find_feasible_solution();
|
||||
lia_move hnf_cut();
|
||||
void patch_nbasic_column(unsigned j) { m_patcher.patch_nbasic_column(j); }
|
||||
|
||||
int select_int_infeasible_var();
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ inline std::string lconstraint_kind_string(lconstraint_kind t) {
|
|||
case EQ: return std::string("=");
|
||||
case NE: return std::string("!=");
|
||||
}
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
return std::string(); // it is unreachable
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,24 +12,17 @@ Author:
|
|||
#include "math/lp/lp_core_solver_base.h"
|
||||
#include <algorithm>
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include "math/lp/binary_heap_priority_queue.h"
|
||||
#include "math/lp/breakpoint.h"
|
||||
#include "math/lp/lp_primal_core_solver.h"
|
||||
#include "math/lp/stacked_vector.h"
|
||||
#include "math/lp/lar_solution_signature.h"
|
||||
#include "util/stacked_value.h"
|
||||
namespace lp {
|
||||
|
||||
class lar_core_solver {
|
||||
// m_sign_of_entering is set to 1 if the entering variable needs
|
||||
// to grow and is set to -1 otherwise
|
||||
int m_sign_of_entering_delta;
|
||||
vector<std::pair<mpq, unsigned>> m_infeasible_linear_combination;
|
||||
int m_infeasible_sum_sign; // todo: get rid of this field
|
||||
vector<numeric_pair<mpq>> m_right_sides_dummy;
|
||||
vector<mpq> m_costs_dummy;
|
||||
vector<double> m_d_right_sides_dummy;
|
||||
vector<double> m_d_costs_dummy;
|
||||
|
||||
public:
|
||||
stacked_value<simplex_strategy_enum> m_stacked_simplex_strategy;
|
||||
stacked_vector<column_type> m_column_types;
|
||||
|
|
@ -42,23 +35,9 @@ public:
|
|||
vector<unsigned> m_r_basis;
|
||||
vector<unsigned> m_r_nbasis;
|
||||
vector<int> m_r_heading;
|
||||
stacked_vector<unsigned> m_r_columns_nz;
|
||||
stacked_vector<unsigned> m_r_rows_nz;
|
||||
|
||||
// d - solver fields, for doubles
|
||||
vector<double> m_d_x; // the solution in doubles
|
||||
vector<double> m_d_lower_bounds;
|
||||
vector<double> m_d_upper_bounds;
|
||||
static_matrix<double, double> m_d_A;
|
||||
stacked_vector<unsigned> m_d_pushed_basis;
|
||||
vector<unsigned> m_d_basis;
|
||||
vector<unsigned> m_d_nbasis;
|
||||
vector<int> m_d_heading;
|
||||
|
||||
|
||||
lp_primal_core_solver<mpq, numeric_pair<mpq>> m_r_solver; // solver in rational numbers
|
||||
|
||||
lp_primal_core_solver<double, double> m_d_solver; // solver in doubles
|
||||
|
||||
lar_core_solver(
|
||||
lp_settings & settings,
|
||||
|
|
@ -66,6 +45,7 @@ public:
|
|||
);
|
||||
|
||||
lp_settings & settings() { return m_r_solver.m_settings;}
|
||||
|
||||
const lp_settings & settings() const { return m_r_solver.m_settings;}
|
||||
|
||||
int get_infeasible_sum_sign() const { return m_infeasible_sum_sign; }
|
||||
|
|
@ -79,8 +59,7 @@ public:
|
|||
|
||||
column_type get_column_type(unsigned j) { return m_column_types[j];}
|
||||
|
||||
void calculate_pivot_row(unsigned i);
|
||||
|
||||
|
||||
void print_pivot_row(std::ostream & out, unsigned row_index) const {
|
||||
for (unsigned j : m_r_solver.m_pivot_row.m_index) {
|
||||
if (numeric_traits<mpq>::is_pos(m_r_solver.m_pivot_row.m_data[j]))
|
||||
|
|
@ -96,21 +75,9 @@ public:
|
|||
m_r_solver.print_column_bound_info(m_r_solver.m_basis[row_index], out);
|
||||
}
|
||||
|
||||
|
||||
void advance_on_sorted_breakpoints(unsigned entering);
|
||||
|
||||
void change_slope_on_breakpoint(unsigned entering, breakpoint<numeric_pair<mpq>> * b, mpq & slope_at_entering);
|
||||
|
||||
bool row_is_infeasible(unsigned row);
|
||||
|
||||
bool row_is_evidence(unsigned row);
|
||||
|
||||
bool find_evidence_row();
|
||||
|
||||
|
||||
void prefix_r();
|
||||
|
||||
void prefix_d();
|
||||
|
||||
unsigned m_m() const { return m_r_A.row_count(); }
|
||||
|
||||
unsigned m_n() const { return m_r_A.column_count(); }
|
||||
|
|
@ -122,8 +89,6 @@ public:
|
|||
template <typename L>
|
||||
int get_sign(const L & v) { return v > zero_of_type<L>() ? 1 : (v < zero_of_type<L>() ? -1 : 0); }
|
||||
|
||||
void fill_evidence(unsigned row);
|
||||
|
||||
unsigned get_number_of_non_ints() const;
|
||||
|
||||
void solve();
|
||||
|
|
@ -136,422 +101,40 @@ public:
|
|||
|
||||
void fill_not_improvable_zero_sum();
|
||||
|
||||
void pop_basis(unsigned k) {
|
||||
if (!settings().use_tableau()) {
|
||||
m_r_pushed_basis.pop(k);
|
||||
m_r_basis = m_r_pushed_basis();
|
||||
m_r_solver.init_basis_heading_and_non_basic_columns_vector();
|
||||
m_d_pushed_basis.pop(k);
|
||||
m_d_basis = m_d_pushed_basis();
|
||||
m_d_solver.init_basis_heading_and_non_basic_columns_vector();
|
||||
} else {
|
||||
m_d_basis = m_r_basis;
|
||||
m_d_nbasis = m_r_nbasis;
|
||||
m_d_heading = m_r_heading;
|
||||
}
|
||||
}
|
||||
|
||||
void push() {
|
||||
lp_assert(m_r_solver.basis_heading_is_correct());
|
||||
lp_assert(!need_to_presolve_with_double_solver() || m_d_solver.basis_heading_is_correct());
|
||||
lp_assert(m_column_types.size() == m_r_A.column_count());
|
||||
m_stacked_simplex_strategy = settings().simplex_strategy();
|
||||
m_stacked_simplex_strategy.push();
|
||||
m_column_types.push();
|
||||
// rational
|
||||
if (!settings().use_tableau())
|
||||
m_r_A.push();
|
||||
m_r_lower_bounds.push();
|
||||
m_r_upper_bounds.push();
|
||||
if (!settings().use_tableau()) {
|
||||
push_vector(m_r_pushed_basis, m_r_basis);
|
||||
push_vector(m_r_columns_nz, m_r_solver.m_columns_nz);
|
||||
push_vector(m_r_rows_nz, m_r_solver.m_rows_nz);
|
||||
}
|
||||
|
||||
m_d_A.push();
|
||||
if (!settings().use_tableau())
|
||||
push_vector(m_d_pushed_basis, m_d_basis);
|
||||
|
||||
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
void push_vector(stacked_vector<K> & pushed_vector, const vector<K> & vector) {
|
||||
lp_assert(pushed_vector.size() <= vector.size());
|
||||
for (unsigned i = 0; i < vector.size();i++) {
|
||||
if (i == pushed_vector.size()) {
|
||||
pushed_vector.push_back(vector[i]);
|
||||
} else {
|
||||
pushed_vector[i] = vector[i];
|
||||
}
|
||||
}
|
||||
pushed_vector.push();
|
||||
}
|
||||
|
||||
void pop_markowitz_counts(unsigned k) {
|
||||
m_r_columns_nz.pop(k);
|
||||
m_r_rows_nz.pop(k);
|
||||
m_r_solver.m_columns_nz.resize(m_r_columns_nz.size());
|
||||
m_r_solver.m_rows_nz.resize(m_r_rows_nz.size());
|
||||
for (unsigned i = 0; i < m_r_columns_nz.size(); i++)
|
||||
m_r_solver.m_columns_nz[i] = m_r_columns_nz[i];
|
||||
for (unsigned i = 0; i < m_r_rows_nz.size(); i++)
|
||||
m_r_solver.m_rows_nz[i] = m_r_rows_nz[i];
|
||||
}
|
||||
|
||||
|
||||
void pop(unsigned k) {
|
||||
// rationals
|
||||
if (!settings().use_tableau())
|
||||
m_r_A.pop(k);
|
||||
m_r_lower_bounds.pop(k);
|
||||
m_r_upper_bounds.pop(k);
|
||||
m_column_types.pop(k);
|
||||
|
||||
delete m_r_solver.m_factorization;
|
||||
m_r_solver.m_factorization = nullptr;
|
||||
m_r_x.resize(m_r_A.column_count());
|
||||
m_r_solver.m_costs.resize(m_r_A.column_count());
|
||||
m_r_solver.m_d.resize(m_r_A.column_count());
|
||||
if(!settings().use_tableau())
|
||||
pop_markowitz_counts(k);
|
||||
m_d_A.pop(k);
|
||||
// doubles
|
||||
delete m_d_solver.m_factorization;
|
||||
m_d_solver.m_factorization = nullptr;
|
||||
|
||||
m_d_x.resize(m_d_A.column_count());
|
||||
pop_basis(k);
|
||||
m_stacked_simplex_strategy.pop(k);
|
||||
settings().set_simplex_strategy(m_stacked_simplex_strategy);
|
||||
lp_assert(m_r_solver.basis_heading_is_correct());
|
||||
lp_assert(!need_to_presolve_with_double_solver() || m_d_solver.basis_heading_is_correct());
|
||||
}
|
||||
|
||||
bool need_to_presolve_with_double_solver() const {
|
||||
return settings().simplex_strategy() == simplex_strategy_enum::lu;
|
||||
}
|
||||
|
||||
template <typename L>
|
||||
bool is_zero_vector(const vector<L> & b) {
|
||||
for (const L & m: b)
|
||||
if (!is_zero(m)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool update_xj_and_get_delta(unsigned j, non_basic_column_value_position pos_type, numeric_pair<mpq> & delta) {
|
||||
auto & x = m_r_x[j];
|
||||
switch (pos_type) {
|
||||
case at_lower_bound:
|
||||
if (x == m_r_solver.m_lower_bounds[j])
|
||||
return false;
|
||||
delta = m_r_solver.m_lower_bounds[j] - x;
|
||||
m_r_solver.m_x[j] = m_r_solver.m_lower_bounds[j];
|
||||
break;
|
||||
case at_fixed:
|
||||
case at_upper_bound:
|
||||
if (x == m_r_solver.m_upper_bounds[j])
|
||||
return false;
|
||||
delta = m_r_solver.m_upper_bounds[j] - x;
|
||||
x = m_r_solver.m_upper_bounds[j];
|
||||
break;
|
||||
case free_of_bounds: {
|
||||
return false;
|
||||
}
|
||||
case not_at_bound:
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::free_column:
|
||||
return false;
|
||||
case column_type::upper_bound:
|
||||
delta = m_r_solver.m_upper_bounds[j] - x;
|
||||
x = m_r_solver.m_upper_bounds[j];
|
||||
break;
|
||||
case column_type::lower_bound:
|
||||
delta = m_r_solver.m_lower_bounds[j] - x;
|
||||
x = m_r_solver.m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::boxed:
|
||||
if (x > m_r_solver.m_upper_bounds[j]) {
|
||||
delta = m_r_solver.m_upper_bounds[j] - x;
|
||||
x += m_r_solver.m_upper_bounds[j];
|
||||
} else {
|
||||
delta = m_r_solver.m_lower_bounds[j] - x;
|
||||
x = m_r_solver.m_lower_bounds[j];
|
||||
}
|
||||
break;
|
||||
case column_type::fixed:
|
||||
delta = m_r_solver.m_lower_bounds[j] - x;
|
||||
x = m_r_solver.m_lower_bounds[j];
|
||||
break;
|
||||
|
||||
default:
|
||||
lp_assert(false);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
m_r_solver.remove_column_from_inf_set(j);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void prepare_solver_x_with_signature_tableau(const lar_solution_signature & signature) {
|
||||
lp_assert(m_r_solver.inf_set_is_correct());
|
||||
for (auto &t : signature) {
|
||||
unsigned j = t.first;
|
||||
if (m_r_heading[j] >= 0)
|
||||
continue;
|
||||
auto pos_type = t.second;
|
||||
numeric_pair<mpq> delta;
|
||||
if (!update_xj_and_get_delta(j, pos_type, delta))
|
||||
continue;
|
||||
for (const auto & cc : m_r_solver.m_A.m_columns[j]){
|
||||
unsigned i = cc.var();
|
||||
unsigned jb = m_r_solver.m_basis[i];
|
||||
m_r_solver.add_delta_to_x_and_track_feasibility(jb, - delta * m_r_solver.m_A.get_val(cc));
|
||||
}
|
||||
CASSERT("A_off", m_r_solver.A_mult_x_is_off() == false);
|
||||
}
|
||||
lp_assert(m_r_solver.inf_set_is_correct());
|
||||
}
|
||||
|
||||
|
||||
template <typename L, typename K>
|
||||
void prepare_solver_x_with_signature(const lar_solution_signature & signature, lp_primal_core_solver<L,K> & s) {
|
||||
for (auto &t : signature) {
|
||||
unsigned j = t.first;
|
||||
lp_assert(m_r_heading[j] < 0);
|
||||
auto pos_type = t.second;
|
||||
switch (pos_type) {
|
||||
case at_lower_bound:
|
||||
s.m_x[j] = s.m_lower_bounds[j];
|
||||
break;
|
||||
case at_fixed:
|
||||
case at_upper_bound:
|
||||
s.m_x[j] = s.m_upper_bounds[j];
|
||||
break;
|
||||
case free_of_bounds: {
|
||||
s.m_x[j] = zero_of_type<K>();
|
||||
continue;
|
||||
}
|
||||
case not_at_bound:
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::free_column:
|
||||
lp_assert(false); // unreachable
|
||||
case column_type::upper_bound:
|
||||
s.m_x[j] = s.m_upper_bounds[j];
|
||||
break;
|
||||
case column_type::lower_bound:
|
||||
s.m_x[j] = s.m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::boxed:
|
||||
if (settings().random_next() % 2) {
|
||||
s.m_x[j] = s.m_lower_bounds[j];
|
||||
} else {
|
||||
s.m_x[j] = s.m_upper_bounds[j];
|
||||
}
|
||||
break;
|
||||
case column_type::fixed:
|
||||
s.m_x[j] = s.m_lower_bounds[j];
|
||||
break;
|
||||
default:
|
||||
lp_assert(false);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
lp_assert(is_zero_vector(s.m_b));
|
||||
s.solve_Ax_eq_b();
|
||||
}
|
||||
|
||||
template <typename L, typename K>
|
||||
void catch_up_in_lu_in_reverse(const vector<unsigned> & trace_of_basis_change, lp_primal_core_solver<L,K> & cs) {
|
||||
// recover the previous working basis
|
||||
for (unsigned i = trace_of_basis_change.size(); i > 0; i-= 2) {
|
||||
unsigned entering = trace_of_basis_change[i-1];
|
||||
unsigned leaving = trace_of_basis_change[i-2];
|
||||
cs.change_basis_unconditionally(entering, leaving);
|
||||
}
|
||||
cs.init_lu();
|
||||
}
|
||||
|
||||
//basis_heading is the basis heading of the solver owning trace_of_basis_change
|
||||
// here we compact the trace as we go to avoid unnecessary column changes
|
||||
template <typename L, typename K>
|
||||
void catch_up_in_lu(const vector<unsigned> & trace_of_basis_change, const vector<int> & basis_heading, lp_primal_core_solver<L,K> & cs) {
|
||||
if (cs.m_factorization == nullptr || cs.m_factorization->m_refactor_counter + trace_of_basis_change.size()/2 >= 200) {
|
||||
for (unsigned i = 0; i < trace_of_basis_change.size(); i+= 2) {
|
||||
unsigned entering = trace_of_basis_change[i];
|
||||
unsigned leaving = trace_of_basis_change[i+1];
|
||||
cs.change_basis_unconditionally(entering, leaving);
|
||||
}
|
||||
if (cs.m_factorization != nullptr) {
|
||||
delete cs.m_factorization;
|
||||
cs.m_factorization = nullptr;
|
||||
}
|
||||
} else {
|
||||
indexed_vector<L> w(cs.m_A.row_count());
|
||||
// the queues of delayed indices
|
||||
std::queue<unsigned> entr_q, leav_q;
|
||||
auto * l = cs.m_factorization;
|
||||
lp_assert(l->get_status() == LU_status::OK);
|
||||
for (unsigned i = 0; i < trace_of_basis_change.size(); i+= 2) {
|
||||
unsigned entering = trace_of_basis_change[i];
|
||||
unsigned leaving = trace_of_basis_change[i+1];
|
||||
bool good_e = basis_heading[entering] >= 0 && cs.m_basis_heading[entering] < 0;
|
||||
bool good_l = basis_heading[leaving] < 0 && cs.m_basis_heading[leaving] >= 0;
|
||||
if (!good_e && !good_l) continue;
|
||||
if (good_e && !good_l) {
|
||||
while (!leav_q.empty() && cs.m_basis_heading[leav_q.front()] < 0)
|
||||
leav_q.pop();
|
||||
if (!leav_q.empty()) {
|
||||
leaving = leav_q.front();
|
||||
leav_q.pop();
|
||||
} else {
|
||||
entr_q.push(entering);
|
||||
continue;
|
||||
}
|
||||
} else if (!good_e && good_l) {
|
||||
while (!entr_q.empty() && cs.m_basis_heading[entr_q.front()] >= 0)
|
||||
entr_q.pop();
|
||||
if (!entr_q.empty()) {
|
||||
entering = entr_q.front();
|
||||
entr_q.pop();
|
||||
} else {
|
||||
leav_q.push(leaving);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
lp_assert(cs.m_basis_heading[entering] < 0);
|
||||
lp_assert(cs.m_basis_heading[leaving] >= 0);
|
||||
if (l->get_status() == LU_status::OK) {
|
||||
l->prepare_entering(entering, w); // to init vector w
|
||||
l->replace_column(zero_of_type<L>(), w, cs.m_basis_heading[leaving]);
|
||||
}
|
||||
cs.change_basis_unconditionally(entering, leaving);
|
||||
}
|
||||
if (l->get_status() != LU_status::OK) {
|
||||
delete l;
|
||||
cs.m_factorization = nullptr;
|
||||
}
|
||||
}
|
||||
if (cs.m_factorization == nullptr) {
|
||||
if (numeric_traits<L>::precise())
|
||||
init_factorization(cs.m_factorization, cs.m_A, cs.m_basis, settings());
|
||||
}
|
||||
}
|
||||
|
||||
bool no_r_lu() const {
|
||||
return m_r_solver.m_factorization == nullptr || m_r_solver.m_factorization->get_status() == LU_status::Degenerated;
|
||||
}
|
||||
|
||||
void solve_on_signature_tableau(const lar_solution_signature & signature, const vector<unsigned> & changes_of_basis) {
|
||||
r_basis_is_OK();
|
||||
lp_assert(settings().use_tableau());
|
||||
bool r = catch_up_in_lu_tableau(changes_of_basis, m_d_solver.m_basis_heading);
|
||||
|
||||
if (!r) { // it is the case where m_d_solver gives a degenerated basis
|
||||
prepare_solver_x_with_signature_tableau(signature); // still are going to use the signature partially
|
||||
m_r_solver.find_feasible_solution();
|
||||
m_d_basis = m_r_basis;
|
||||
m_d_heading = m_r_heading;
|
||||
m_d_nbasis = m_r_nbasis;
|
||||
delete m_d_solver.m_factorization;
|
||||
m_d_solver.m_factorization = nullptr;
|
||||
} else {
|
||||
prepare_solver_x_with_signature_tableau(signature);
|
||||
m_r_solver.start_tracing_basis_changes();
|
||||
m_r_solver.find_feasible_solution();
|
||||
if (settings().get_cancel_flag())
|
||||
return;
|
||||
m_r_solver.stop_tracing_basis_changes();
|
||||
// and now catch up in the double solver
|
||||
lp_assert(m_r_solver.total_iterations() >= m_r_solver.m_trace_of_basis_change_vector.size() /2);
|
||||
catch_up_in_lu(m_r_solver.m_trace_of_basis_change_vector, m_r_solver.m_basis_heading, m_d_solver);
|
||||
}
|
||||
lp_assert(r_basis_is_OK());
|
||||
}
|
||||
|
||||
bool adjust_x_of_column(unsigned j) {
|
||||
/*
|
||||
if (m_r_solver.m_basis_heading[j] >= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_r_solver.column_is_feasible(j)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_r_solver.snap_column_to_bound_tableau(j);
|
||||
lp_assert(m_r_solver.column_is_feasible(j));
|
||||
m_r_solver.m_inf_set.erase(j);
|
||||
*/
|
||||
lp_assert(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool catch_up_in_lu_tableau(const vector<unsigned> & trace_of_basis_change, const vector<int> & basis_heading) {
|
||||
lp_assert(r_basis_is_OK());
|
||||
// the queues of delayed indices
|
||||
std::queue<unsigned> entr_q, leav_q;
|
||||
for (unsigned i = 0; i < trace_of_basis_change.size(); i+= 2) {
|
||||
unsigned entering = trace_of_basis_change[i];
|
||||
unsigned leaving = trace_of_basis_change[i+1];
|
||||
bool good_e = basis_heading[entering] >= 0 && m_r_solver.m_basis_heading[entering] < 0;
|
||||
bool good_l = basis_heading[leaving] < 0 && m_r_solver.m_basis_heading[leaving] >= 0;
|
||||
if (!good_e && !good_l) continue;
|
||||
if (good_e && !good_l) {
|
||||
while (!leav_q.empty() && m_r_solver.m_basis_heading[leav_q.front()] < 0)
|
||||
leav_q.pop();
|
||||
if (!leav_q.empty()) {
|
||||
leaving = leav_q.front();
|
||||
leav_q.pop();
|
||||
} else {
|
||||
entr_q.push(entering);
|
||||
continue;
|
||||
}
|
||||
} else if (!good_e && good_l) {
|
||||
while (!entr_q.empty() && m_r_solver.m_basis_heading[entr_q.front()] >= 0)
|
||||
entr_q.pop();
|
||||
if (!entr_q.empty()) {
|
||||
entering = entr_q.front();
|
||||
entr_q.pop();
|
||||
} else {
|
||||
leav_q.push(leaving);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
lp_assert(m_r_solver.m_basis_heading[entering] < 0);
|
||||
lp_assert(m_r_solver.m_basis_heading[leaving] >= 0);
|
||||
m_r_solver.change_basis_unconditionally(entering, leaving);
|
||||
if(!m_r_solver.pivot_column_tableau(entering, m_r_solver.m_basis_heading[entering])) {
|
||||
// unroll the last step
|
||||
m_r_solver.change_basis_unconditionally(leaving, entering);
|
||||
#ifdef Z3DEBUG
|
||||
bool t =
|
||||
#endif
|
||||
m_r_solver.pivot_column_tableau(leaving, m_r_solver.m_basis_heading[leaving]);
|
||||
#ifdef Z3DEBUG
|
||||
lp_assert(t);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
lp_assert(r_basis_is_OK());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool r_basis_is_OK() const {
|
||||
#ifdef Z3DEBUG
|
||||
if (!m_r_solver.m_settings.use_tableau())
|
||||
return true;
|
||||
|
||||
for (unsigned j : m_r_solver.m_basis) {
|
||||
lp_assert(m_r_solver.m_A.m_columns[j].size() == 1);
|
||||
}
|
||||
|
|
@ -565,139 +148,7 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
void solve_on_signature(const lar_solution_signature & signature, const vector<unsigned> & changes_of_basis) {
|
||||
SASSERT(!settings().use_tableau());
|
||||
if (m_r_solver.m_factorization == nullptr) {
|
||||
for (unsigned j = 0; j < changes_of_basis.size(); j+=2) {
|
||||
unsigned entering = changes_of_basis[j];
|
||||
unsigned leaving = changes_of_basis[j + 1];
|
||||
m_r_solver.change_basis_unconditionally(entering, leaving);
|
||||
}
|
||||
init_factorization(m_r_solver.m_factorization, m_r_A, m_r_basis, settings());
|
||||
} else {
|
||||
catch_up_in_lu(changes_of_basis, m_d_solver.m_basis_heading, m_r_solver);
|
||||
}
|
||||
|
||||
if (no_r_lu()) { // it is the case where m_d_solver gives a degenerated basis, we need to roll back
|
||||
catch_up_in_lu_in_reverse(changes_of_basis, m_r_solver);
|
||||
m_r_solver.find_feasible_solution();
|
||||
m_d_basis = m_r_basis;
|
||||
m_d_heading = m_r_heading;
|
||||
m_d_nbasis = m_r_nbasis;
|
||||
delete m_d_solver.m_factorization;
|
||||
m_d_solver.m_factorization = nullptr;
|
||||
} else {
|
||||
prepare_solver_x_with_signature(signature, m_r_solver);
|
||||
m_r_solver.start_tracing_basis_changes();
|
||||
m_r_solver.find_feasible_solution();
|
||||
if (settings().get_cancel_flag())
|
||||
return;
|
||||
m_r_solver.stop_tracing_basis_changes();
|
||||
// and now catch up in the double solver
|
||||
lp_assert(m_r_solver.total_iterations() >= m_r_solver.m_trace_of_basis_change_vector.size() /2);
|
||||
catch_up_in_lu(m_r_solver.m_trace_of_basis_change_vector, m_r_solver.m_basis_heading, m_d_solver);
|
||||
}
|
||||
}
|
||||
|
||||
void create_double_matrix(static_matrix<double, double> & A) {
|
||||
for (unsigned i = 0; i < m_r_A.row_count(); i++) {
|
||||
auto & row = m_r_A.m_rows[i];
|
||||
for (row_cell<mpq> & c : row) {
|
||||
A.add_new_element(i, c.var(), c.coeff().get_double());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fill_basis_d(
|
||||
vector<unsigned>& basis_d,
|
||||
vector<int>& heading_d,
|
||||
vector<unsigned>& nbasis_d){
|
||||
basis_d = m_r_basis;
|
||||
heading_d = m_r_heading;
|
||||
nbasis_d = m_r_nbasis;
|
||||
}
|
||||
|
||||
template <typename L, typename K>
|
||||
void extract_signature_from_lp_core_solver(const lp_primal_core_solver<L, K> & solver, lar_solution_signature & signature) {
|
||||
signature.clear();
|
||||
lp_assert(signature.size() == 0);
|
||||
for (unsigned j = 0; j < solver.m_basis_heading.size(); j++) {
|
||||
if (solver.m_basis_heading[j] < 0) {
|
||||
signature[j] = solver.get_non_basic_column_value_position(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void get_bounds_for_double_solver() {
|
||||
unsigned n = m_n();
|
||||
m_d_lower_bounds.resize(n);
|
||||
m_d_upper_bounds.resize(n);
|
||||
double delta = find_delta_for_strict_boxed_bounds().get_double();
|
||||
if (delta > 0.000001)
|
||||
delta = 0.000001;
|
||||
for (unsigned j = 0; j < n; j++) {
|
||||
if (lower_bound_is_set(j)) {
|
||||
const auto & lb = m_r_solver.m_lower_bounds[j];
|
||||
m_d_lower_bounds[j] = lb.x.get_double() + delta * lb.y.get_double();
|
||||
}
|
||||
if (upper_bound_is_set(j)) {
|
||||
const auto & ub = m_r_solver.m_upper_bounds[j];
|
||||
m_d_upper_bounds[j] = ub.x.get_double() + delta * ub.y.get_double();
|
||||
lp_assert(!lower_bound_is_set(j) || (m_d_upper_bounds[j] >= m_d_lower_bounds[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scale_problem_for_doubles(
|
||||
static_matrix<double, double>& A,
|
||||
vector<double> & lower_bounds,
|
||||
vector<double> & upper_bounds) {
|
||||
vector<double> column_scale_vector;
|
||||
vector<double> right_side_vector(A.column_count());
|
||||
settings().reps_in_scaler = 5;
|
||||
scaler<double, double > scaler(right_side_vector,
|
||||
A,
|
||||
settings().scaling_minimum,
|
||||
settings().scaling_maximum,
|
||||
column_scale_vector,
|
||||
settings());
|
||||
if (! scaler.scale()) {
|
||||
// the scale did not succeed, unscaling
|
||||
A.clear();
|
||||
create_double_matrix(A);
|
||||
} else {
|
||||
for (unsigned j = 0; j < A.column_count(); j++) {
|
||||
if (m_r_solver.column_has_upper_bound(j)) {
|
||||
upper_bounds[j] /= column_scale_vector[j];
|
||||
}
|
||||
if (m_r_solver.column_has_lower_bound(j)) {
|
||||
lower_bounds[j] /= column_scale_vector[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// returns the trace of basis changes
|
||||
vector<unsigned> find_solution_signature_with_doubles(lar_solution_signature & signature) {
|
||||
if (m_d_solver.m_factorization == nullptr || m_d_solver.m_factorization->get_status() != LU_status::OK) {
|
||||
vector<unsigned> ret;
|
||||
return ret;
|
||||
}
|
||||
get_bounds_for_double_solver();
|
||||
|
||||
extract_signature_from_lp_core_solver(m_r_solver, signature);
|
||||
prepare_solver_x_with_signature(signature, m_d_solver);
|
||||
m_d_solver.start_tracing_basis_changes();
|
||||
m_d_solver.find_feasible_solution();
|
||||
if (settings().get_cancel_flag())
|
||||
return vector<unsigned>();
|
||||
|
||||
m_d_solver.stop_tracing_basis_changes();
|
||||
extract_signature_from_lp_core_solver(m_d_solver, signature);
|
||||
return m_d_solver.m_trace_of_basis_change_vector;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool lower_bound_is_set(unsigned j) const {
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::free_column:
|
||||
|
|
@ -708,7 +159,7 @@ public:
|
|||
case column_type::fixed:
|
||||
return true;
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -723,7 +174,7 @@ public:
|
|||
case column_type::fixed:
|
||||
return true;
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -762,10 +213,6 @@ public:
|
|||
return delta;
|
||||
}
|
||||
|
||||
void init_column_row_nz_for_r_solver() {
|
||||
m_r_solver.init_column_row_non_zeroes();
|
||||
}
|
||||
|
||||
bool column_is_fixed(unsigned j) const {
|
||||
return m_column_types()[j] == column_type::fixed ||
|
||||
( m_column_types()[j] == column_type::boxed &&
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ Revision History:
|
|||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lar_core_solver.h"
|
||||
#include "math/lp/lar_solution_signature.h"
|
||||
namespace lp {
|
||||
lar_core_solver::lar_core_solver(
|
||||
lp_settings & settings,
|
||||
|
|
@ -31,78 +30,26 @@ lar_core_solver::lar_core_solver(
|
|||
m_r_lower_bounds(),
|
||||
m_r_upper_bounds(),
|
||||
settings,
|
||||
column_names),
|
||||
m_d_solver(m_d_A,
|
||||
m_d_right_sides_dummy,
|
||||
m_d_x,
|
||||
m_d_basis,
|
||||
m_d_nbasis,
|
||||
m_d_heading,
|
||||
m_d_costs_dummy,
|
||||
m_column_types(),
|
||||
m_d_lower_bounds,
|
||||
m_d_upper_bounds,
|
||||
settings,
|
||||
column_names) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lar_core_solver::calculate_pivot_row(unsigned i) {
|
||||
m_r_solver.calculate_pivot_row(i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lar_core_solver::prefix_r() {
|
||||
if (!m_r_solver.m_settings.use_tableau()) {
|
||||
m_r_solver.m_copy_of_xB.resize(m_r_solver.m_n());
|
||||
m_r_solver.m_ed.resize(m_r_solver.m_m());
|
||||
m_r_solver.m_pivot_row.resize(m_r_solver.m_n());
|
||||
m_r_solver.m_pivot_row_of_B_1.resize(m_r_solver.m_m());
|
||||
m_r_solver.m_w.resize(m_r_solver.m_m());
|
||||
m_r_solver.m_y.resize(m_r_solver.m_m());
|
||||
m_r_solver.m_rows_nz.resize(m_r_solver.m_m(), 0);
|
||||
m_r_solver.m_columns_nz.resize(m_r_solver.m_n(), 0);
|
||||
init_column_row_nz_for_r_solver();
|
||||
}
|
||||
|
||||
m_r_solver.m_b.resize(m_r_solver.m_m());
|
||||
|
||||
// m_r_solver.m_b.resize(m_r_solver.m_m());
|
||||
if (m_r_solver.m_settings.simplex_strategy() != simplex_strategy_enum::tableau_rows) {
|
||||
if(m_r_solver.m_settings.use_breakpoints_in_feasibility_search)
|
||||
m_r_solver.m_breakpoint_indices_queue.resize(m_r_solver.m_n());
|
||||
m_r_solver.m_costs.resize(m_r_solver.m_n());
|
||||
m_r_solver.m_d.resize(m_r_solver.m_n());
|
||||
m_r_solver.set_using_infeas_costs(true);
|
||||
}
|
||||
}
|
||||
|
||||
void lar_core_solver::prefix_d() {
|
||||
m_d_solver.m_b.resize(m_d_solver.m_m());
|
||||
m_d_solver.m_breakpoint_indices_queue.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_copy_of_xB.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_costs.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_d.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_ed.resize(m_d_solver.m_m());
|
||||
m_d_solver.m_pivot_row.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_pivot_row_of_B_1.resize(m_d_solver.m_m());
|
||||
m_d_solver.m_w.resize(m_d_solver.m_m());
|
||||
m_d_solver.m_y.resize(m_d_solver.m_m());
|
||||
m_d_solver.m_steepest_edge_coefficients.resize(m_d_solver.m_n());
|
||||
m_d_solver.m_column_norms.clear();
|
||||
m_d_solver.m_column_norms.resize(m_d_solver.m_n(), 2);
|
||||
m_d_solver.clear_inf_set();
|
||||
m_d_solver.resize_inf_set(m_d_solver.m_n());
|
||||
}
|
||||
|
||||
void lar_core_solver::fill_not_improvable_zero_sum_from_inf_row() {
|
||||
CASSERT("A_off", m_r_solver.A_mult_x_is_off() == false);
|
||||
unsigned bj = m_r_basis[m_r_solver.m_inf_row_index_for_tableau];
|
||||
m_infeasible_sum_sign = m_r_solver.inf_sign_of_column(bj);
|
||||
m_infeasible_linear_combination.clear();
|
||||
for (auto & rc : m_r_solver.m_A.m_rows[m_r_solver.m_inf_row_index_for_tableau]) {
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(rc.coeff(), rc.var()));
|
||||
}
|
||||
for (auto & rc : m_r_solver.m_A.m_rows[m_r_solver.m_inf_row_index_for_tableau])
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(rc.coeff(), rc.var()));
|
||||
}
|
||||
|
||||
void lar_core_solver::fill_not_improvable_zero_sum() {
|
||||
|
|
@ -111,30 +58,27 @@ void lar_core_solver::fill_not_improvable_zero_sum() {
|
|||
return;
|
||||
}
|
||||
// reusing the existing mechanism for row_feasibility_loop
|
||||
m_infeasible_sum_sign = m_r_solver.m_settings.use_breakpoints_in_feasibility_search? -1 : 1;
|
||||
m_infeasible_sum_sign = 1;
|
||||
m_infeasible_linear_combination.clear();
|
||||
for (auto j : m_r_solver.m_basis) {
|
||||
const mpq & cost_j = m_r_solver.m_costs[j];
|
||||
if (!numeric_traits<mpq>::is_zero(cost_j)) {
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(cost_j, j));
|
||||
}
|
||||
if (!numeric_traits<mpq>::is_zero(cost_j))
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(cost_j, j));
|
||||
}
|
||||
// m_costs are expressed by m_d ( additional costs), substructing the latter gives 0
|
||||
for (unsigned j = 0; j < m_r_solver.m_n(); j++) {
|
||||
if (m_r_solver.m_basis_heading[j] >= 0) continue;
|
||||
const mpq & d_j = m_r_solver.m_d[j];
|
||||
if (!numeric_traits<mpq>::is_zero(d_j)) {
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(-d_j, j));
|
||||
}
|
||||
if (!numeric_traits<mpq>::is_zero(d_j))
|
||||
m_infeasible_linear_combination.push_back(std::make_pair(-d_j, j));
|
||||
}
|
||||
}
|
||||
|
||||
unsigned lar_core_solver::get_number_of_non_ints() const {
|
||||
unsigned n = 0;
|
||||
for (auto & x : m_r_solver.m_x) {
|
||||
if (x.is_int() == false)
|
||||
n++;
|
||||
}
|
||||
for (auto & x : m_r_solver.m_x)
|
||||
if (!x.is_int())
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
@ -149,38 +93,16 @@ void lar_core_solver::solve() {
|
|||
return;
|
||||
}
|
||||
++settings().stats().m_need_to_solve_inf;
|
||||
CASSERT("A_off", !m_r_solver.A_mult_x_is_off());
|
||||
lp_assert((!settings().use_tableau()) || r_basis_is_OK());
|
||||
if (need_to_presolve_with_double_solver()) {
|
||||
TRACE("lar_solver", tout << "presolving\n";);
|
||||
prefix_d();
|
||||
lar_solution_signature solution_signature;
|
||||
vector<unsigned> changes_of_basis = find_solution_signature_with_doubles(solution_signature);
|
||||
if (m_d_solver.get_status() == lp_status::TIME_EXHAUSTED) {
|
||||
m_r_solver.set_status(lp_status::TIME_EXHAUSTED);
|
||||
return;
|
||||
}
|
||||
if (settings().use_tableau())
|
||||
solve_on_signature_tableau(solution_signature, changes_of_basis);
|
||||
else
|
||||
solve_on_signature(solution_signature, changes_of_basis);
|
||||
|
||||
lp_assert(!settings().use_tableau() || r_basis_is_OK());
|
||||
} else {
|
||||
if (!settings().use_tableau()) {
|
||||
TRACE("lar_solver", tout << "no tablau\n";);
|
||||
bool snapped = m_r_solver.snap_non_basic_x_to_bound();
|
||||
lp_assert(m_r_solver.non_basic_columns_are_set_correctly());
|
||||
if (snapped)
|
||||
m_r_solver.solve_Ax_eq_b();
|
||||
}
|
||||
if (m_r_solver.m_look_for_feasible_solution_only) //todo : should it be set?
|
||||
m_r_solver.find_feasible_solution();
|
||||
else {
|
||||
m_r_solver.solve();
|
||||
}
|
||||
lp_assert(!settings().use_tableau() || r_basis_is_OK());
|
||||
lp_assert( r_basis_is_OK());
|
||||
|
||||
|
||||
if (m_r_solver.m_look_for_feasible_solution_only) //todo : should it be set?
|
||||
m_r_solver.find_feasible_solution();
|
||||
else {
|
||||
m_r_solver.solve();
|
||||
}
|
||||
lp_assert(r_basis_is_OK());
|
||||
|
||||
switch (m_r_solver.get_status())
|
||||
{
|
||||
case lp_status::INFEASIBLE:
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "util/debug.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include <unordered_map>
|
||||
namespace lp {
|
||||
typedef std::unordered_map<unsigned, non_basic_column_value_position> lar_solution_signature;
|
||||
}
|
||||
|
|
@ -1,16 +1,13 @@
|
|||
#include "math/lp/lar_solver.h"
|
||||
#include "smt/params/smt_params_helper.hpp"
|
||||
|
||||
/*
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
Author: Nikolaj Bjorner, Lev Nachmanson
|
||||
*/
|
||||
|
||||
namespace lp {
|
||||
#include "math/lp/lar_solver.h"
|
||||
#include "smt/params/smt_params_helper.hpp"
|
||||
|
||||
////////////////// methods ////////////////////////////////
|
||||
static_matrix<double, double>& lar_solver::A_d() { return m_mpq_lar_core_solver.m_d_A; }
|
||||
static_matrix<double, double > const& lar_solver::A_d() const { return m_mpq_lar_core_solver.m_d_A; }
|
||||
|
||||
namespace lp {
|
||||
|
||||
lp_settings& lar_solver::settings() { return m_settings; }
|
||||
|
||||
|
|
@ -18,7 +15,6 @@ namespace lp {
|
|||
|
||||
statistics& lar_solver::stats() { return m_settings.stats(); }
|
||||
|
||||
|
||||
void lar_solver::updt_params(params_ref const& _p) {
|
||||
smt_params_helper p(_p);
|
||||
set_track_pivoted_rows(p.arith_bprop_on_pivoted_rows());
|
||||
|
|
@ -42,15 +38,12 @@ namespace lp {
|
|||
}
|
||||
|
||||
lar_solver::~lar_solver() {
|
||||
|
||||
for (auto t : m_terms)
|
||||
delete t;
|
||||
}
|
||||
|
||||
bool lar_solver::use_lu() const { return m_settings.simplex_strategy() == simplex_strategy_enum::lu; }
|
||||
|
||||
|
||||
bool lar_solver::sizes_are_correct() const {
|
||||
lp_assert(strategy_is_undecided() || !m_mpq_lar_core_solver.need_to_presolve_with_double_solver() || A_r().column_count() == A_d().column_count());
|
||||
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_solver.m_column_types.size());
|
||||
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_solver.m_costs.size());
|
||||
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_x.size());
|
||||
|
|
@ -142,10 +135,9 @@ namespace lp {
|
|||
|
||||
|
||||
bool lar_solver::row_has_a_big_num(unsigned i) const {
|
||||
for (const auto& c : A_r().m_rows[i]) {
|
||||
for (const auto& c : A_r().m_rows[i])
|
||||
if (c.coeff().is_big())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -199,9 +191,11 @@ namespace lp {
|
|||
stats().m_max_rows = A_r().row_count();
|
||||
if (strategy_is_undecided())
|
||||
decide_on_strategy_and_adjust_initial_state();
|
||||
|
||||
auto strategy_was = settings().simplex_strategy();
|
||||
settings().set_simplex_strategy(simplex_strategy_enum::tableau_rows);
|
||||
m_mpq_lar_core_solver.m_r_solver.m_look_for_feasible_solution_only = true;
|
||||
auto ret = solve();
|
||||
settings().set_simplex_strategy(strategy_was);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -229,9 +223,6 @@ namespace lp {
|
|||
evidence.add_pair(ul.lower_bound_witness(), -numeric_traits<mpq>::one());
|
||||
}
|
||||
|
||||
|
||||
unsigned lar_solver::get_total_iterations() const { return m_mpq_lar_core_solver.m_r_solver.total_iterations(); }
|
||||
|
||||
void lar_solver::push() {
|
||||
m_simplex_strategy = m_settings.simplex_strategy();
|
||||
m_simplex_strategy.push();
|
||||
|
|
@ -253,19 +244,14 @@ namespace lp {
|
|||
set.erase(j);
|
||||
}
|
||||
|
||||
void lar_solver::shrink_inf_set_after_pop(unsigned n, u_set& set) {
|
||||
clean_popped_elements(n, set);
|
||||
set.resize(n);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lar_solver::pop(unsigned k) {
|
||||
TRACE("lar_solver", tout << "k = " << k << std::endl;);
|
||||
m_crossed_bounds_column.pop(k);
|
||||
unsigned n = m_columns_to_ul_pairs.peek_size(k);
|
||||
m_var_register.shrink(n);
|
||||
if (m_settings.use_tableau())
|
||||
pop_tableau();
|
||||
pop_tableau();
|
||||
lp_assert(A_r().column_count() == n);
|
||||
TRACE("lar_solver_details",
|
||||
for (unsigned j = 0; j < n; j++) {
|
||||
|
|
@ -286,9 +272,9 @@ namespace lp {
|
|||
unsigned m = A_r().row_count();
|
||||
clean_popped_elements(m, m_rows_with_changed_bounds);
|
||||
clean_inf_set_of_r_solver_after_pop();
|
||||
lp_assert(m_settings.simplex_strategy() == simplex_strategy_enum::undecided ||
|
||||
(!use_tableau()) || m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
|
||||
lp_assert(
|
||||
m_settings.simplex_strategy() == simplex_strategy_enum::undecided ||
|
||||
m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
|
||||
m_constraints.pop(k);
|
||||
m_term_count.pop(k);
|
||||
|
|
@ -302,7 +288,7 @@ namespace lp {
|
|||
m_simplex_strategy.pop(k);
|
||||
m_settings.set_simplex_strategy(m_simplex_strategy);
|
||||
lp_assert(sizes_are_correct());
|
||||
lp_assert((!m_settings.use_tableau()) || m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
lp_assert(m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
m_usage_in_terms.pop(k);
|
||||
set_status(lp_status::UNKNOWN);
|
||||
}
|
||||
|
|
@ -371,7 +357,6 @@ namespace lp {
|
|||
m_basic_columns_with_changed_cost.resize(m_mpq_lar_core_solver.m_r_x.size());
|
||||
move_non_basic_columns_to_bounds(false);
|
||||
auto& rslv = m_mpq_lar_core_solver.m_r_solver;
|
||||
rslv.set_using_infeas_costs(false);
|
||||
lp_assert(costs_are_zeros_for_r_solver());
|
||||
lp_assert(reduced_costs_are_zeroes_for_r_solver());
|
||||
rslv.m_costs.resize(A_r().column_count(), zero_of_type<mpq>());
|
||||
|
|
@ -480,12 +465,9 @@ namespace lp {
|
|||
m_mpq_lar_core_solver.m_r_solver.set_status(lp_status::OPTIMAL);
|
||||
return ret;
|
||||
|
||||
case simplex_strategy_enum::lu:
|
||||
lp_assert(false); // not implemented
|
||||
return false;
|
||||
|
||||
|
||||
default:
|
||||
lp_unreachable(); // wrong mode
|
||||
UNREACHABLE(); // wrong mode
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -509,11 +491,11 @@ namespace lp {
|
|||
lp_status lar_solver::maximize_term(unsigned j_or_term,
|
||||
impq& term_max) {
|
||||
TRACE("lar_solver", print_values(tout););
|
||||
|
||||
lar_term term = get_term_to_maximize(j_or_term);
|
||||
if (term.is_empty()) {
|
||||
return lp_status::UNBOUNDED;
|
||||
}
|
||||
|
||||
impq prev_value;
|
||||
auto backup = m_mpq_lar_core_solver.m_r_x;
|
||||
if (m_mpq_lar_core_solver.m_r_solver.calc_current_x_is_feasible_include_non_basis()) {
|
||||
|
|
@ -582,7 +564,6 @@ namespace lp {
|
|||
|
||||
void lar_solver::pop_core_solver_params(unsigned k) {
|
||||
A_r().pop(k);
|
||||
A_d().pop(k);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -620,33 +601,20 @@ namespace lp {
|
|||
else {
|
||||
const lar_term& term = *m_terms[tv::unmask_term(t.second)];
|
||||
|
||||
for (auto p : term) {
|
||||
for (auto p : term)
|
||||
register_monoid_in_map(coeffs, t.first * p.coeff(), p.column());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& p : coeffs)
|
||||
if (!is_zero(p.second))
|
||||
left_side.push_back(std::make_pair(p.second, p.first));
|
||||
for (auto& [v, c] : coeffs)
|
||||
if (!is_zero(c))
|
||||
left_side.push_back(std::make_pair(c, v));
|
||||
}
|
||||
|
||||
void lar_solver::insert_row_with_changed_bounds(unsigned rid) {
|
||||
m_rows_with_changed_bounds.insert(rid);
|
||||
}
|
||||
|
||||
void lar_solver::detect_rows_of_bound_change_column_for_nbasic_column(unsigned j) {
|
||||
if (A_r().row_count() != m_column_buffer.data_size())
|
||||
m_column_buffer.resize(A_r().row_count());
|
||||
else
|
||||
m_column_buffer.clear();
|
||||
lp_assert(m_column_buffer.size() == 0 && m_column_buffer.is_OK());
|
||||
|
||||
m_mpq_lar_core_solver.m_r_solver.solve_Bd(j, m_column_buffer);
|
||||
for (unsigned i : m_column_buffer.m_index)
|
||||
insert_row_with_changed_bounds(i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lar_solver::detect_rows_of_bound_change_column_for_nbasic_column_tableau(unsigned j) {
|
||||
|
|
@ -654,24 +622,17 @@ namespace lp {
|
|||
insert_row_with_changed_bounds(rc.var());
|
||||
}
|
||||
|
||||
bool lar_solver::use_tableau() const { return m_settings.use_tableau(); }
|
||||
|
||||
|
||||
bool lar_solver::use_tableau_costs() const {
|
||||
return m_settings.simplex_strategy() == simplex_strategy_enum::tableau_costs;
|
||||
}
|
||||
|
||||
void lar_solver::adjust_x_of_column(unsigned j) {
|
||||
lp_assert(false);
|
||||
}
|
||||
|
||||
bool lar_solver::row_is_correct(unsigned i) const {
|
||||
numeric_pair<mpq> r = zero_of_type<numeric_pair<mpq>>();
|
||||
for (const auto& c : A_r().m_rows[i]) {
|
||||
r += c.coeff() * m_mpq_lar_core_solver.m_r_x[c.var()];
|
||||
}
|
||||
CTRACE("lar_solver", !is_zero(r), tout << "row = " << i << ", j = " << m_mpq_lar_core_solver.m_r_basis[i] << "\n";
|
||||
print_row(A_r().m_rows[i], tout); tout << " = " << r << "\n";
|
||||
);
|
||||
print_row(A_r().m_rows[i], tout); tout << " = " << r << "\n");
|
||||
return is_zero(r);
|
||||
}
|
||||
|
||||
|
|
@ -698,27 +659,15 @@ namespace lp {
|
|||
}
|
||||
|
||||
void lar_solver::change_basic_columns_dependend_on_a_given_nb_column(unsigned j, const numeric_pair<mpq>& delta) {
|
||||
if (use_tableau()) {
|
||||
for (const auto& c : A_r().m_columns[j]) {
|
||||
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
|
||||
if (tableau_with_costs()) {
|
||||
m_basic_columns_with_changed_cost.insert(bj);
|
||||
}
|
||||
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -A_r().get_val(c) * delta);
|
||||
TRACE("change_x_del",
|
||||
tout << "changed basis column " << bj << ", it is " <<
|
||||
(m_mpq_lar_core_solver.m_r_solver.column_is_feasible(bj) ? "feas" : "inf") << std::endl;);
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_column_buffer.clear();
|
||||
m_column_buffer.resize(A_r().row_count());
|
||||
m_mpq_lar_core_solver.m_r_solver.solve_Bd(j, m_column_buffer);
|
||||
for (unsigned i : m_column_buffer.m_index) {
|
||||
unsigned bj = m_mpq_lar_core_solver.m_r_basis[i];
|
||||
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -m_column_buffer[i] * delta);
|
||||
}
|
||||
for (const auto& c : A_r().m_columns[j]) {
|
||||
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
|
||||
if (tableau_with_costs()) {
|
||||
m_basic_columns_with_changed_cost.insert(bj);
|
||||
}
|
||||
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -A_r().get_val(c) * delta);
|
||||
TRACE("change_x_del",
|
||||
tout << "changed basis column " << bj << ", it is " <<
|
||||
(m_mpq_lar_core_solver.m_r_solver.column_is_feasible(bj) ? "feas" : "inf") << std::endl;);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -741,17 +690,11 @@ namespace lp {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void lar_solver::detect_rows_with_changed_bounds_for_column(unsigned j) {
|
||||
if (m_mpq_lar_core_solver.m_r_heading[j] >= 0) {
|
||||
if (m_mpq_lar_core_solver.m_r_heading[j] >= 0)
|
||||
insert_row_with_changed_bounds(m_mpq_lar_core_solver.m_r_heading[j]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (use_tableau())
|
||||
detect_rows_of_bound_change_column_for_nbasic_column_tableau(j);
|
||||
else
|
||||
detect_rows_of_bound_change_column_for_nbasic_column(j);
|
||||
else
|
||||
detect_rows_of_bound_change_column_for_nbasic_column_tableau(j);
|
||||
}
|
||||
|
||||
void lar_solver::detect_rows_with_changed_bounds() {
|
||||
|
|
@ -759,39 +702,18 @@ namespace lp {
|
|||
detect_rows_with_changed_bounds_for_column(j);
|
||||
}
|
||||
|
||||
void lar_solver::update_x_and_inf_costs_for_columns_with_changed_bounds() {
|
||||
for (auto j : m_columns_with_changed_bounds)
|
||||
update_x_and_inf_costs_for_column_with_changed_bounds(j);
|
||||
}
|
||||
|
||||
void lar_solver::update_x_and_inf_costs_for_columns_with_changed_bounds_tableau() {
|
||||
for (auto j : m_columns_with_changed_bounds)
|
||||
update_x_and_inf_costs_for_column_with_changed_bounds(j);
|
||||
|
||||
if (tableau_with_costs()) {
|
||||
if (m_mpq_lar_core_solver.m_r_solver.using_infeas_costs()) {
|
||||
for (unsigned j : m_basic_columns_with_changed_cost)
|
||||
m_mpq_lar_core_solver.m_r_solver.update_inf_cost_for_column_tableau(j);
|
||||
lp_assert(m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lar_solver::solve_with_core_solver() {
|
||||
if (!use_tableau())
|
||||
add_last_rows_to_lu(m_mpq_lar_core_solver.m_r_solver);
|
||||
if (m_mpq_lar_core_solver.need_to_presolve_with_double_solver()) {
|
||||
add_last_rows_to_lu(m_mpq_lar_core_solver.m_d_solver);
|
||||
}
|
||||
m_mpq_lar_core_solver.prefix_r();
|
||||
if (costs_are_used()) {
|
||||
m_basic_columns_with_changed_cost.resize(m_mpq_lar_core_solver.m_r_x.size());
|
||||
}
|
||||
if (use_tableau())
|
||||
update_x_and_inf_costs_for_columns_with_changed_bounds_tableau();
|
||||
else
|
||||
update_x_and_inf_costs_for_columns_with_changed_bounds();
|
||||
update_x_and_inf_costs_for_columns_with_changed_bounds_tableau();
|
||||
m_mpq_lar_core_solver.solve();
|
||||
set_status(m_mpq_lar_core_solver.m_r_solver.get_status());
|
||||
lp_assert(((stats().m_make_feasible% 100) != 0) || m_status != lp_status::OPTIMAL || all_constraints_hold());
|
||||
|
|
@ -823,44 +745,6 @@ namespace lp {
|
|||
return r;
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename L>
|
||||
void lar_solver::add_last_rows_to_lu(lp_primal_core_solver<K, L>& s) {
|
||||
auto& f = s.m_factorization;
|
||||
if (f != nullptr) {
|
||||
auto columns_to_replace = f->get_set_of_columns_to_replace_for_add_last_rows(s.m_basis_heading);
|
||||
if (f->m_refactor_counter + columns_to_replace.size() >= 200 || f->has_dense_submatrix()) {
|
||||
delete f;
|
||||
f = nullptr;
|
||||
}
|
||||
else {
|
||||
f->add_last_rows_to_B(s.m_basis_heading, columns_to_replace);
|
||||
}
|
||||
}
|
||||
if (f == nullptr) {
|
||||
init_factorization(f, s.m_A, s.m_basis, m_settings);
|
||||
if (f->get_status() != LU_status::OK) {
|
||||
delete f;
|
||||
f = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool lar_solver::x_is_correct() const {
|
||||
if (m_mpq_lar_core_solver.m_r_x.size() != A_r().column_count()) {
|
||||
return false;
|
||||
}
|
||||
for (unsigned i = 0; i < A_r().row_count(); i++) {
|
||||
numeric_pair<mpq> delta = A_r().dot_product_with_row(i, m_mpq_lar_core_solver.m_r_x);
|
||||
if (!delta.is_zero()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;;
|
||||
|
||||
}
|
||||
|
||||
bool lar_solver::var_is_registered(var_index vj) const {
|
||||
if (tv::is_term(vj)) {
|
||||
return tv::unmask_term(vj) < m_terms.size();
|
||||
|
|
@ -869,44 +753,6 @@ namespace lp {
|
|||
}
|
||||
|
||||
|
||||
void lar_solver::fill_last_row_of_A_r(static_matrix<mpq, numeric_pair<mpq>>& A, const lar_term* ls) {
|
||||
lp_assert(A.row_count() > 0);
|
||||
lp_assert(A.column_count() > 0);
|
||||
unsigned last_row = A.row_count() - 1;
|
||||
lp_assert(A.m_rows[last_row].size() == 0);
|
||||
for (auto t : *ls) {
|
||||
lp_assert(!is_zero(t.coeff()));
|
||||
var_index j = t.column();
|
||||
A.set(last_row, j, -t.coeff());
|
||||
}
|
||||
unsigned basis_j = A.column_count() - 1;
|
||||
A.set(last_row, basis_j, mpq(1));
|
||||
}
|
||||
|
||||
template <typename U, typename V>
|
||||
void lar_solver::create_matrix_A(static_matrix<U, V>& matr) {
|
||||
lp_assert(false); // not implemented
|
||||
/*
|
||||
unsigned m = number_or_nontrivial_left_sides();
|
||||
unsigned n = m_vec_of_canonic_left_sides.size();
|
||||
if (matr.row_count() == m && matr.column_count() == n)
|
||||
return;
|
||||
matr.init_empty_matrix(m, n);
|
||||
copy_from_mpq_matrix(matr);
|
||||
*/
|
||||
}
|
||||
|
||||
template <typename U, typename V>
|
||||
void lar_solver::copy_from_mpq_matrix(static_matrix<U, V>& matr) {
|
||||
matr.m_rows.resize(A_r().row_count());
|
||||
matr.m_columns.resize(A_r().column_count());
|
||||
for (unsigned i = 0; i < matr.row_count(); i++) {
|
||||
for (auto& it : A_r().m_rows[i]) {
|
||||
matr.set(i, it.var(), convert_struct<U, mpq>::convert(it.coeff()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool lar_solver::all_constrained_variables_are_registered(const vector<std::pair<mpq, var_index>>& left_side) {
|
||||
for (auto it : left_side) {
|
||||
if (!var_is_registered(it.second))
|
||||
|
|
@ -943,33 +789,11 @@ namespace lp {
|
|||
case GT: return left_side_val > constr.rhs();
|
||||
case EQ: return left_side_val == constr.rhs();
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
return false; // it is unreachable
|
||||
}
|
||||
|
||||
bool lar_solver::the_relations_are_of_same_type(const vector<std::pair<mpq, unsigned>>& evidence, lconstraint_kind& the_kind_of_sum) const {
|
||||
unsigned n_of_G = 0, n_of_L = 0;
|
||||
bool strict = false;
|
||||
for (auto& it : evidence) {
|
||||
mpq coeff = it.first;
|
||||
constraint_index con_ind = it.second;
|
||||
lconstraint_kind kind = coeff.is_pos() ?
|
||||
m_constraints[con_ind].kind() :
|
||||
flip_kind(m_constraints[con_ind].kind());
|
||||
if (kind == GT || kind == LT)
|
||||
strict = true;
|
||||
if (kind == GE || kind == GT)
|
||||
n_of_G++;
|
||||
else if (kind == LE || kind == LT)
|
||||
n_of_L++;
|
||||
}
|
||||
the_kind_of_sum = n_of_G ? GE : (n_of_L ? LE : EQ);
|
||||
if (strict)
|
||||
the_kind_of_sum = static_cast<lconstraint_kind>((static_cast<int>(the_kind_of_sum) / 2));
|
||||
|
||||
return n_of_G == 0 || n_of_L == 0;
|
||||
}
|
||||
|
||||
void lar_solver::register_in_map(std::unordered_map<var_index, mpq>& coeffs, const lar_base_constraint& cn, const mpq& a) {
|
||||
for (auto& it : cn.coeffs()) {
|
||||
|
|
@ -1020,7 +844,7 @@ namespace lp {
|
|||
case EQ: lp_assert(rs != zero_of_type<mpq>());
|
||||
break;
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1351,12 +1175,6 @@ namespace lp {
|
|||
insert_row_with_changed_bounds(r.var());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lar_solver::pivot_fixed_vars_from_basis() {
|
||||
m_mpq_lar_core_solver.m_r_solver.pivot_fixed_vars_from_basis();
|
||||
}
|
||||
|
||||
void lar_solver::pop() {
|
||||
pop(1);
|
||||
}
|
||||
|
|
@ -1406,7 +1224,6 @@ namespace lp {
|
|||
A_r().m_rows.pop_back();
|
||||
A_r().m_columns.pop_back();
|
||||
CASSERT("check_static_matrix", A_r().is_correct());
|
||||
slv.m_b.pop_back();
|
||||
}
|
||||
|
||||
void lar_solver::remove_last_column_from_A() {
|
||||
|
|
@ -1514,14 +1331,6 @@ namespace lp {
|
|||
for (unsigned j : became_feas)
|
||||
m_mpq_lar_core_solver.m_r_solver.remove_column_from_inf_set(j);
|
||||
|
||||
|
||||
if (use_tableau_costs()) {
|
||||
for (unsigned j : became_feas)
|
||||
m_mpq_lar_core_solver.m_r_solver.update_inf_cost_for_column_tableau(j);
|
||||
for (unsigned j : basic_columns_with_changed_cost)
|
||||
m_mpq_lar_core_solver.m_r_solver.update_inf_cost_for_column_tableau(j);
|
||||
lp_assert(m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
|
||||
}
|
||||
}
|
||||
|
||||
bool lar_solver::model_is_int_feasible() const {
|
||||
|
|
@ -1569,6 +1378,66 @@ namespace lp {
|
|||
return m_mpq_lar_core_solver.column_is_free(j);
|
||||
}
|
||||
|
||||
// column is at lower or upper bound, lower and upper bound are different.
|
||||
// the lower/upper bound is not strict.
|
||||
// the LP obtained by making the bound strict is infeasible
|
||||
// -> the column has to be fixed
|
||||
bool lar_solver::is_fixed_at_bound(column_index const& j) {
|
||||
if (column_is_fixed(j))
|
||||
return false;
|
||||
mpq val;
|
||||
if (!has_value(j, val))
|
||||
return false;
|
||||
lp::lconstraint_kind k;
|
||||
if (column_has_upper_bound(j) &&
|
||||
get_upper_bound(j).x == val) {
|
||||
verbose_stream() << "check upper " << j << "\n";
|
||||
push();
|
||||
if (column_is_int(j))
|
||||
k = LE, val -= 1;
|
||||
else
|
||||
k = LT;
|
||||
auto ci = mk_var_bound(j, k, val);
|
||||
update_column_type_and_bound(j, k, val, ci);
|
||||
auto st = find_feasible_solution();
|
||||
pop(1);
|
||||
return st == lp_status::INFEASIBLE;
|
||||
}
|
||||
if (column_has_lower_bound(j) &&
|
||||
get_lower_bound(j).x == val) {
|
||||
verbose_stream() << "check lower " << j << "\n";
|
||||
push();
|
||||
if (column_is_int(j))
|
||||
k = GE, val += 1;
|
||||
else
|
||||
k = GT;
|
||||
auto ci = mk_var_bound(j, k, val);
|
||||
update_column_type_and_bound(j, k, val, ci);
|
||||
auto st = find_feasible_solution();
|
||||
pop(1);
|
||||
return st == lp_status::INFEASIBLE;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool lar_solver::has_fixed_at_bound() {
|
||||
verbose_stream() << "has-fixed-at-bound\n";
|
||||
unsigned num_fixed = 0;
|
||||
for (unsigned j = 0; j < A_r().m_columns.size(); ++j) {
|
||||
auto ci = column_index(j);
|
||||
if (is_fixed_at_bound(ci)) {
|
||||
++num_fixed;
|
||||
verbose_stream() << "fixed " << j << "\n";
|
||||
}
|
||||
}
|
||||
verbose_stream() << "num fixed " << num_fixed << "\n";
|
||||
if (num_fixed > 0)
|
||||
find_feasible_solution();
|
||||
return num_fixed > 0;
|
||||
}
|
||||
|
||||
|
||||
// below is the initialization functionality of lar_solver
|
||||
|
||||
bool lar_solver::strategy_is_undecided() const {
|
||||
|
|
@ -1627,31 +1496,9 @@ namespace lp {
|
|||
register_new_ext_var_index(ext_j, is_int);
|
||||
m_mpq_lar_core_solver.m_column_types.push_back(column_type::free_column);
|
||||
increase_by_one_columns_with_changed_bounds();
|
||||
add_new_var_to_core_fields_for_mpq(false); // false for not adding a row
|
||||
if (use_lu())
|
||||
add_new_var_to_core_fields_for_doubles(false);
|
||||
add_new_var_to_core_fields_for_mpq(false); // false for not adding a row
|
||||
}
|
||||
|
||||
void lar_solver::add_new_var_to_core_fields_for_doubles(bool register_in_basis) {
|
||||
unsigned j = A_d().column_count();
|
||||
A_d().add_column();
|
||||
lp_assert(m_mpq_lar_core_solver.m_d_x.size() == j);
|
||||
// lp_assert(m_mpq_lar_core_solver.m_d_lower_bounds.size() == j && m_mpq_lar_core_solver.m_d_upper_bounds.size() == j); // restore later
|
||||
m_mpq_lar_core_solver.m_d_x.resize(j + 1);
|
||||
m_mpq_lar_core_solver.m_d_lower_bounds.resize(j + 1);
|
||||
m_mpq_lar_core_solver.m_d_upper_bounds.resize(j + 1);
|
||||
lp_assert(m_mpq_lar_core_solver.m_d_heading.size() == j); // as A().column_count() on the entry to the method
|
||||
if (register_in_basis) {
|
||||
A_d().add_row();
|
||||
m_mpq_lar_core_solver.m_d_heading.push_back(m_mpq_lar_core_solver.m_d_basis.size());
|
||||
m_mpq_lar_core_solver.m_d_basis.push_back(j);
|
||||
}
|
||||
else {
|
||||
m_mpq_lar_core_solver.m_d_heading.push_back(-static_cast<int>(m_mpq_lar_core_solver.m_d_nbasis.size()) - 1);
|
||||
m_mpq_lar_core_solver.m_d_nbasis.push_back(j);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lar_solver::add_new_var_to_core_fields_for_mpq(bool register_in_basis) {
|
||||
unsigned j = A_r().column_count();
|
||||
TRACE("add_var", tout << "j = " << j << std::endl;);
|
||||
|
|
@ -1687,24 +1534,21 @@ namespace lp {
|
|||
#if Z3DEBUG_CHECK_UNIQUE_TERMS
|
||||
bool lar_solver::term_coeffs_are_ok(const vector<std::pair<mpq, var_index>>& coeffs) {
|
||||
|
||||
for (const auto& p : coeffs) {
|
||||
for (const auto& p : coeffs)
|
||||
if (column_is_real(p.second))
|
||||
return true;
|
||||
}
|
||||
|
||||
mpq g;
|
||||
bool g_is_set = false;
|
||||
for (const auto& p : coeffs) {
|
||||
if (!p.first.is_int()) {
|
||||
if (!p.first.is_int())
|
||||
return false;
|
||||
}
|
||||
if (!g_is_set) {
|
||||
g_is_set = true;
|
||||
g = p.first;
|
||||
}
|
||||
else {
|
||||
else
|
||||
g = gcd(g, p.first);
|
||||
}
|
||||
}
|
||||
if (g == one_of_type<mpq>())
|
||||
return true;
|
||||
|
|
@ -1716,8 +1560,6 @@ namespace lp {
|
|||
m_terms.push_back(t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// terms
|
||||
bool lar_solver::all_vars_are_registered(const vector<std::pair<mpq, var_index>>& coeffs) {
|
||||
for (const auto& p : coeffs) {
|
||||
|
|
@ -1732,20 +1574,17 @@ namespace lp {
|
|||
std::set<unsigned> seen_terms;
|
||||
for (auto p : *t) {
|
||||
auto j = p.column();
|
||||
if (this->column_corresponds_to_term(j)) {
|
||||
if (this->column_corresponds_to_term(j))
|
||||
seen_terms.insert(j);
|
||||
}
|
||||
}
|
||||
while (!seen_terms.empty()) {
|
||||
unsigned j = *seen_terms.begin();
|
||||
seen_terms.erase(j);
|
||||
auto tj = this->m_var_register.local_to_external(j);
|
||||
auto& ot = this->get_term(tj);
|
||||
for (auto p : ot){
|
||||
if (this->column_corresponds_to_term(p.column())) {
|
||||
for (auto p : ot)
|
||||
if (this->column_corresponds_to_term(p.column()))
|
||||
seen_terms.insert(p.column());
|
||||
}
|
||||
}
|
||||
t->subst_by_term(ot, j);
|
||||
}
|
||||
}
|
||||
|
|
@ -1763,15 +1602,14 @@ namespace lp {
|
|||
SASSERT(m_terms.size() == m_term_register.size());
|
||||
unsigned adjusted_term_index = m_terms.size() - 1;
|
||||
var_index ret = tv::mask_term(adjusted_term_index);
|
||||
if (use_tableau() && !coeffs.empty()) {
|
||||
if (!coeffs.empty()) {
|
||||
add_row_from_term_no_constraint(m_terms.back(), ret);
|
||||
if (m_settings.bound_propagation())
|
||||
insert_row_with_changed_bounds(A_r().row_count() - 1);
|
||||
}
|
||||
lp_assert(m_var_register.size() == A_r().column_count());
|
||||
if (m_need_register_terms) {
|
||||
if (m_need_register_terms)
|
||||
register_normalized_term(*t, A_r().column_count() - 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -1784,44 +1622,32 @@ namespace lp {
|
|||
ul_pair ul(true); // to mark this column as associated_with_row
|
||||
m_columns_to_ul_pairs.push_back(ul);
|
||||
add_basic_var_to_core_fields();
|
||||
if (use_tableau()) {
|
||||
A_r().fill_last_row_with_pivoting(*term,
|
||||
|
||||
A_r().fill_last_row_with_pivoting(*term,
|
||||
j,
|
||||
m_mpq_lar_core_solver.m_r_solver.m_basis_heading);
|
||||
m_mpq_lar_core_solver.m_r_solver.m_b.resize(A_r().column_count(), zero_of_type<mpq>());
|
||||
}
|
||||
else {
|
||||
fill_last_row_of_A_r(A_r(), term);
|
||||
}
|
||||
m_mpq_lar_core_solver.m_r_solver.m_basis_heading);
|
||||
|
||||
|
||||
m_mpq_lar_core_solver.m_r_solver.update_x(j, get_basic_var_value_from_row(A_r().row_count() - 1));
|
||||
if (use_lu())
|
||||
fill_last_row_of_A_d(A_d(), term);
|
||||
for (lar_term::ival c : *term) {
|
||||
unsigned j = c.column();
|
||||
while (m_usage_in_terms.size() <= j) {
|
||||
while (m_usage_in_terms.size() <= j)
|
||||
m_usage_in_terms.push_back(0);
|
||||
}
|
||||
m_usage_in_terms[j] = m_usage_in_terms[j] + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void lar_solver::add_basic_var_to_core_fields() {
|
||||
bool use_lu = m_mpq_lar_core_solver.need_to_presolve_with_double_solver();
|
||||
lp_assert(!use_lu || A_r().column_count() == A_d().column_count());
|
||||
m_mpq_lar_core_solver.m_column_types.push_back(column_type::free_column);
|
||||
increase_by_one_columns_with_changed_bounds();
|
||||
m_incorrect_columns.increase_size_by_one();
|
||||
m_rows_with_changed_bounds.increase_size_by_one();
|
||||
add_new_var_to_core_fields_for_mpq(true);
|
||||
if (use_lu)
|
||||
add_new_var_to_core_fields_for_doubles(true);
|
||||
|
||||
}
|
||||
|
||||
bool lar_solver::bound_is_integer_for_integer_column(unsigned j, const mpq& right_side) const {
|
||||
if (!column_is_int(j))
|
||||
return true;
|
||||
return right_side.is_int();
|
||||
return !column_is_int(j) || right_side.is_int();
|
||||
}
|
||||
|
||||
constraint_index lar_solver::add_var_bound_check_on_equal(var_index j, lconstraint_kind kind, const mpq& right_side, var_index& equal_var) {
|
||||
|
|
@ -2005,59 +1831,26 @@ namespace lp {
|
|||
|
||||
void lar_solver::decide_on_strategy_and_adjust_initial_state() {
|
||||
lp_assert(strategy_is_undecided());
|
||||
if (m_columns_to_ul_pairs.size() > m_settings.column_number_threshold_for_using_lu_in_lar_solver) {
|
||||
m_settings.set_simplex_strategy(simplex_strategy_enum::lu);
|
||||
}
|
||||
else {
|
||||
m_settings.set_simplex_strategy(simplex_strategy_enum::tableau_rows); // todo: when to switch to tableau_costs?
|
||||
}
|
||||
|
||||
m_settings.set_simplex_strategy(simplex_strategy_enum::tableau_rows); // todo: when to switch to tableau_costs?
|
||||
|
||||
adjust_initial_state();
|
||||
}
|
||||
|
||||
void lar_solver::adjust_initial_state() {
|
||||
switch (m_settings.simplex_strategy()) {
|
||||
case simplex_strategy_enum::lu:
|
||||
adjust_initial_state_for_lu();
|
||||
break;
|
||||
case simplex_strategy_enum::tableau_rows:
|
||||
adjust_initial_state_for_tableau_rows();
|
||||
break;
|
||||
case simplex_strategy_enum::tableau_costs:
|
||||
lp_assert(false); // not implemented
|
||||
UNREACHABLE(); // not implemented
|
||||
case simplex_strategy_enum::undecided:
|
||||
adjust_initial_state_for_tableau_rows();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lar_solver::adjust_initial_state_for_lu() {
|
||||
copy_from_mpq_matrix(A_d());
|
||||
unsigned n = A_d().column_count();
|
||||
m_mpq_lar_core_solver.m_d_x.resize(n);
|
||||
m_mpq_lar_core_solver.m_d_lower_bounds.resize(n);
|
||||
m_mpq_lar_core_solver.m_d_upper_bounds.resize(n);
|
||||
m_mpq_lar_core_solver.m_d_heading = m_mpq_lar_core_solver.m_r_heading;
|
||||
m_mpq_lar_core_solver.m_d_basis = m_mpq_lar_core_solver.m_r_basis;
|
||||
|
||||
/*
|
||||
unsigned j = A_d().column_count();
|
||||
A_d().add_column();
|
||||
lp_assert(m_mpq_lar_core_solver.m_d_x.size() == j);
|
||||
// lp_assert(m_mpq_lar_core_solver.m_d_lower_bounds.size() == j && m_mpq_lar_core_solver.m_d_upper_bounds.size() == j); // restore later
|
||||
m_mpq_lar_core_solver.m_d_x.resize(j + 1 );
|
||||
m_mpq_lar_core_solver.m_d_lower_bounds.resize(j + 1);
|
||||
m_mpq_lar_core_solver.m_d_upper_bounds.resize(j + 1);
|
||||
lp_assert(m_mpq_lar_core_solver.m_d_heading.size() == j); // as A().column_count() on the entry to the method
|
||||
if (register_in_basis) {
|
||||
A_d().add_row();
|
||||
m_mpq_lar_core_solver.m_d_heading.push_back(m_mpq_lar_core_solver.m_d_basis.size());
|
||||
m_mpq_lar_core_solver.m_d_basis.push_back(j);
|
||||
}else {
|
||||
m_mpq_lar_core_solver.m_d_heading.push_back(- static_cast<int>(m_mpq_lar_core_solver.m_d_nbasis.size()) - 1);
|
||||
m_mpq_lar_core_solver.m_d_nbasis.push_back(j);
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
void lar_solver::adjust_initial_state_for_tableau_rows() {
|
||||
for (unsigned i = 0; i < m_terms.size(); i++) {
|
||||
if (m_var_register.external_is_used(tv::mask_term(i)))
|
||||
|
|
@ -2066,24 +1859,7 @@ namespace lp {
|
|||
}
|
||||
}
|
||||
|
||||
// this fills the last row of A_d and sets the basis column: -1 in the last column of the row
|
||||
void lar_solver::fill_last_row_of_A_d(static_matrix<double, double>& A, const lar_term* ls) {
|
||||
lp_assert(A.row_count() > 0);
|
||||
lp_assert(A.column_count() > 0);
|
||||
unsigned last_row = A.row_count() - 1;
|
||||
lp_assert(A.m_rows[last_row].empty());
|
||||
|
||||
for (auto t : *ls) {
|
||||
lp_assert(!is_zero(t.coeff()));
|
||||
var_index j = t.column();
|
||||
A.set(last_row, j, -t.coeff().get_double());
|
||||
}
|
||||
|
||||
unsigned basis_j = A.column_count() - 1;
|
||||
A.set(last_row, basis_j, -1);
|
||||
lp_assert(A.is_correct());
|
||||
}
|
||||
|
||||
|
||||
void lar_solver::update_column_type_and_bound_with_ub(unsigned j, lp::lconstraint_kind kind, const mpq& right_side, unsigned constraint_index) {
|
||||
SASSERT(column_has_upper_bound(j));
|
||||
if (column_has_lower_bound(j)) {
|
||||
|
|
@ -2156,7 +1932,7 @@ namespace lp {
|
|||
}
|
||||
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
if (m_mpq_lar_core_solver.m_r_upper_bounds[j] == m_mpq_lar_core_solver.m_r_lower_bounds[j]) {
|
||||
m_mpq_lar_core_solver.m_column_types[j] = column_type::fixed;
|
||||
|
|
@ -2210,7 +1986,7 @@ namespace lp {
|
|||
}
|
||||
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2260,7 +2036,7 @@ namespace lp {
|
|||
}
|
||||
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
void lar_solver::update_bound_with_no_ub_no_lb(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index ci) {
|
||||
|
|
@ -2301,7 +2077,7 @@ namespace lp {
|
|||
}
|
||||
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2536,10 +2312,6 @@ namespace lp {
|
|||
return true;
|
||||
}
|
||||
|
||||
void lar_solver::pivot_column_tableau(unsigned j, unsigned row_index) {
|
||||
m_mpq_lar_core_solver.m_r_solver.pivot_column_tableau(j, row_index);
|
||||
m_mpq_lar_core_solver.m_r_solver.change_basis(j, r_basis()[row_index]);
|
||||
}
|
||||
} // namespace lp
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -31,14 +31,12 @@
|
|||
#include "math/lp/lar_constraints.h"
|
||||
#include "math/lp/lar_core_solver.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/scaler.h"
|
||||
#include "math/lp/lp_primal_core_solver.h"
|
||||
#include "math/lp/random_updater.h"
|
||||
#include "util/stacked_value.h"
|
||||
#include "math/lp/stacked_vector.h"
|
||||
#include "math/lp/implied_bound.h"
|
||||
#include "math/lp/bound_analyzer_on_row.h"
|
||||
#include "math/lp/conversion_helper.h"
|
||||
#include "math/lp/int_solver.h"
|
||||
#include "math/lp/nra_solver.h"
|
||||
#include "math/lp/lp_types.h"
|
||||
|
|
@ -113,8 +111,6 @@ class lar_solver : public column_namer {
|
|||
// end of fields
|
||||
|
||||
////////////////// methods ////////////////////////////////
|
||||
static_matrix<double, double> & A_d();
|
||||
static_matrix<double, double > const & A_d() const;
|
||||
|
||||
static bool valid_index(unsigned j) { return static_cast<int>(j) >= 0;}
|
||||
const lar_term & get_term(unsigned j) const;
|
||||
|
|
@ -125,7 +121,6 @@ class lar_solver : public column_namer {
|
|||
bool term_is_int(const lar_term * t) const;
|
||||
bool term_is_int(const vector<std::pair<mpq, unsigned int>> & coeffs) const;
|
||||
void add_non_basic_var_to_core_fields(unsigned ext_j, bool is_int);
|
||||
void add_new_var_to_core_fields_for_doubles(bool register_in_basis);
|
||||
void add_new_var_to_core_fields_for_mpq(bool register_in_basis);
|
||||
mpq adjust_bound_for_int(lpvar j, lconstraint_kind&, const mpq&);
|
||||
|
||||
|
|
@ -134,7 +129,6 @@ class lar_solver : public column_namer {
|
|||
var_index add_term_undecided(const vector<std::pair<mpq, var_index>> & coeffs);
|
||||
bool term_coeffs_are_ok(const vector<std::pair<mpq, var_index>> & coeffs);
|
||||
void push_term(lar_term* t);
|
||||
void add_row_for_term(const lar_term * term, unsigned term_ext_index);
|
||||
void add_row_from_term_no_constraint(const lar_term * term, unsigned term_ext_index);
|
||||
void add_basic_var_to_core_fields();
|
||||
bool compare_values(impq const& lhs, lconstraint_kind k, const mpq & rhs);
|
||||
|
|
@ -163,39 +157,28 @@ class lar_solver : public column_namer {
|
|||
unsigned row_of_basic_column(unsigned) const;
|
||||
void decide_on_strategy_and_adjust_initial_state();
|
||||
void adjust_initial_state();
|
||||
void adjust_initial_state_for_lu();
|
||||
void adjust_initial_state_for_tableau_rows();
|
||||
void fill_last_row_of_A_d(static_matrix<double, double> & A, const lar_term* ls);
|
||||
bool use_lu() const;
|
||||
bool sizes_are_correct() const;
|
||||
bool implied_bound_is_correctly_explained(implied_bound const & be, const vector<std::pair<mpq, unsigned>> & explanation) const;
|
||||
|
||||
template <typename T>
|
||||
void analyze_new_bounds_on_row_tableau(
|
||||
unsigned row_index,
|
||||
lp_bound_propagator<T> & bp ) {
|
||||
|
||||
if (A_r().m_rows[row_index].size() > settings().max_row_length_for_bound_propagation
|
||||
|| row_has_a_big_num(row_index))
|
||||
return;
|
||||
lp_assert(use_tableau());
|
||||
|
||||
bound_analyzer_on_row<row_strip<mpq>, lp_bound_propagator<T>>::analyze_row(A_r().m_rows[row_index],
|
||||
null_ci,
|
||||
zero_of_type<numeric_pair<mpq>>(),
|
||||
row_index,
|
||||
bp
|
||||
);
|
||||
}
|
||||
|
||||
void substitute_basis_var_in_terms_for_row(unsigned i);
|
||||
|
||||
template <typename T>
|
||||
void calculate_implied_bounds_for_row(unsigned i, lp_bound_propagator<T> & bp) {
|
||||
SASSERT(use_tableau());
|
||||
analyze_new_bounds_on_row_tableau(i, bp);
|
||||
unsigned calculate_implied_bounds_for_row(unsigned row_index, lp_bound_propagator<T> & bp) {
|
||||
|
||||
if (A_r().m_rows[row_index].size() > settings().max_row_length_for_bound_propagation || row_has_a_big_num(row_index))
|
||||
return 0;
|
||||
|
||||
return bound_analyzer_on_row<row_strip<mpq>, lp_bound_propagator<T>>::analyze_row(
|
||||
A_r().m_rows[row_index],
|
||||
null_ci,
|
||||
zero_of_type<numeric_pair<mpq>>(),
|
||||
row_index,
|
||||
bp);
|
||||
}
|
||||
|
||||
static void clean_popped_elements(unsigned n, u_set& set);
|
||||
static void shrink_inf_set_after_pop(unsigned n, u_set & set);
|
||||
bool maximize_term_on_tableau(const lar_term & term,
|
||||
impq &term_max);
|
||||
bool costs_are_zeros_for_r_solver() const;
|
||||
|
|
@ -209,12 +192,9 @@ class lar_solver : public column_namer {
|
|||
void set_lower_bound_witness(var_index j, constraint_index ci);
|
||||
void substitute_terms_in_linear_expression( const vector<std::pair<mpq, var_index>>& left_side_with_terms,
|
||||
vector<std::pair<mpq, var_index>> &left_side) const;
|
||||
void detect_rows_of_bound_change_column_for_nbasic_column(unsigned j);
|
||||
|
||||
void detect_rows_of_bound_change_column_for_nbasic_column_tableau(unsigned j);
|
||||
bool use_tableau() const;
|
||||
bool use_tableau_costs() const;
|
||||
void detect_rows_of_column_with_bound_change(unsigned j);
|
||||
void adjust_x_of_column(unsigned j);
|
||||
bool tableau_with_costs() const;
|
||||
bool costs_are_used() const;
|
||||
void change_basic_columns_dependend_on_a_given_nb_column(unsigned j, const numeric_pair<mpq> & delta);
|
||||
|
|
@ -224,27 +204,15 @@ class lar_solver : public column_namer {
|
|||
void detect_rows_with_changed_bounds_for_column(unsigned j);
|
||||
void detect_rows_with_changed_bounds();
|
||||
|
||||
void update_x_and_inf_costs_for_columns_with_changed_bounds();
|
||||
void update_x_and_inf_costs_for_columns_with_changed_bounds_tableau();
|
||||
void solve_with_core_solver();
|
||||
numeric_pair<mpq> get_basic_var_value_from_row(unsigned i);
|
||||
template <typename K, typename L>
|
||||
void add_last_rows_to_lu(lp_primal_core_solver<K,L> & s);
|
||||
bool x_is_correct() const;
|
||||
void fill_last_row_of_A_r(static_matrix<mpq, numeric_pair<mpq>> & A, const lar_term * ls);
|
||||
template <typename U, typename V>
|
||||
void create_matrix_A(static_matrix<U, V> & matr);
|
||||
template <typename U, typename V>
|
||||
void copy_from_mpq_matrix(static_matrix<U, V> & matr);
|
||||
bool try_to_set_fixed(column_info<mpq> & ci);
|
||||
bool all_constrained_variables_are_registered(const vector<std::pair<mpq, var_index>>& left_side);
|
||||
bool all_constraints_hold() const;
|
||||
bool constraint_holds(const lar_base_constraint & constr, std::unordered_map<var_index, mpq> & var_map) const;
|
||||
bool the_relations_are_of_same_type(const vector<std::pair<mpq, unsigned>> & evidence, lconstraint_kind & the_kind_of_sum) const;
|
||||
static void register_in_map(std::unordered_map<var_index, mpq> & coeffs, const lar_base_constraint & cn, const mpq & a);
|
||||
static void register_monoid_in_map(std::unordered_map<var_index, mpq> & coeffs, const mpq & a, unsigned j);
|
||||
bool the_left_sides_sum_to_zero(const vector<std::pair<mpq, unsigned>> & evidence) const;
|
||||
bool the_right_sides_do_not_sum_to_zero(const vector<std::pair<mpq, unsigned>> & evidence);
|
||||
bool explanation_is_correct(explanation&) const;
|
||||
bool inf_explanation_is_correct() const;
|
||||
mpq sum_of_right_sides_of_explanation(explanation &) const;
|
||||
|
|
@ -254,7 +222,6 @@ class lar_solver : public column_namer {
|
|||
int inf_sign) const;
|
||||
mpq get_left_side_val(const lar_base_constraint & cns, const std::unordered_map<var_index, mpq> & var_map) const;
|
||||
void fill_var_set_for_random_update(unsigned sz, var_index const * vars, vector<unsigned>& column_list);
|
||||
void pivot_fixed_vars_from_basis();
|
||||
bool column_represents_row_in_tableau(unsigned j);
|
||||
void make_sure_that_the_bottom_right_elem_not_zero_in_tableau(unsigned i, unsigned j);
|
||||
void remove_last_row_and_column_from_tableau(unsigned j);
|
||||
|
|
@ -264,27 +231,16 @@ class lar_solver : public column_namer {
|
|||
void remove_last_column_from_tableau();
|
||||
void pop_tableau();
|
||||
void clean_inf_set_of_r_solver_after_pop();
|
||||
void shrink_explanation_to_minimum(vector<std::pair<mpq, constraint_index>> & explanation) const;
|
||||
inline bool column_value_is_integer(unsigned j) const { return get_column_value(j).is_int(); }
|
||||
bool model_is_int_feasible() const;
|
||||
inline
|
||||
indexed_vector<mpq> & get_column_in_lu_mode(unsigned j) {
|
||||
m_column_buffer.clear();
|
||||
m_column_buffer.resize(A_r().row_count());
|
||||
m_mpq_lar_core_solver.m_r_solver.solve_Bd(j, m_column_buffer);
|
||||
return m_column_buffer;
|
||||
}
|
||||
|
||||
bool bound_is_integer_for_integer_column(unsigned j, const mpq & right_side) const;
|
||||
inline lar_core_solver & get_core_solver() { return m_mpq_lar_core_solver; }
|
||||
void catch_up_in_updating_int_solver();
|
||||
var_index to_column(unsigned ext_j) const;
|
||||
void fix_terms_with_rounded_columns();
|
||||
void update_delta_for_terms(const impq & delta, unsigned j, const vector<unsigned>&);
|
||||
void fill_vars_to_terms(vector<vector<unsigned>> & vars_to_terms);
|
||||
bool remove_from_basis(unsigned);
|
||||
lar_term get_term_to_maximize(unsigned ext_j) const;
|
||||
bool sum_first_coords(const lar_term& t, mpq & val) const;
|
||||
void collect_rounded_rows_to_fix();
|
||||
void register_normalized_term(const lar_term&, lpvar);
|
||||
void deregister_normalized_term(const lar_term&);
|
||||
|
||||
|
|
@ -300,10 +256,7 @@ public:
|
|||
return m_fixed_var_table_int;
|
||||
}
|
||||
|
||||
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>>& fixed_var_table_int() {
|
||||
return m_fixed_var_table_int;
|
||||
}
|
||||
|
||||
|
||||
const map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>>& fixed_var_table_real() const {
|
||||
return m_fixed_var_table_real;
|
||||
}
|
||||
|
|
@ -329,9 +282,7 @@ public:
|
|||
inline void set_column_value_test(unsigned j, const impq& v) {
|
||||
set_column_value(j, v);
|
||||
}
|
||||
|
||||
unsigned get_total_iterations() const;
|
||||
|
||||
|
||||
var_index add_named_var(unsigned ext_j, bool is_integer, const std::string&);
|
||||
|
||||
lp_status maximize_term(unsigned j_or_term, impq &term_max);
|
||||
|
|
@ -383,9 +334,9 @@ public:
|
|||
void mark_rows_for_bound_prop(lpvar j);
|
||||
template <typename T>
|
||||
void propagate_bounds_for_touched_rows(lp_bound_propagator<T> & bp) {
|
||||
SASSERT(use_tableau());
|
||||
unsigned num_prop = 0;
|
||||
for (unsigned i : m_rows_with_changed_bounds) {
|
||||
calculate_implied_bounds_for_row(i, bp);
|
||||
num_prop += calculate_implied_bounds_for_row(i, bp);
|
||||
if (settings().get_cancel_flag())
|
||||
return;
|
||||
}
|
||||
|
|
@ -405,8 +356,20 @@ public:
|
|||
}
|
||||
m_rows_with_changed_bounds.clear();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void check_missed_propagations(lp_bound_propagator<T> & bp) {
|
||||
for (unsigned i = 0; i < A_r().row_count(); i++)
|
||||
if (!m_rows_with_changed_bounds.contains(i))
|
||||
if (0 < calculate_implied_bounds_for_row(i, bp)) {
|
||||
verbose_stream() << i << ": " << get_row(i) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
bool is_fixed_at_bound(column_index const& j);
|
||||
bool has_fixed_at_bound();
|
||||
|
||||
bool is_fixed(column_index const& j) const { return column_is_fixed(j); }
|
||||
bool is_fixed(column_index const& j) const { return column_is_fixed(j); }
|
||||
inline column_index to_column_index(unsigned v) const { return column_index(external_to_column_index(v)); }
|
||||
bool external_is_used(unsigned) const;
|
||||
void pop(unsigned k);
|
||||
|
|
@ -436,8 +399,8 @@ public:
|
|||
void change_basic_columns_dependend_on_a_given_nb_column_report(unsigned j,
|
||||
const numeric_pair<mpq> & delta,
|
||||
const ChangeReport& after) {
|
||||
if (use_tableau()) {
|
||||
for (const auto & c : A_r().m_columns[j]) {
|
||||
|
||||
for (const auto & c : A_r().m_columns[j]) {
|
||||
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
|
||||
if (tableau_with_costs()) {
|
||||
m_basic_columns_with_changed_cost.insert(bj);
|
||||
|
|
@ -447,20 +410,8 @@ public:
|
|||
TRACE("change_x_del",
|
||||
tout << "changed basis column " << bj << ", it is " <<
|
||||
( m_mpq_lar_core_solver.m_r_solver.column_is_feasible(bj)? "feas":"inf") << std::endl;);
|
||||
|
||||
|
||||
}
|
||||
} else {
|
||||
NOT_IMPLEMENTED_YET();
|
||||
m_column_buffer.clear();
|
||||
m_column_buffer.resize(A_r().row_count());
|
||||
m_mpq_lar_core_solver.m_r_solver.solve_Bd(j, m_column_buffer);
|
||||
for (unsigned i : m_column_buffer.m_index) {
|
||||
unsigned bj = m_mpq_lar_core_solver.m_r_basis[i];
|
||||
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -m_column_buffer[i] * delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ChangeReport>
|
||||
void set_value_for_nbasic_column_report(unsigned j,
|
||||
|
|
@ -567,8 +518,6 @@ public:
|
|||
return m_mpq_lar_core_solver.lower_bound(j);
|
||||
}
|
||||
|
||||
void pivot_column_tableau(unsigned j, unsigned row_index);
|
||||
|
||||
inline const impq & column_upper_bound(unsigned j) const {
|
||||
return m_mpq_lar_core_solver.upper_bound(j);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ public:
|
|||
return p.coeff().is_one();
|
||||
}
|
||||
}
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ inline std::string lia_move_to_string(lia_move m) {
|
|||
case lia_move::unsat:
|
||||
return "unsat";
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
};
|
||||
return "strange";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -578,8 +578,12 @@ public:
|
|||
);
|
||||
|
||||
bool added = m_imp.add_eq(je, ke, exp, is_fixed);
|
||||
if (added)
|
||||
lp().stats().m_offset_eqs++;
|
||||
if (added) {
|
||||
if (is_fixed)
|
||||
lp().stats().m_fixed_eqs++;
|
||||
else
|
||||
lp().stats().m_offset_eqs++;
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,75 +23,24 @@ Revision History:
|
|||
#include "util/vector.h"
|
||||
#include <functional>
|
||||
#include "math/lp/lp_core_solver_base_def.h"
|
||||
template bool lp::lp_core_solver_base<double, double>::A_mult_x_is_off() const;
|
||||
template bool lp::lp_core_solver_base<double, double>::A_mult_x_is_off_on_index(const vector<unsigned> &) const;
|
||||
template bool lp::lp_core_solver_base<double, double>::basis_heading_is_correct() const;
|
||||
template void lp::lp_core_solver_base<double, double>::calculate_pivot_row_of_B_1(unsigned int);
|
||||
template void lp::lp_core_solver_base<double, double>::calculate_pivot_row_when_pivot_row_of_B1_is_ready(unsigned);
|
||||
template bool lp::lp_core_solver_base<double, double>::column_is_dual_feasible(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<double, double>::fill_reduced_costs_from_m_y_by_rows();
|
||||
template bool lp::lp_core_solver_base<double, double>::find_x_by_solving();
|
||||
template lp::non_basic_column_value_position lp::lp_core_solver_base<double, double>::get_non_basic_column_value_position(unsigned int) const;
|
||||
template lp::non_basic_column_value_position lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::get_non_basic_column_value_position(unsigned int) const;
|
||||
template lp::non_basic_column_value_position lp::lp_core_solver_base<lp::mpq, lp::mpq>::get_non_basic_column_value_position(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<double, double>::init_reduced_costs_for_one_iteration();
|
||||
template lp::lp_core_solver_base<double, double>::lp_core_solver_base(
|
||||
lp::static_matrix<double, double>&, vector<double>&,
|
||||
vector<unsigned int >&,
|
||||
vector<unsigned> &, vector<int> &,
|
||||
vector<double >&,
|
||||
vector<double >&,
|
||||
lp::lp_settings&, const column_namer&, const vector<lp::column_type >&,
|
||||
const vector<double >&,
|
||||
const vector<double >&);
|
||||
|
||||
template bool lp::lp_core_solver_base<double, double>::print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over(char const*, std::ostream &);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over(char const*, std::ostream &);
|
||||
template void lp::lp_core_solver_base<double, double>::restore_x(unsigned int, double const&);
|
||||
template void lp::lp_core_solver_base<double, double>::set_non_basic_x_to_correct_bounds();
|
||||
template void lp::lp_core_solver_base<double, double>::snap_xN_to_bounds_and_free_columns_to_zeroes();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::snap_xN_to_bounds_and_free_columns_to_zeroes();
|
||||
template void lp::lp_core_solver_base<double, double>::solve_Ax_eq_b();
|
||||
template void lp::lp_core_solver_base<double, double>::solve_Bd(unsigned int);
|
||||
template void lp::lp_core_solver_base<double, double>::solve_Bd(unsigned int, lp::indexed_vector<double>&, lp::indexed_vector<double>&) const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq>>::solve_Bd(unsigned int, indexed_vector<lp::mpq>&);
|
||||
template void lp::lp_core_solver_base<double, double>::solve_yB(vector<double >&) const;
|
||||
template bool lp::lp_core_solver_base<double, double>::update_basis_and_x(int, int, double const&);
|
||||
template void lp::lp_core_solver_base<double, double>::add_delta_to_entering(unsigned int, const double&);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::A_mult_x_is_off() const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::A_mult_x_is_off_on_index(const vector<unsigned> &) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::basis_heading_is_correct() const ;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::calculate_pivot_row_of_B_1(unsigned int);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::calculate_pivot_row_when_pivot_row_of_B1_is_ready(unsigned);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::column_is_dual_feasible(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::fill_reduced_costs_from_m_y_by_rows();
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::find_x_by_solving();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::init_reduced_costs_for_one_iteration();
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over(char const*, std::ostream &);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::restore_x(unsigned int, lp::mpq const&);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::set_non_basic_x_to_correct_bounds();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::solve_Ax_eq_b();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::solve_Bd(unsigned int);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::solve_yB(vector<lp::mpq>&) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::update_basis_and_x(int, int, lp::mpq const&);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::add_delta_to_entering(unsigned int, const lp::mpq&);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::calculate_pivot_row_of_B_1(unsigned int);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::calculate_pivot_row_when_pivot_row_of_B1_is_ready(unsigned);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::init();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::init_basis_heading_and_non_basic_columns_vector();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::init_reduced_costs_for_one_iteration();
|
||||
template lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::lp_core_solver_base(lp::static_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&, vector<lp::numeric_pair<lp::mpq> >&, vector<unsigned int >&, vector<unsigned> &, vector<int> &, vector<lp::numeric_pair<lp::mpq> >&, vector<lp::mpq>&, lp::lp_settings&, const column_namer&, const vector<lp::column_type >&,
|
||||
template lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::lp_core_solver_base(lp::static_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&,
|
||||
// vector<lp::numeric_pair<lp::mpq> >&,
|
||||
vector<unsigned int >&, vector<unsigned> &, vector<int> &, vector<lp::numeric_pair<lp::mpq> >&, vector<lp::mpq>&, lp::lp_settings&, const column_namer&, const vector<lp::column_type >&,
|
||||
const vector<lp::numeric_pair<lp::mpq> >&,
|
||||
const vector<lp::numeric_pair<lp::mpq> >&);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::print_statistics_with_cost_and_check_that_the_time_is_over(lp::numeric_pair<lp::mpq>, std::ostream&);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::snap_xN_to_bounds_and_fill_xB();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::solve_Ax_eq_b();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::solve_Bd(unsigned int);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::update_basis_and_x(int, int, lp::numeric_pair<lp::mpq> const&);
|
||||
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::add_delta_to_entering(unsigned int, const lp::numeric_pair<lp::mpq>&);
|
||||
template lp::lp_core_solver_base<lp::mpq, lp::mpq>::lp_core_solver_base(
|
||||
lp::static_matrix<lp::mpq, lp::mpq>&,
|
||||
vector<lp::mpq>&,
|
||||
//vector<lp::mpq>&,
|
||||
vector<unsigned int >&,
|
||||
vector<unsigned> &, vector<int> &,
|
||||
vector<lp::mpq>&,
|
||||
|
|
@ -102,49 +51,20 @@ template lp::lp_core_solver_base<lp::mpq, lp::mpq>::lp_core_solver_base(
|
|||
const vector<lp::mpq>&,
|
||||
const vector<lp::mpq>&);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::print_statistics_with_iterations_and_check_that_the_time_is_over(std::ostream &);
|
||||
template std::string lp::lp_core_solver_base<double, double>::column_name(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<double, double>::pretty_print(std::ostream & out);
|
||||
template void lp::lp_core_solver_base<double, double>::restore_state(double*, double*);
|
||||
template void lp::lp_core_solver_base<double, double>::save_state(double*, double*);
|
||||
template std::string lp::lp_core_solver_base<lp::mpq, lp::mpq>::column_name(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::pretty_print(std::ostream & out);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::restore_state(lp::mpq*, lp::mpq*);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::save_state(lp::mpq*, lp::mpq*);
|
||||
template std::string lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::column_name(unsigned int) const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::pretty_print(std::ostream & out);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::restore_state(lp::mpq*, lp::mpq*);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::save_state(lp::mpq*, lp::mpq*);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::solve_yB(vector<lp::mpq>&) const;
|
||||
template void lp::lp_core_solver_base<double, double>::init_lu();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::mpq>::init_lu();
|
||||
template int lp::lp_core_solver_base<double, double>::pivots_in_column_and_row_are_different(int, int) const;
|
||||
template int lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::pivots_in_column_and_row_are_different(int, int) const;
|
||||
template int lp::lp_core_solver_base<lp::mpq, lp::mpq>::pivots_in_column_and_row_are_different(int, int) const;
|
||||
template bool lp::lp_core_solver_base<double, double>::calc_current_x_is_feasible_include_non_basis(void)const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::calc_current_x_is_feasible_include_non_basis(void)const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::calc_current_x_is_feasible_include_non_basis() const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::pivot_fixed_vars_from_basis();
|
||||
template bool lp::lp_core_solver_base<double, double>::column_is_feasible(unsigned int) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::column_is_feasible(unsigned int) const;
|
||||
// template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::print_linear_combination_of_column_indices(vector<std::pair<lp::mpq, unsigned int>, std::allocator<std::pair<lp::mpq, unsigned int> > > const&, std::ostream&) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::column_is_feasible(unsigned int) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::snap_non_basic_x_to_bound();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::init_lu();
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::A_mult_x_is_off_on_index(vector<unsigned int> const&) const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::find_x_by_solving();
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::restore_x(unsigned int, lp::numeric_pair<lp::mpq> const&);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq>>::pivot_column_tableau(unsigned int, unsigned int);
|
||||
template bool lp::lp_core_solver_base<double, double>::pivot_column_tableau(unsigned int, unsigned int);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::pivot_column_tableau(unsigned int, unsigned int);
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::transpose_rows_tableau(unsigned int, unsigned int);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::inf_set_is_correct() const;
|
||||
template bool lp::lp_core_solver_base<double, double>::inf_set_is_correct() const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq>::inf_set_is_correct() const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::infeasibility_costs_are_correct() const;
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::mpq >::infeasibility_costs_are_correct() const;
|
||||
template bool lp::lp_core_solver_base<double, double >::infeasibility_costs_are_correct() const;
|
||||
template void lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::calculate_pivot_row(unsigned int);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::remove_from_basis(unsigned int);
|
||||
template bool lp::lp_core_solver_base<lp::mpq, lp::numeric_pair<lp::mpq> >::remove_from_basis(unsigned int, lp::numeric_pair<lp::mpq> const&);
|
||||
template void lp::lp_core_solver_base<rational, rational>::solve_Bd(unsigned int, lp::indexed_vector<rational>&, lp::indexed_vector<rational>&) const;
|
||||
template void lp::lp_core_solver_base<rational, lp::numeric_pair<rational> >::solve_Bd(unsigned int, lp::indexed_vector<rational>&, lp::indexed_vector<rational>&) const;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -25,11 +25,21 @@ Revision History:
|
|||
#include "math/lp/core_solver_pretty_printer.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include "math/lp/lu.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
#include "math/lp/column_namer.h"
|
||||
#include "math/lp/u_set.h"
|
||||
|
||||
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
X dot_product(const vector<T> & a, const vector<X> & b) {
|
||||
lp_assert(a.size() == b.size());
|
||||
auto r = zero_of_type<X>();
|
||||
for (unsigned i = 0; i < a.size(); i++) {
|
||||
r += a[i] * b[i];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename T, typename X> // X represents the type of the x variable and the bounds
|
||||
class lp_core_solver_base {
|
||||
|
|
@ -53,44 +63,31 @@ public:
|
|||
bool current_x_is_infeasible() const { return m_inf_set.size() != 0; }
|
||||
private:
|
||||
u_set m_inf_set;
|
||||
bool m_using_infeas_costs;
|
||||
public:
|
||||
const u_set& inf_set() const { return m_inf_set; }
|
||||
u_set& inf_set() { return m_inf_set; }
|
||||
void inf_set_increase_size_by_one() { m_inf_set.increase_size_by_one(); }
|
||||
bool inf_set_contains(unsigned j) const { return m_inf_set.contains(j); }
|
||||
unsigned inf_set_size() const { return m_inf_set.size(); }
|
||||
bool using_infeas_costs() const { return m_using_infeas_costs; }
|
||||
void set_using_infeas_costs(bool val) { m_using_infeas_costs = val; }
|
||||
vector<unsigned> m_columns_nz; // m_columns_nz[i] keeps an approximate value of non zeroes the i-th column
|
||||
vector<unsigned> m_rows_nz; // m_rows_nz[i] keeps an approximate value of non zeroes in the i-th row
|
||||
indexed_vector<T> m_pivot_row_of_B_1; // the pivot row of the reverse of B
|
||||
unsigned inf_set_size() const { return m_inf_set.size(); }
|
||||
indexed_vector<T> m_pivot_row; // this is the real pivot row of the simplex tableu
|
||||
static_matrix<T, X> & m_A; // the matrix A
|
||||
vector<X> & m_b; // the right side
|
||||
// vector<X> const & m_b; // the right side
|
||||
vector<unsigned> & m_basis;
|
||||
vector<unsigned>& m_nbasis;
|
||||
vector<int>& m_basis_heading;
|
||||
vector<X> & m_x; // a feasible solution, the fist time set in the constructor
|
||||
vector<X> & m_x; // a feasible solution, the first time set in the constructor
|
||||
vector<T> & m_costs;
|
||||
lp_settings & m_settings;
|
||||
lu<static_matrix<T, X>> * m_factorization = nullptr;
|
||||
vector<T> m_y; // the buffer for yB = cb
|
||||
// a device that is able to solve Bx=c, xB=d, and change the basis
|
||||
|
||||
const column_namer & m_column_names;
|
||||
indexed_vector<T> m_w; // the vector featuring in 24.3 of the Chvatal book
|
||||
vector<T> m_d; // the vector of reduced costs
|
||||
indexed_vector<T> m_ed; // the solution of B*m_ed = a
|
||||
const vector<column_type> & m_column_types;
|
||||
const vector<X> & m_lower_bounds;
|
||||
const vector<X> & m_upper_bounds;
|
||||
vector<T> m_column_norms; // the approximate squares of column norms that help choosing a profitable column
|
||||
vector<X> m_copy_of_xB;
|
||||
const vector<X> & m_upper_bounds;
|
||||
unsigned m_basis_sort_counter;
|
||||
vector<T> m_steepest_edge_coefficients;
|
||||
vector<unsigned> m_trace_of_basis_change_vector; // the even positions are entering, the odd positions are leaving
|
||||
bool m_tracing_basis_changes;
|
||||
u_set* m_pivoted_rows;
|
||||
u_set* m_pivoted_rows;
|
||||
bool m_look_for_feasible_solution_only;
|
||||
|
||||
void start_tracing_basis_changes() {
|
||||
|
|
@ -118,7 +115,7 @@ public:
|
|||
unsigned m_n() const { return m_A.column_count(); } // the number of columns in the matrix m_A
|
||||
|
||||
lp_core_solver_base(static_matrix<T, X> & A,
|
||||
vector<X> & b, // the right side vector
|
||||
//vector<X> & b, // the right side vector
|
||||
vector<unsigned> & basis,
|
||||
vector<unsigned> & nbasis,
|
||||
vector<int> & heading,
|
||||
|
|
@ -134,7 +131,7 @@ public:
|
|||
void init();
|
||||
|
||||
virtual ~lp_core_solver_base() {
|
||||
delete m_factorization;
|
||||
|
||||
}
|
||||
|
||||
vector<unsigned> & non_basis() {
|
||||
|
|
@ -149,46 +146,12 @@ public:
|
|||
lp_status get_status() const{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
void fill_cb(T * y) const;
|
||||
|
||||
void fill_cb(vector<T> & y) const;
|
||||
|
||||
void solve_yB(vector<T> & y) const;
|
||||
|
||||
void solve_Bd(unsigned entering, indexed_vector<T> & d_buff, indexed_vector<T>& w_buff) const;
|
||||
|
||||
void solve_Bd(unsigned entering);
|
||||
|
||||
void solve_Bd(unsigned entering, indexed_vector<T> & column);
|
||||
|
||||
void pretty_print(std::ostream & out);
|
||||
|
||||
void save_state(T * w_buffer, T * d_buffer);
|
||||
|
||||
void restore_state(T * w_buffer, T * d_buffer);
|
||||
|
||||
X get_cost() const {
|
||||
return dot_product(m_costs, m_x);
|
||||
}
|
||||
|
||||
void copy_m_w(T * buffer);
|
||||
|
||||
void restore_m_w(T * buffer);
|
||||
|
||||
// needed for debugging
|
||||
void copy_m_ed(T * buffer);
|
||||
|
||||
void restore_m_ed(T * buffer);
|
||||
|
||||
bool A_mult_x_is_off() const;
|
||||
|
||||
bool A_mult_x_is_off_on_index(const vector<unsigned> & index) const;
|
||||
// from page 182 of Istvan Maros's book
|
||||
void calculate_pivot_row_of_B_1(unsigned pivot_row);
|
||||
|
||||
void calculate_pivot_row_when_pivot_row_of_B1_is_ready(unsigned pivot_row);
|
||||
|
||||
void add_delta_to_entering(unsigned entering, const X & delta);
|
||||
|
||||
const X & get_var_value(unsigned j) const {
|
||||
|
|
@ -207,13 +170,10 @@ public:
|
|||
|
||||
void set_total_iterations(unsigned s) { m_total_iterations = s; }
|
||||
|
||||
void set_non_basic_x_to_correct_bounds();
|
||||
|
||||
bool at_bound(const X &x, const X & bound) const {
|
||||
return !below_bound(x, bound) && !above_bound(x, bound);
|
||||
}
|
||||
|
||||
|
||||
bool need_to_pivot_to_basis_tableau() const {
|
||||
unsigned m = m_A.row_count();
|
||||
for (unsigned i = 0; i < m; i++) {
|
||||
|
|
@ -235,11 +195,7 @@ public:
|
|||
if (m_settings.simplex_strategy() == simplex_strategy_enum::tableau_rows)
|
||||
return true;
|
||||
CASSERT("check_static_matrix", m_A.is_correct());
|
||||
if (m_using_infeas_costs) {
|
||||
if (infeasibility_costs_are_correct() == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned n = m_A.column_count();
|
||||
for (unsigned j = 0; j < n; j++) {
|
||||
|
|
@ -262,19 +218,17 @@ public:
|
|||
}
|
||||
|
||||
bool below_bound(const X & x, const X & bound) const {
|
||||
return precise()? x < bound : below_bound_numeric<X>(x, bound, m_settings.primal_feasibility_tolerance);
|
||||
return x < bound ;
|
||||
}
|
||||
|
||||
bool above_bound(const X & x, const X & bound) const {
|
||||
return precise()? x > bound : above_bound_numeric<X>(x, bound, m_settings.primal_feasibility_tolerance);
|
||||
return x > bound ;
|
||||
}
|
||||
|
||||
bool x_below_low_bound(unsigned p) const {
|
||||
return below_bound(m_x[p], m_lower_bounds[p]);
|
||||
}
|
||||
|
||||
bool infeasibility_costs_are_correct() const;
|
||||
bool infeasibility_cost_is_correct_for_column(unsigned j) const;
|
||||
|
||||
bool x_above_lower_bound(unsigned p) const {
|
||||
return above_bound(m_x[p], m_lower_bounds[p]);
|
||||
|
|
@ -284,7 +238,6 @@ public:
|
|||
return below_bound(m_x[p], m_upper_bounds[p]);
|
||||
}
|
||||
|
||||
|
||||
bool x_above_upper_bound(unsigned p) const {
|
||||
return above_bound(m_x[p], m_upper_bounds[p]);
|
||||
}
|
||||
|
|
@ -310,15 +263,10 @@ public:
|
|||
|
||||
bool d_is_not_positive(unsigned j) const;
|
||||
|
||||
|
||||
bool time_is_over();
|
||||
|
||||
void rs_minus_Anx(vector<X> & rs);
|
||||
|
||||
bool find_x_by_solving();
|
||||
|
||||
bool update_basis_and_x(int entering, int leaving, X const & tt);
|
||||
|
||||
bool basis_has_no_doubles() const;
|
||||
|
||||
bool non_basis_has_no_doubles() const;
|
||||
|
|
@ -328,79 +276,19 @@ public:
|
|||
|
||||
bool basis_heading_is_correct() const;
|
||||
|
||||
void restore_x_and_refactor(int entering, int leaving, X const & t);
|
||||
|
||||
void restore_x(unsigned entering, X const & t);
|
||||
|
||||
void fill_reduced_costs_from_m_y_by_rows();
|
||||
|
||||
void copy_rs_to_xB(vector<X> & rs);
|
||||
virtual bool lower_bounds_are_set() const { return false; }
|
||||
X lower_bound_value(unsigned j) const { return m_lower_bounds[j]; }
|
||||
X upper_bound_value(unsigned j) const { return m_upper_bounds[j]; }
|
||||
|
||||
column_type get_column_type(unsigned j) const {return m_column_types[j]; }
|
||||
|
||||
bool pivot_row_element_is_too_small_for_ratio_test(unsigned j) {
|
||||
return m_settings.abs_val_is_smaller_than_pivot_tolerance(m_pivot_row[j]);
|
||||
}
|
||||
|
||||
|
||||
X bound_span(unsigned j) const {
|
||||
return m_upper_bounds[j] - m_lower_bounds[j];
|
||||
}
|
||||
|
||||
std::string column_name(unsigned column) const;
|
||||
|
||||
void copy_right_side(vector<X> & rs);
|
||||
|
||||
void add_delta_to_xB(vector<X> & del);
|
||||
|
||||
void find_error_in_BxB(vector<X>& rs);
|
||||
|
||||
// recalculates the projection of x to B, such that Ax = b, whereab is the right side
|
||||
void solve_Ax_eq_b();
|
||||
|
||||
bool snap_non_basic_x_to_bound() {
|
||||
bool ret = false;
|
||||
for (unsigned j : non_basis())
|
||||
ret = snap_column_to_bound(j) || ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool snap_column_to_bound(unsigned j) {
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
if (x_is_at_bound(j))
|
||||
break;
|
||||
m_x[j] = m_lower_bounds[j];
|
||||
return true;
|
||||
case column_type::boxed:
|
||||
if (x_is_at_bound(j))
|
||||
break; // we should preserve x if possible
|
||||
// snap randomly
|
||||
if (m_settings.random_next() % 2 == 1)
|
||||
m_x[j] = m_lower_bounds[j];
|
||||
else
|
||||
m_x[j] = m_upper_bounds[j];
|
||||
return true;
|
||||
case column_type::lower_bound:
|
||||
if (x_is_at_lower_bound(j))
|
||||
break;
|
||||
m_x[j] = m_lower_bounds[j];
|
||||
return true;
|
||||
case column_type::upper_bound:
|
||||
if (x_is_at_upper_bound(j))
|
||||
break;
|
||||
m_x[j] = m_upper_bounds[j];
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool make_column_feasible(unsigned j, numeric_pair<mpq> & delta) {
|
||||
bool ret = false;
|
||||
lp_assert(m_basis_heading[j] < 0);
|
||||
|
|
@ -445,21 +333,7 @@ public:
|
|||
|
||||
}
|
||||
|
||||
|
||||
void snap_non_basic_x_to_bound_and_free_to_zeroes();
|
||||
void snap_xN_to_bounds_and_fill_xB();
|
||||
|
||||
void snap_xN_to_bounds_and_free_columns_to_zeroes();
|
||||
|
||||
void init_reduced_costs_for_one_iteration();
|
||||
|
||||
non_basic_column_value_position get_non_basic_column_value_position(unsigned j) const;
|
||||
|
||||
void init_lu();
|
||||
int pivots_in_column_and_row_are_different(int entering, int leaving) const;
|
||||
void pivot_fixed_vars_from_basis();
|
||||
bool remove_from_basis(unsigned j);
|
||||
bool remove_from_basis(unsigned j, const impq&);
|
||||
bool pivot_column_general(unsigned j, unsigned j_basic, indexed_vector<T> & w);
|
||||
void init_basic_part_of_basis_heading() {
|
||||
unsigned m = m_basis.size();
|
||||
|
|
@ -531,31 +405,6 @@ public:
|
|||
change_basis_unconditionally(leaving, entering);
|
||||
}
|
||||
|
||||
bool non_basic_column_is_set_correctly(unsigned j) const {
|
||||
if (j >= this->m_n())
|
||||
return false;
|
||||
switch (this->m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
case column_type::boxed:
|
||||
if (!this->x_is_at_bound(j))
|
||||
return false;
|
||||
break;
|
||||
case column_type::lower_bound:
|
||||
if (!this->x_is_at_lower_bound(j))
|
||||
return false;
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
if (!this->x_is_at_upper_bound(j))
|
||||
return false;
|
||||
break;
|
||||
case column_type::free_column:
|
||||
break;
|
||||
default:
|
||||
lp_assert(false);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool non_basic_columns_are_set_correctly() const {
|
||||
for (unsigned j : this->m_nbasis)
|
||||
if (!column_is_feasible(j)) {
|
||||
|
|
@ -615,13 +464,11 @@ public:
|
|||
out << "[-oo, oo]";
|
||||
break;
|
||||
default:
|
||||
lp_assert(false);
|
||||
UNREACHABLE();
|
||||
}
|
||||
return out << "\n";
|
||||
}
|
||||
|
||||
bool column_is_free(unsigned j) const { return this->m_column_types[j] == column_type::free_column; }
|
||||
|
||||
bool column_is_fixed(unsigned j) const { return this->m_column_types[j] == column_type::fixed; }
|
||||
|
||||
|
||||
|
|
@ -654,16 +501,6 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// only check for basic columns
|
||||
bool calc_current_x_is_feasible() const {
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
if (!column_is_feasible(m_basis[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void transpose_rows_tableau(unsigned i, unsigned ii);
|
||||
|
||||
void pivot_to_reduced_costs_tableau(unsigned i, unsigned j);
|
||||
|
|
@ -671,13 +508,10 @@ public:
|
|||
bool pivot_column_tableau(unsigned j, unsigned row_index);
|
||||
bool divide_row_by_pivot(unsigned pivot_row, unsigned pivot_col);
|
||||
|
||||
bool precise() const { return numeric_traits<T>::precise(); }
|
||||
|
||||
simplex_strategy_enum simplex_strategy() const { return
|
||||
m_settings.simplex_strategy();
|
||||
}
|
||||
|
||||
bool use_tableau() const { return m_settings.use_tableau(); }
|
||||
|
||||
template <typename K>
|
||||
static void swap(vector<K> &v, unsigned i, unsigned j) {
|
||||
|
|
@ -767,7 +601,7 @@ public:
|
|||
return m_iters_with_no_cost_growing;
|
||||
}
|
||||
|
||||
void calculate_pivot_row(unsigned i);
|
||||
|
||||
unsigned get_base_column_in_row(unsigned row_index) const {
|
||||
return m_basis[row_index];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace lp {
|
|||
|
||||
template <typename T, typename X> lp_core_solver_base<T, X>::
|
||||
lp_core_solver_base(static_matrix<T, X> & A,
|
||||
vector<X> & b, // the right side vector
|
||||
// vector<X> & b, // the right side vector
|
||||
vector<unsigned> & basis,
|
||||
vector<unsigned> & nbasis,
|
||||
vector<int> & heading,
|
||||
|
|
@ -43,29 +43,20 @@ lp_core_solver_base(static_matrix<T, X> & A,
|
|||
m_iters_with_no_cost_growing(0),
|
||||
m_status(lp_status::FEASIBLE),
|
||||
m_inf_set(A.column_count()),
|
||||
m_using_infeas_costs(false),
|
||||
m_pivot_row_of_B_1(A.row_count()),
|
||||
m_pivot_row(A.column_count()),
|
||||
m_A(A),
|
||||
m_b(b),
|
||||
m_basis(basis),
|
||||
m_nbasis(nbasis),
|
||||
m_basis_heading(heading),
|
||||
m_x(x),
|
||||
m_costs(costs),
|
||||
m_settings(settings),
|
||||
m_y(m_m()),
|
||||
m_column_names(column_names),
|
||||
m_w(m_m()),
|
||||
m_d(m_n()),
|
||||
m_ed(m_m()),
|
||||
m_column_types(column_types),
|
||||
m_lower_bounds(lower_bound_values),
|
||||
m_upper_bounds(upper_bound_values),
|
||||
m_column_norms(m_n()),
|
||||
m_copy_of_xB(m_m()),
|
||||
m_basis_sort_counter(0),
|
||||
m_steepest_edge_coefficients(A.column_count()),
|
||||
m_tracing_basis_changes(false),
|
||||
m_pivoted_rows(nullptr),
|
||||
m_look_for_feasible_solution_only(false) {
|
||||
|
|
@ -82,8 +73,7 @@ allocate_basis_heading() { // the rest of initialization will be handled by the
|
|||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
init() {
|
||||
allocate_basis_heading();
|
||||
if (m_settings.use_lu())
|
||||
init_factorization(m_factorization, m_A, m_basis, m_settings);
|
||||
|
||||
}
|
||||
|
||||
// i is the pivot row, and j is the pivot column
|
||||
|
|
@ -102,26 +92,6 @@ pivot_to_reduced_costs_tableau(unsigned i, unsigned j) {
|
|||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
fill_cb(T * y) const {
|
||||
for (unsigned i = 0; i < m_m(); i++) {
|
||||
y[i] = m_costs[m_basis[i]];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
fill_cb(vector<T> & y) const {
|
||||
for (unsigned i = 0; i < m_m(); i++) {
|
||||
y[i] = m_costs[m_basis[i]];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
solve_yB(vector<T> & y) const {
|
||||
fill_cb(y); // now y = cB, that is the projection of costs to basis
|
||||
m_factorization->solve_yB_with_error_check(y, m_basis);
|
||||
}
|
||||
|
||||
// template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
// update_index_of_ed() {
|
||||
|
|
@ -132,35 +102,9 @@ solve_yB(vector<T> & y) const {
|
|||
// m_index_of_ed.push_back(i);
|
||||
// }
|
||||
// }
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::solve_Bd(unsigned entering, indexed_vector<T> & column) {
|
||||
lp_assert(!m_settings.use_tableau());
|
||||
if (m_factorization == nullptr) {
|
||||
init_factorization(m_factorization, m_A, m_basis, m_settings);
|
||||
}
|
||||
m_factorization->solve_Bd_faster(entering, column);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::solve_Bd(unsigned , indexed_vector<T>& , indexed_vector<T> &) const {
|
||||
NOT_IMPLEMENTED_YET();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
solve_Bd(unsigned entering) {
|
||||
lp_assert(m_ed.is_OK());
|
||||
m_factorization->solve_Bd(entering, m_ed, m_w);
|
||||
if (this->precise())
|
||||
m_columns_nz[entering] = m_ed.m_index.size();
|
||||
lp_assert(m_ed.is_OK());
|
||||
lp_assert(m_w.is_OK());
|
||||
#ifdef Z3DEBUG
|
||||
// auto B = get_B(*m_factorization, m_basis);
|
||||
// vector<T> a(m_m());
|
||||
// m_A.copy_column_to_vector(entering, a);
|
||||
// vector<T> cd(m_ed.m_data);
|
||||
// B.apply_from_left(cd, m_settings);
|
||||
// lp_assert(vectors_are_equal(cd , a));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
pretty_print(std::ostream & out) {
|
||||
|
|
@ -168,162 +112,11 @@ pretty_print(std::ostream & out) {
|
|||
pp.print();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
save_state(T * w_buffer, T * d_buffer) {
|
||||
copy_m_w(w_buffer);
|
||||
copy_m_ed(d_buffer);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
restore_state(T * w_buffer, T * d_buffer) {
|
||||
restore_m_w(w_buffer);
|
||||
restore_m_ed(d_buffer);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
copy_m_w(T * buffer) {
|
||||
unsigned i = m_m();
|
||||
while (i --) {
|
||||
buffer[i] = m_w[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
restore_m_w(T * buffer) {
|
||||
m_w.m_index.clear();
|
||||
unsigned i = m_m();
|
||||
while (i--) {
|
||||
if (!is_zero(m_w[i] = buffer[i]))
|
||||
m_w.m_index.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
// needed for debugging
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
copy_m_ed(T * buffer) {
|
||||
unsigned i = m_m();
|
||||
while (i --) {
|
||||
buffer[i] = m_ed[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
restore_m_ed(T * buffer) {
|
||||
unsigned i = m_m();
|
||||
while (i --) {
|
||||
m_ed[i] = buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
A_mult_x_is_off() const {
|
||||
lp_assert(m_x.size() == m_A.column_count());
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (unsigned i = 0; i < m_m(); i++) {
|
||||
X delta = m_b[i] - m_A.dot_product_with_row(i, m_x);
|
||||
if (delta != numeric_traits<X>::zero()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
T feps = convert_struct<T, double>::convert(m_settings.refactor_tolerance);
|
||||
X one = convert_struct<X, double>::convert(1.0);
|
||||
for (unsigned i = 0; i < m_m(); i++) {
|
||||
X delta = abs(m_b[i] - m_A.dot_product_with_row(i, m_x));
|
||||
X eps = feps * (one + T(0.1) * abs(m_b[i]));
|
||||
|
||||
if (delta > eps) {
|
||||
#if 0
|
||||
LP_OUT(m_settings, "x is off ("
|
||||
<< "m_b[" << i << "] = " << m_b[i] << " "
|
||||
<< "left side = " << m_A.dot_product_with_row(i, m_x) << ' '
|
||||
<< "delta = " << delta << ' '
|
||||
<< "iters = " << total_iterations() << ")" << std::endl);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
A_mult_x_is_off_on_index(const vector<unsigned> & index) const {
|
||||
lp_assert(m_x.size() == m_A.column_count());
|
||||
if (numeric_traits<T>::precise()) return false;
|
||||
#if RUN_A_MULT_X_IS_OFF_FOR_PRECESE
|
||||
for (unsigned i : index) {
|
||||
X delta = m_b[i] - m_A.dot_product_with_row(i, m_x);
|
||||
if (delta != numeric_traits<X>::zero()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
// todo(levnach) run on m_ed.m_index only !!!!!
|
||||
T feps = convert_struct<T, double>::convert(m_settings.refactor_tolerance);
|
||||
X one = convert_struct<X, double>::convert(1.0);
|
||||
for (unsigned i : index) {
|
||||
X delta = abs(m_b[i] - m_A.dot_product_with_row(i, m_x));
|
||||
X eps = feps * (one + T(0.1) * abs(m_b[i]));
|
||||
|
||||
if (delta > eps) {
|
||||
#if 0
|
||||
LP_OUT(m_settings, "x is off ("
|
||||
<< "m_b[" << i << "] = " << m_b[i] << " "
|
||||
<< "left side = " << m_A.dot_product_with_row(i, m_x) << ' '
|
||||
<< "delta = " << delta << ' '
|
||||
<< "iters = " << total_iterations() << ")" << std::endl);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// from page 182 of Istvan Maros's book
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
calculate_pivot_row_of_B_1(unsigned pivot_row) {
|
||||
lp_assert(! use_tableau());
|
||||
lp_assert(m_pivot_row_of_B_1.is_OK());
|
||||
m_pivot_row_of_B_1.clear();
|
||||
m_pivot_row_of_B_1.set_value(numeric_traits<T>::one(), pivot_row);
|
||||
lp_assert(m_pivot_row_of_B_1.is_OK());
|
||||
m_factorization->solve_yB_with_error_check_indexed(m_pivot_row_of_B_1, m_basis_heading, m_basis, m_settings);
|
||||
lp_assert(m_pivot_row_of_B_1.is_OK());
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
calculate_pivot_row_when_pivot_row_of_B1_is_ready(unsigned pivot_row) {
|
||||
m_pivot_row.clear();
|
||||
|
||||
for (unsigned i : m_pivot_row_of_B_1.m_index) {
|
||||
const T & pi_1 = m_pivot_row_of_B_1[i];
|
||||
if (numeric_traits<T>::is_zero(pi_1)) {
|
||||
continue;
|
||||
}
|
||||
for (auto & c : m_A.m_rows[i]) {
|
||||
unsigned j = c.var();
|
||||
if (m_basis_heading[j] < 0) {
|
||||
m_pivot_row.add_value_at_index_with_drop_tolerance(j, c.coeff() * pi_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (precise()) {
|
||||
m_rows_nz[pivot_row] = m_pivot_row.m_index.size();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
add_delta_to_entering(unsigned entering, const X& delta) {
|
||||
m_x[entering] += delta;
|
||||
if (!use_tableau())
|
||||
for (unsigned i : m_ed.m_index) {
|
||||
if (!numeric_traits<X>::precise())
|
||||
m_copy_of_xB[i] = m_x[m_basis[i]];
|
||||
m_x[m_basis[i]] -= delta * m_ed[i];
|
||||
}
|
||||
else
|
||||
|
||||
for (const auto & c : m_A.m_columns[entering]) {
|
||||
unsigned i = c.var();
|
||||
m_x[m_basis[i]] -= delta * m_A.get_val(c);
|
||||
|
|
@ -336,7 +129,7 @@ print_statistics(char const* str, X cost, std::ostream & out) {
|
|||
if (str!= nullptr)
|
||||
out << str << " ";
|
||||
out << "iterations = " << (total_iterations() - 1) << ", cost = " << T_to_string(cost)
|
||||
<< ", nonzeros = " << (m_factorization != nullptr? m_factorization->get_number_of_nonzeroes() : m_A.number_of_non_zeroes()) << std::endl;
|
||||
<< ", nonzeros = " << m_A.number_of_non_zeroes() << std::endl;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
|
|
@ -370,26 +163,6 @@ print_statistics_with_cost_and_check_that_the_time_is_over(X cost, std::ostream
|
|||
return time_is_over();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
set_non_basic_x_to_correct_bounds() {
|
||||
for (unsigned j : non_basis()) {
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::boxed:
|
||||
m_x[j] = m_d[j] < 0? m_upper_bounds[j]: m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::lower_bound:
|
||||
m_x[j] = m_lower_bounds[j];
|
||||
lp_assert(column_is_dual_feasible(j));
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
m_x[j] = m_upper_bounds[j];
|
||||
lp_assert(column_is_dual_feasible(j));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
column_is_dual_feasible(unsigned j) const {
|
||||
switch (m_column_types[j]) {
|
||||
|
|
@ -400,29 +173,24 @@ column_is_dual_feasible(unsigned j) const {
|
|||
case column_type::lower_bound:
|
||||
return x_is_at_lower_bound(j) && d_is_not_negative(j);
|
||||
case column_type::upper_bound:
|
||||
lp_assert(false); // impossible case
|
||||
UNREACHABLE();
|
||||
break;
|
||||
case column_type::free_column:
|
||||
return numeric_traits<X>::is_zero(m_d[j]);
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
return false;
|
||||
}
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
d_is_not_negative(unsigned j) const {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
return m_d[j] >= numeric_traits<T>::zero();
|
||||
}
|
||||
return m_d[j] > -T(0.00001);
|
||||
return m_d[j] >= numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
d_is_not_positive(unsigned j) const {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
return m_d[j] <= numeric_traits<T>::zero();
|
||||
}
|
||||
return m_d[j] < T(0.00001);
|
||||
return m_d[j] <= numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -441,7 +209,7 @@ template <typename T, typename X> void lp_core_solver_base<T, X>::
|
|||
rs_minus_Anx(vector<X> & rs) {
|
||||
unsigned row = m_m();
|
||||
while (row--) {
|
||||
auto &rsv = rs[row] = m_b[row];
|
||||
auto& rsv = rs[row] = zero_of_type<X>(); //m_b[row];
|
||||
for (auto & it : m_A.m_rows[row]) {
|
||||
unsigned j = it.var();
|
||||
if (m_basis_heading[j] < 0) {
|
||||
|
|
@ -451,45 +219,22 @@ rs_minus_Anx(vector<X> & rs) {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
find_x_by_solving() {
|
||||
solve_Ax_eq_b();
|
||||
bool ret= !A_mult_x_is_off();
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::column_is_feasible(unsigned j) const {
|
||||
const X& x = this->m_x[j];
|
||||
switch (this->m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
case column_type::boxed:
|
||||
if (this->above_bound(x, this->m_upper_bounds[j])) {
|
||||
return false;
|
||||
} else if (this->below_bound(x, this->m_lower_bounds[j])) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
return !this->above_bound(x, this->m_upper_bounds[j]) &&
|
||||
!this->below_bound(x, this->m_lower_bounds[j]);
|
||||
case column_type::lower_bound:
|
||||
if (this->below_bound(x, this->m_lower_bounds[j])) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
return !this->below_bound(x, this->m_lower_bounds[j]);
|
||||
case column_type::upper_bound:
|
||||
if (this->above_bound(x, this->m_upper_bounds[j])) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
return !this->above_bound(x, this->m_upper_bounds[j]);
|
||||
case column_type::free_column:
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
return false; // it is unreachable
|
||||
}
|
||||
|
|
@ -517,70 +262,9 @@ template <typename T, typename X> bool lp_core_solver_base<T, X>::inf_set_is_cor
|
|||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
update_basis_and_x(int entering, int leaving, X const & tt) {
|
||||
|
||||
if (!is_zero(tt)) {
|
||||
add_delta_to_entering(entering, tt);
|
||||
if ((!numeric_traits<T>::precise()) && A_mult_x_is_off_on_index(m_ed.m_index) && !find_x_by_solving()) {
|
||||
init_factorization(m_factorization, m_A, m_basis, m_settings);
|
||||
if (!find_x_by_solving()) {
|
||||
restore_x(entering, tt);
|
||||
if(A_mult_x_is_off()) {
|
||||
m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
m_iters_with_no_cost_growing++;
|
||||
return false;
|
||||
}
|
||||
|
||||
init_factorization(m_factorization, m_A, m_basis, m_settings);
|
||||
m_iters_with_no_cost_growing++;
|
||||
if (m_factorization->get_status() != LU_status::OK) {
|
||||
std::stringstream s;
|
||||
// s << "failing refactor on off_result for entering = " << entering << ", leaving = " << leaving << " total_iterations = " << total_iterations();
|
||||
m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool refactor = m_factorization->need_to_refactor();
|
||||
if (!refactor) {
|
||||
const T & pivot = this->m_pivot_row[entering]; // m_ed[m_factorization->basis_heading(leaving)] is the same but the one that we are using is more precise
|
||||
m_factorization->replace_column(pivot, m_w, m_basis_heading[leaving]);
|
||||
if (m_factorization->get_status() == LU_status::OK) {
|
||||
change_basis(entering, leaving);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// need to refactor == true
|
||||
change_basis(entering, leaving);
|
||||
init_lu();
|
||||
if (m_factorization->get_status() != LU_status::OK) {
|
||||
if (m_look_for_feasible_solution_only && !precise()) {
|
||||
m_status = lp_status::UNSTABLE;
|
||||
delete m_factorization;
|
||||
m_factorization = nullptr;
|
||||
return false;
|
||||
}
|
||||
// LP_OUT(m_settings, "failing refactor for entering = " << entering << ", leaving = " << leaving << " total_iterations = " << total_iterations() << std::endl);
|
||||
restore_x_and_refactor(entering, leaving, tt);
|
||||
if (m_status == lp_status::FLOATING_POINT_ERROR)
|
||||
return false;
|
||||
CASSERT("A_off", !A_mult_x_is_off());
|
||||
m_iters_with_no_cost_growing++;
|
||||
// LP_OUT(m_settings, "rolled back after failing of init_factorization()" << std::endl);
|
||||
m_status = lp_status::UNSTABLE;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
divide_row_by_pivot(unsigned pivot_row, unsigned pivot_col) {
|
||||
lp_assert(numeric_traits<T>::precise());
|
||||
int pivot_index = -1;
|
||||
auto & row = m_A.m_rows[pivot_row];
|
||||
unsigned size = row.size();
|
||||
|
|
@ -598,7 +282,7 @@ divide_row_by_pivot(unsigned pivot_row, unsigned pivot_col) {
|
|||
if (is_zero(coeff))
|
||||
return false;
|
||||
|
||||
this->m_b[pivot_row] /= coeff;
|
||||
// this->m_b[pivot_row] /= coeff;
|
||||
for (unsigned j = 0; j < size; j++) {
|
||||
auto & c = row[j];
|
||||
if (c.var() != pivot_col) {
|
||||
|
|
@ -662,259 +346,60 @@ basis_has_no_doubles() const {
|
|||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
non_basis_has_no_doubles() const {
|
||||
std::set<int> bm;
|
||||
for (auto j : m_nbasis) {
|
||||
bm.insert(j);
|
||||
}
|
||||
for (auto j : m_nbasis)
|
||||
bm.insert(j);
|
||||
return bm.size() == m_nbasis.size();
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
basis_is_correctly_represented_in_heading() const {
|
||||
for (unsigned i = 0; i < m_m(); i++) {
|
||||
for (unsigned i = 0; i < m_m(); i++)
|
||||
if (m_basis_heading[m_basis[i]] != static_cast<int>(i))
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
non_basis_is_correctly_represented_in_heading() const {
|
||||
for (unsigned i = 0; i < m_nbasis.size(); i++) {
|
||||
for (unsigned i = 0; i < m_nbasis.size(); i++)
|
||||
if (m_basis_heading[m_nbasis[i]] != - static_cast<int>(i) - 1)
|
||||
return false;
|
||||
}
|
||||
for (unsigned j = 0; j < m_A.column_count(); j++) {
|
||||
if (m_basis_heading[j] >= 0) {
|
||||
|
||||
for (unsigned j = 0; j < m_A.column_count(); j++)
|
||||
if (m_basis_heading[j] >= 0)
|
||||
lp_assert(static_cast<unsigned>(m_basis_heading[j]) < m_A.row_count() && m_basis[m_basis_heading[j]] == j);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::
|
||||
basis_heading_is_correct() const {
|
||||
if ( m_A.column_count() > 10 ) { // for the performance reason
|
||||
if ( m_A.column_count() > 10 ) // for the performance reason
|
||||
return true;
|
||||
}
|
||||
|
||||
lp_assert(m_basis_heading.size() == m_A.column_count());
|
||||
lp_assert(m_basis.size() == m_A.row_count());
|
||||
lp_assert(m_nbasis.size() <= m_A.column_count() - m_A.row_count()); // for the dual the size of non basis can be smaller
|
||||
if (!basis_has_no_doubles()) {
|
||||
|
||||
if (!basis_has_no_doubles())
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!non_basis_has_no_doubles()) {
|
||||
|
||||
if (!non_basis_has_no_doubles())
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!basis_is_correctly_represented_in_heading())
|
||||
return false;
|
||||
|
||||
if (!basis_is_correctly_represented_in_heading()) {
|
||||
if (!non_basis_is_correctly_represented_in_heading())
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!non_basis_is_correctly_represented_in_heading()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
restore_x_and_refactor(int entering, int leaving, X const & t) {
|
||||
this->restore_basis_change(entering, leaving);
|
||||
restore_x(entering, t);
|
||||
init_factorization(m_factorization, m_A, m_basis, m_settings);
|
||||
if (m_factorization->get_status() == LU_status::Degenerated) {
|
||||
LP_OUT(m_settings, "cannot refactor" << std::endl);
|
||||
m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
return;
|
||||
}
|
||||
// solve_Ax_eq_b();
|
||||
if (A_mult_x_is_off()) {
|
||||
LP_OUT(m_settings, "cannot restore solution" << std::endl);
|
||||
m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
restore_x(unsigned entering, X const & t) {
|
||||
if (is_zero(t)) return;
|
||||
m_x[entering] -= t;
|
||||
for (unsigned i : m_ed.m_index) {
|
||||
m_x[m_basis[i]] = m_copy_of_xB[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
fill_reduced_costs_from_m_y_by_rows() {
|
||||
unsigned j = m_n();
|
||||
while (j--) {
|
||||
if (m_basis_heading[j] < 0)
|
||||
m_d[j] = m_costs[j];
|
||||
else
|
||||
m_d[j] = numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
unsigned i = m_m();
|
||||
while (i--) {
|
||||
const T & y = m_y[i];
|
||||
if (is_zero(y)) continue;
|
||||
for (row_cell<T> & c : m_A.m_rows[i]) {
|
||||
j = c.var();
|
||||
if (m_basis_heading[j] < 0) {
|
||||
m_d[j] -= y * c.coeff();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
copy_rs_to_xB(vector<X> & rs) {
|
||||
unsigned j = m_m();
|
||||
while (j--) {
|
||||
m_x[m_basis[j]] = rs[j];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> std::string lp_core_solver_base<T, X>::
|
||||
column_name(unsigned column) const {
|
||||
return m_column_names.get_variable_name(column);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
copy_right_side(vector<X> & rs) {
|
||||
unsigned i = m_m();
|
||||
while (i --) {
|
||||
rs[i] = m_b[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
add_delta_to_xB(vector<X> & del) {
|
||||
unsigned i = m_m();
|
||||
while (i--) {
|
||||
this->m_x[this->m_basis[i]] -= del[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
find_error_in_BxB(vector<X>& rs){
|
||||
unsigned row = m_m();
|
||||
while (row--) {
|
||||
auto &rsv = rs[row];
|
||||
for (auto & it : m_A.m_rows[row]) {
|
||||
unsigned j = it.var();
|
||||
if (m_basis_heading[j] >= 0) {
|
||||
rsv -= m_x[j] * it.coeff();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// recalculates the projection of x to B, such that Ax = b
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
solve_Ax_eq_b() {
|
||||
if (numeric_traits<X>::precise()) {
|
||||
vector<X> rs(m_m());
|
||||
rs_minus_Anx(rs);
|
||||
m_factorization->solve_By(rs);
|
||||
copy_rs_to_xB(rs);
|
||||
} else {
|
||||
vector<X> rs(m_m());
|
||||
rs_minus_Anx(rs);
|
||||
vector<X> rrs = rs; // another copy of rs
|
||||
m_factorization->solve_By(rs);
|
||||
copy_rs_to_xB(rs);
|
||||
find_error_in_BxB(rrs);
|
||||
m_factorization->solve_By(rrs);
|
||||
add_delta_to_xB(rrs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
snap_non_basic_x_to_bound_and_free_to_zeroes() {
|
||||
for (unsigned j : non_basis()) {
|
||||
lp_assert(j < m_x.size());
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
case column_type::boxed:
|
||||
case column_type::lower_bound:
|
||||
m_x[j] = m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
m_x[j] = m_upper_bounds[j];
|
||||
break;
|
||||
default:
|
||||
m_x[j] = zero_of_type<X>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
snap_xN_to_bounds_and_fill_xB() {
|
||||
snap_non_basic_x_to_bound();
|
||||
solve_Ax_eq_b();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
snap_xN_to_bounds_and_free_columns_to_zeroes() {
|
||||
snap_non_basic_x_to_bound_and_free_to_zeroes();
|
||||
solve_Ax_eq_b();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::
|
||||
init_reduced_costs_for_one_iteration() {
|
||||
solve_yB(m_y);
|
||||
fill_reduced_costs_from_m_y_by_rows();
|
||||
}
|
||||
|
||||
template <typename T, typename X> non_basic_column_value_position lp_core_solver_base<T, X>::
|
||||
get_non_basic_column_value_position(unsigned j) const {
|
||||
switch (m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
return x_is_at_lower_bound(j)? at_fixed : not_at_bound;
|
||||
case column_type::free_column:
|
||||
return free_of_bounds;
|
||||
case column_type::boxed:
|
||||
return x_is_at_lower_bound(j)? at_lower_bound :(
|
||||
x_is_at_upper_bound(j)? at_upper_bound:
|
||||
not_at_bound
|
||||
);
|
||||
case column_type::lower_bound:
|
||||
return x_is_at_lower_bound(j)? at_lower_bound : not_at_bound;
|
||||
case column_type::upper_bound:
|
||||
return x_is_at_upper_bound(j)? at_upper_bound : not_at_bound;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
lp_unreachable();
|
||||
return at_lower_bound;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::init_lu() {
|
||||
init_factorization(this->m_factorization, this->m_A, this->m_basis, this->m_settings);
|
||||
}
|
||||
|
||||
template <typename T, typename X> int lp_core_solver_base<T, X>::pivots_in_column_and_row_are_different(int entering, int leaving) const {
|
||||
const T & column_p = this->m_ed[this->m_basis_heading[leaving]];
|
||||
const T & row_p = this->m_pivot_row[entering];
|
||||
if (is_zero(column_p) || is_zero(row_p)) return true; // pivots cannot be zero
|
||||
// the pivots have to have the same sign
|
||||
if (column_p < 0) {
|
||||
if (row_p > 0)
|
||||
return 2;
|
||||
} else { // column_p > 0
|
||||
if (row_p < 0)
|
||||
return 2;
|
||||
}
|
||||
T diff_normalized = abs((column_p - row_p) / (numeric_traits<T>::one() + abs(row_p)));
|
||||
if ( !this->m_settings.abs_val_is_smaller_than_harris_tolerance(diff_normalized / T(10)))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::transpose_rows_tableau(unsigned i, unsigned j) {
|
||||
transpose_basis(i, j);
|
||||
m_A.transpose_rows(i, j);
|
||||
|
|
@ -924,51 +409,14 @@ template <typename T, typename X> bool lp_core_solver_base<T, X>::pivot_column_g
|
|||
lp_assert(m_basis_heading[j] < 0);
|
||||
lp_assert(m_basis_heading[j_basic] >= 0);
|
||||
unsigned row_index = m_basis_heading[j_basic];
|
||||
if (m_settings.m_simplex_strategy == simplex_strategy_enum::lu) {
|
||||
if (m_factorization->need_to_refactor()) {
|
||||
init_lu();
|
||||
}
|
||||
else {
|
||||
m_factorization->prepare_entering(j, w); // to init vector w
|
||||
m_factorization->replace_column(zero_of_type<T>(), w, row_index);
|
||||
}
|
||||
if (m_factorization->get_status() != LU_status::OK) {
|
||||
init_lu();
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
change_basis(j, j_basic);
|
||||
}
|
||||
}
|
||||
else { // the tableau case
|
||||
if (pivot_column_tableau(j, row_index))
|
||||
change_basis(j, j_basic);
|
||||
else return false;
|
||||
}
|
||||
// the tableau case
|
||||
if (pivot_column_tableau(j, row_index))
|
||||
change_basis(j, j_basic);
|
||||
else return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_core_solver_base<T, X>::pivot_fixed_vars_from_basis() {
|
||||
// run over basis and non-basis at the same time
|
||||
indexed_vector<T> w(m_basis.size()); // the buffer
|
||||
unsigned i = 0; // points to basis
|
||||
for (; i < m_basis.size(); i++) {
|
||||
unsigned basic_j = m_basis[i];
|
||||
|
||||
if (get_column_type(basic_j) != column_type::fixed) continue;
|
||||
T a;
|
||||
unsigned j;
|
||||
for (auto &c : m_A.m_rows[i]) {
|
||||
j = c.var();
|
||||
if (j == basic_j)
|
||||
continue;
|
||||
if (get_column_type(j) != column_type::fixed) {
|
||||
if (pivot_column_general(j, basic_j, w))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::remove_from_basis(unsigned basic_j) {
|
||||
indexed_vector<T> w(m_basis.size()); // the buffer
|
||||
|
|
@ -982,91 +430,5 @@ template <typename T, typename X> bool lp_core_solver_base<T, X>::remove_from_ba
|
|||
return false;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_core_solver_base<T, X>::remove_from_basis(unsigned basic_j, const impq& val) {
|
||||
indexed_vector<T> w(m_basis.size()); // the buffer
|
||||
unsigned i = m_basis_heading[basic_j];
|
||||
for (auto &c : m_A.m_rows[i]) {
|
||||
if (c.var() == basic_j)
|
||||
continue;
|
||||
if (pivot_column_general(c.var(), basic_j, w))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> bool
|
||||
lp_core_solver_base<T, X>::infeasibility_costs_are_correct() const {
|
||||
if (! this->m_using_infeas_costs)
|
||||
return true;
|
||||
lp_assert(costs_on_nbasis_are_zeros());
|
||||
for (unsigned j :this->m_basis) {
|
||||
if (!infeasibility_cost_is_correct_for_column(j)) {
|
||||
TRACE("lar_solver", tout << "incorrect cost for column " << j << std::endl;);
|
||||
return false;
|
||||
}
|
||||
if (!is_zero(m_d[j])) {
|
||||
TRACE("lar_solver", tout << "non zero inf cost for basis j = " << j << std::endl;);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool
|
||||
lp_core_solver_base<T, X>::infeasibility_cost_is_correct_for_column(unsigned j) const {
|
||||
T r = (!this->m_settings.use_breakpoints_in_feasibility_search)? -one_of_type<T>(): one_of_type<T>();
|
||||
|
||||
switch (this->m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
case column_type::boxed:
|
||||
if (this->x_above_upper_bound(j)) {
|
||||
return (this->m_costs[j] == r);
|
||||
}
|
||||
if (this->x_below_low_bound(j)) {
|
||||
return (this->m_costs[j] == -r);
|
||||
}
|
||||
return is_zero(this->m_costs[j]);
|
||||
|
||||
case column_type::lower_bound:
|
||||
if (this->x_below_low_bound(j)) {
|
||||
return this->m_costs[j] == -r;
|
||||
}
|
||||
return is_zero(this->m_costs[j]);
|
||||
|
||||
case column_type::upper_bound:
|
||||
if (this->x_above_upper_bound(j)) {
|
||||
return this->m_costs[j] == r;
|
||||
}
|
||||
return is_zero(this->m_costs[j]);
|
||||
case column_type::free_column:
|
||||
return is_zero(this->m_costs[j]);
|
||||
default:
|
||||
lp_assert(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void lp_core_solver_base<T, X>::calculate_pivot_row(unsigned i) {
|
||||
lp_assert(!use_tableau());
|
||||
lp_assert(m_pivot_row.is_OK());
|
||||
m_pivot_row_of_B_1.clear();
|
||||
m_pivot_row_of_B_1.resize(m_m());
|
||||
m_pivot_row.clear();
|
||||
m_pivot_row.resize(m_n());
|
||||
if (m_settings.use_tableau()) {
|
||||
unsigned basic_j = m_basis[i];
|
||||
for (auto & c : m_A.m_rows[i]) {
|
||||
if (c.var() != basic_j)
|
||||
m_pivot_row.set_value(c.coeff(), c.var());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
calculate_pivot_row_of_B_1(i);
|
||||
calculate_pivot_row_when_pivot_row_of_B1_is_ready(i);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include <functional>
|
||||
#include "math/lp/lp_dual_core_solver_def.h"
|
||||
template void lp::lp_dual_core_solver<lp::mpq, lp::mpq>::start_with_initial_basis_and_make_it_dual_feasible();
|
||||
template void lp::lp_dual_core_solver<lp::mpq, lp::mpq>::solve();
|
||||
template lp::lp_dual_core_solver<double, double>::lp_dual_core_solver(lp::static_matrix<double, double>&, vector<bool>&,
|
||||
vector<double>&,
|
||||
vector<double>&,
|
||||
vector<unsigned int>&,
|
||||
vector<unsigned> &,
|
||||
vector<int> &,
|
||||
vector<double>&,
|
||||
vector<lp::column_type>&,
|
||||
vector<double>&,
|
||||
vector<double>&,
|
||||
lp::lp_settings&, const lp::column_namer&);
|
||||
template void lp::lp_dual_core_solver<double, double>::start_with_initial_basis_and_make_it_dual_feasible();
|
||||
template void lp::lp_dual_core_solver<double, double>::solve();
|
||||
template void lp::lp_dual_core_solver<lp::mpq, lp::mpq>::restore_non_basis();
|
||||
template void lp::lp_dual_core_solver<double, double>::restore_non_basis();
|
||||
template void lp::lp_dual_core_solver<double, double>::revert_to_previous_basis();
|
||||
template void lp::lp_dual_core_solver<lp::mpq, lp::mpq>::revert_to_previous_basis();
|
||||
|
|
@ -1,212 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include "math/lp/lp_core_solver_base.h"
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include "util/vector.h"
|
||||
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
class lp_dual_core_solver:public lp_core_solver_base<T, X> {
|
||||
public:
|
||||
vector<bool> & m_can_enter_basis;
|
||||
int m_r; // the row of the leaving column
|
||||
int m_p; // leaving column; that is m_p = m_basis[m_r]
|
||||
T m_delta; // the offset of the leaving basis variable
|
||||
int m_sign_of_alpha_r; // see page 27
|
||||
T m_theta_D;
|
||||
T m_theta_P;
|
||||
int m_q;
|
||||
// todo : replace by a vector later
|
||||
std::set<unsigned> m_breakpoint_set; // it is F in "Progress in the dual simplex method ..."
|
||||
std::set<unsigned> m_flipped_boxed;
|
||||
std::set<unsigned> m_tight_set; // it is the set of all breakpoints that become tight when m_q becomes tight
|
||||
vector<T> m_a_wave;
|
||||
vector<T> m_betas; // m_betas[i] is approximately a square of the norm of the i-th row of the reverse of B
|
||||
T m_harris_tolerance;
|
||||
std::set<unsigned> m_forbidden_rows;
|
||||
|
||||
lp_dual_core_solver(static_matrix<T, X> & A,
|
||||
vector<bool> & can_enter_basis,
|
||||
vector<X> & b, // the right side vector
|
||||
vector<X> & x, // the number of elements in x needs to be at least as large as the number of columns in A
|
||||
vector<unsigned> & basis,
|
||||
vector<unsigned> & nbasis,
|
||||
vector<int> & heading,
|
||||
vector<T> & costs,
|
||||
vector<column_type> & column_type_array,
|
||||
vector<X> & lower_bound_values,
|
||||
vector<X> & upper_bound_values,
|
||||
lp_settings & settings,
|
||||
const column_namer & column_names):
|
||||
lp_core_solver_base<T, X>(A,
|
||||
b,
|
||||
basis,
|
||||
nbasis,
|
||||
heading,
|
||||
x,
|
||||
costs,
|
||||
settings,
|
||||
column_names,
|
||||
column_type_array,
|
||||
lower_bound_values,
|
||||
upper_bound_values),
|
||||
m_can_enter_basis(can_enter_basis),
|
||||
m_a_wave(this->m_m()),
|
||||
m_betas(this->m_m()) {
|
||||
m_harris_tolerance = numeric_traits<T>::precise()? numeric_traits<T>::zero() : T(this->m_settings.harris_feasibility_tolerance);
|
||||
this->solve_yB(this->m_y);
|
||||
this->init_basic_part_of_basis_heading();
|
||||
fill_non_basis_with_only_able_to_enter_columns();
|
||||
}
|
||||
|
||||
void init_a_wave_by_zeros();
|
||||
|
||||
void fill_non_basis_with_only_able_to_enter_columns() {
|
||||
auto & nb = this->m_nbasis;
|
||||
nb.reset();
|
||||
unsigned j = this->m_n();
|
||||
while (j--) {
|
||||
if (this->m_basis_heading[j] >= 0 || !m_can_enter_basis[j]) continue;
|
||||
nb.push_back(j);
|
||||
this->m_basis_heading[j] = - static_cast<int>(nb.size());
|
||||
}
|
||||
}
|
||||
|
||||
void restore_non_basis();
|
||||
|
||||
bool update_basis(int entering, int leaving);
|
||||
|
||||
void recalculate_xB_and_d();
|
||||
|
||||
void recalculate_d();
|
||||
|
||||
void init_betas();
|
||||
|
||||
void adjust_xb_for_changed_xn_and_init_betas();
|
||||
|
||||
void start_with_initial_basis_and_make_it_dual_feasible();
|
||||
|
||||
bool done();
|
||||
|
||||
T get_edge_steepness_for_lower_bound(unsigned p);
|
||||
|
||||
T get_edge_steepness_for_upper_bound(unsigned p);
|
||||
|
||||
T pricing_for_row(unsigned i);
|
||||
|
||||
void pricing_loop(unsigned number_of_rows_to_try, unsigned offset_in_rows);
|
||||
|
||||
bool advance_on_known_p();
|
||||
|
||||
int define_sign_of_alpha_r();
|
||||
|
||||
bool can_be_breakpoint(unsigned j);
|
||||
|
||||
void fill_breakpoint_set();
|
||||
|
||||
void DSE_FTran();
|
||||
T get_delta();
|
||||
|
||||
void restore_d();
|
||||
|
||||
bool d_is_correct();
|
||||
|
||||
void xb_minus_delta_p_pivot_column();
|
||||
|
||||
void update_betas();
|
||||
|
||||
void apply_flips();
|
||||
|
||||
void snap_xN_column_to_bounds(unsigned j);
|
||||
|
||||
void snap_xN_to_bounds();
|
||||
|
||||
void init_beta_precisely(unsigned i);
|
||||
|
||||
void init_betas_precisely();
|
||||
|
||||
// step 7 of the algorithm from Progress
|
||||
bool basis_change_and_update();
|
||||
|
||||
void revert_to_previous_basis();
|
||||
|
||||
non_basic_column_value_position m_entering_boundary_position;
|
||||
bool update_basis_and_x_local(int entering, int leaving, X const & tt);
|
||||
void recover_leaving();
|
||||
|
||||
bool problem_is_dual_feasible() const;
|
||||
|
||||
bool snap_runaway_nonbasic_column(unsigned);
|
||||
|
||||
bool snap_runaway_nonbasic_columns();
|
||||
|
||||
unsigned get_number_of_rows_to_try_for_leaving();
|
||||
|
||||
void update_a_wave(const T & del, unsigned j) {
|
||||
this->m_A.add_column_to_vector(del, j, & m_a_wave[0]);
|
||||
}
|
||||
|
||||
bool delta_keeps_the_sign(int initial_delta_sign, const T & delta);
|
||||
|
||||
void set_status_to_tentative_dual_unbounded_or_dual_unbounded();
|
||||
|
||||
// it is positive if going from low bound to upper bound and negative if going from upper bound to low bound
|
||||
T signed_span_of_boxed(unsigned j) {
|
||||
return this->x_is_at_lower_bound(j)? this->bound_span(j): - this->bound_span(j);
|
||||
}
|
||||
|
||||
void add_tight_breakpoints_and_q_to_flipped_set();
|
||||
|
||||
T delta_lost_on_flips_of_tight_breakpoints();
|
||||
|
||||
bool tight_breakpoinst_are_all_boxed();
|
||||
|
||||
T calculate_harris_delta_on_breakpoint_set();
|
||||
|
||||
void fill_tight_set_on_harris_delta(const T & harris_delta );
|
||||
|
||||
void find_q_on_tight_set();
|
||||
|
||||
void find_q_and_tight_set();
|
||||
|
||||
void erase_tight_breakpoints_and_q_from_breakpoint_set();
|
||||
|
||||
bool ratio_test();
|
||||
|
||||
void process_flipped();
|
||||
void update_d_and_xB();
|
||||
|
||||
void calculate_beta_r_precisely();
|
||||
// see "Progress in the dual simplex method for large scale LP problems: practical dual phase 1 algorithms"
|
||||
|
||||
void update_xb_after_bound_flips();
|
||||
|
||||
void one_iteration();
|
||||
|
||||
void solve();
|
||||
|
||||
bool lower_bounds_are_set() const override { return true; }
|
||||
};
|
||||
}
|
||||
|
|
@ -1,751 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_dual_core_solver.h"
|
||||
|
||||
namespace lp {
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::init_a_wave_by_zeros() {
|
||||
unsigned j = this->m_m();
|
||||
while (j--) {
|
||||
m_a_wave[j] = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::restore_non_basis() {
|
||||
auto & nb = this->m_nbasis;
|
||||
nb.reset();
|
||||
unsigned j = this->m_n();
|
||||
while (j--) {
|
||||
if (this->m_basis_heading[j] >= 0 ) continue;
|
||||
if (m_can_enter_basis[j]) {
|
||||
lp_assert(std::find(nb.begin(), nb.end(), j) == nb.end());
|
||||
nb.push_back(j);
|
||||
this->m_basis_heading[j] = - static_cast<int>(nb.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::update_basis(int entering, int leaving) {
|
||||
// the second argument is the element of the entering column from the pivot row - its value should be equal to the low diagonal element of the bump after all pivoting is done
|
||||
if (this->m_refactor_counter++ < 200) {
|
||||
this->m_factorization->replace_column(this->m_ed[this->m_factorization->basis_heading(leaving)], this->m_w);
|
||||
if (this->m_factorization->get_status() == LU_status::OK) {
|
||||
this->m_factorization->change_basis(entering, leaving);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// need to refactor
|
||||
this->m_factorization->change_basis(entering, leaving);
|
||||
init_factorization(this->m_factorization, this->m_A, this->m_basis, this->m_basis_heading, this->m_settings);
|
||||
this->m_refactor_counter = 0;
|
||||
if (this->m_factorization->get_status() != LU_status::OK) {
|
||||
LP_OUT(this->m_settings, "failing refactor for entering = " << entering << ", leaving = " << leaving << " total_iterations = " << this->total_iterations() << std::endl);
|
||||
this->m_iters_with_no_cost_growing++;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::recalculate_xB_and_d() {
|
||||
this->solve_Ax_eq_b();
|
||||
recalculate_d();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::recalculate_d() {
|
||||
this->solve_yB(this->m_y);
|
||||
this->fill_reduced_costs_from_m_y_by_rows();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::init_betas() {
|
||||
// todo : look at page 194 of Progress in the dual simplex algorithm for solving large scale LP problems : techniques for a fast and stable implementation
|
||||
// the current implementation is not good enough: todo
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
m_betas[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::adjust_xb_for_changed_xn_and_init_betas() {
|
||||
this->solve_Ax_eq_b();
|
||||
init_betas();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::start_with_initial_basis_and_make_it_dual_feasible() {
|
||||
this->set_non_basic_x_to_correct_bounds(); // It is not an efficient version, see 3.29,
|
||||
// however this version does not require that m_x is the solution of Ax = 0 beforehand
|
||||
adjust_xb_for_changed_xn_and_init_betas();
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::done() {
|
||||
if (this->get_status() == lp_status::OPTIMAL) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // todo, need to be more cases
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::get_edge_steepness_for_lower_bound(unsigned p) {
|
||||
lp_assert(this->m_basis_heading[p] >= 0 && static_cast<unsigned>(this->m_basis_heading[p]) < this->m_m());
|
||||
T del = this->m_x[p] - this->m_lower_bounds[p];
|
||||
del *= del;
|
||||
return del / this->m_betas[this->m_basis_heading[p]];
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::get_edge_steepness_for_upper_bound(unsigned p) {
|
||||
lp_assert(this->m_basis_heading[p] >= 0 && static_cast<unsigned>(this->m_basis_heading[p]) < this->m_m());
|
||||
T del = this->m_x[p] - this->m_upper_bounds[p];
|
||||
del *= del;
|
||||
return del / this->m_betas[this->m_basis_heading[p]];
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::pricing_for_row(unsigned i) {
|
||||
unsigned p = this->m_basis[i];
|
||||
switch (this->m_column_types[p]) {
|
||||
case column_type::fixed:
|
||||
case column_type::boxed:
|
||||
if (this->x_below_low_bound(p)) {
|
||||
T del = get_edge_steepness_for_lower_bound(p);
|
||||
return del;
|
||||
}
|
||||
if (this->x_above_upper_bound(p)) {
|
||||
T del = get_edge_steepness_for_upper_bound(p);
|
||||
return del;
|
||||
}
|
||||
return numeric_traits<T>::zero();
|
||||
case column_type::lower_bound:
|
||||
if (this->x_below_low_bound(p)) {
|
||||
T del = get_edge_steepness_for_lower_bound(p);
|
||||
return del;
|
||||
}
|
||||
return numeric_traits<T>::zero();
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
if (this->x_above_upper_bound(p)) {
|
||||
T del = get_edge_steepness_for_upper_bound(p);
|
||||
return del;
|
||||
}
|
||||
return numeric_traits<T>::zero();
|
||||
break;
|
||||
case column_type::free_column:
|
||||
lp_assert(numeric_traits<T>::is_zero(this->m_d[p]));
|
||||
return numeric_traits<T>::zero();
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
lp_unreachable();
|
||||
return numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::pricing_loop(unsigned number_of_rows_to_try, unsigned offset_in_rows) {
|
||||
m_r = -1;
|
||||
T steepest_edge_max = numeric_traits<T>::zero();
|
||||
unsigned initial_offset_in_rows = offset_in_rows;
|
||||
unsigned i = offset_in_rows;
|
||||
unsigned rows_left = number_of_rows_to_try;
|
||||
do {
|
||||
if (m_forbidden_rows.find(i) != m_forbidden_rows.end()) {
|
||||
if (++i == this->m_m()) {
|
||||
i = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
T se = pricing_for_row(i);
|
||||
if (se > steepest_edge_max) {
|
||||
steepest_edge_max = se;
|
||||
m_r = i;
|
||||
if (rows_left > 0) {
|
||||
rows_left--;
|
||||
}
|
||||
}
|
||||
if (++i == this->m_m()) {
|
||||
i = 0;
|
||||
}
|
||||
} while (i != initial_offset_in_rows && rows_left);
|
||||
if (m_r == -1) {
|
||||
if (this->get_status() != lp_status::UNSTABLE) {
|
||||
this->set_status(lp_status::OPTIMAL);
|
||||
}
|
||||
} else {
|
||||
m_p = this->m_basis[m_r];
|
||||
m_delta = get_delta();
|
||||
if (advance_on_known_p()){
|
||||
m_forbidden_rows.clear();
|
||||
return;
|
||||
}
|
||||
// failure in advance_on_known_p
|
||||
if (this->get_status() == lp_status::FLOATING_POINT_ERROR) {
|
||||
return;
|
||||
}
|
||||
this->set_status(lp_status::UNSTABLE);
|
||||
m_forbidden_rows.insert(m_r);
|
||||
}
|
||||
}
|
||||
|
||||
// this calculation is needed for the steepest edge update,
|
||||
// it hijackes m_pivot_row_of_B_1 for this purpose since we will need it anymore to the end of the cycle
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::DSE_FTran() { // todo, see algorithm 7 from page 35
|
||||
this->m_factorization->solve_By_for_T_indexed_only(this->m_pivot_row_of_B_1, this->m_settings);
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::advance_on_known_p() {
|
||||
if (done()) {
|
||||
return true;
|
||||
}
|
||||
this->calculate_pivot_row_of_B_1(m_r);
|
||||
this->calculate_pivot_row_when_pivot_row_of_B1_is_ready(m_r);
|
||||
if (!ratio_test()) {
|
||||
return true;
|
||||
}
|
||||
calculate_beta_r_precisely();
|
||||
this->solve_Bd(m_q); // FTRAN
|
||||
int pivot_compare_result = this->pivots_in_column_and_row_are_different(m_q, m_p);
|
||||
if (!pivot_compare_result){;}
|
||||
else if (pivot_compare_result == 2) { // the sign is changed, cannot continue
|
||||
lp_unreachable(); // not implemented yet
|
||||
} else {
|
||||
lp_assert(pivot_compare_result == 1);
|
||||
this->init_lu();
|
||||
}
|
||||
DSE_FTran();
|
||||
return basis_change_and_update();
|
||||
}
|
||||
|
||||
template <typename T, typename X> int lp_dual_core_solver<T, X>::define_sign_of_alpha_r() {
|
||||
switch (this->m_column_types[m_p]) {
|
||||
case column_type::boxed:
|
||||
case column_type::fixed:
|
||||
if (this->x_below_low_bound(m_p)) {
|
||||
return -1;
|
||||
}
|
||||
if (this->x_above_upper_bound(m_p)) {
|
||||
return 1;
|
||||
}
|
||||
lp_unreachable();
|
||||
case column_type::lower_bound:
|
||||
if (this->x_below_low_bound(m_p)) {
|
||||
return -1;
|
||||
}
|
||||
lp_unreachable();
|
||||
case column_type::upper_bound:
|
||||
if (this->x_above_upper_bound(m_p)) {
|
||||
return 1;
|
||||
}
|
||||
lp_unreachable();
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
lp_unreachable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::can_be_breakpoint(unsigned j) {
|
||||
if (this->pivot_row_element_is_too_small_for_ratio_test(j)) return false;
|
||||
switch (this->m_column_types[j]) {
|
||||
case column_type::lower_bound:
|
||||
lp_assert(this->m_settings.abs_val_is_smaller_than_harris_tolerance(this->m_x[j] - this->m_lower_bounds[j]));
|
||||
return m_sign_of_alpha_r * this->m_pivot_row[j] > 0;
|
||||
case column_type::upper_bound:
|
||||
lp_assert(this->m_settings.abs_val_is_smaller_than_harris_tolerance(this->m_x[j] - this->m_upper_bounds[j]));
|
||||
return m_sign_of_alpha_r * this->m_pivot_row[j] < 0;
|
||||
case column_type::boxed:
|
||||
{
|
||||
bool lower_bound = this->x_is_at_lower_bound(j);
|
||||
bool grawing = m_sign_of_alpha_r * this->m_pivot_row[j] > 0;
|
||||
return lower_bound == grawing;
|
||||
}
|
||||
case column_type::fixed: // is always dual feasible so we ignore it
|
||||
return false;
|
||||
case column_type::free_column:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::fill_breakpoint_set() {
|
||||
m_breakpoint_set.clear();
|
||||
for (unsigned j : this->non_basis()) {
|
||||
if (can_be_breakpoint(j)) {
|
||||
m_breakpoint_set.insert(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// template <typename T, typename X> void lp_dual_core_solver<T, X>::FTran() {
|
||||
// this->solve_Bd(m_q);
|
||||
// }
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::get_delta() {
|
||||
switch (this->m_column_types[m_p]) {
|
||||
case column_type::boxed:
|
||||
if (this->x_below_low_bound(m_p)) {
|
||||
return this->m_x[m_p] - this->m_lower_bounds[m_p];
|
||||
}
|
||||
if (this->x_above_upper_bound(m_p)) {
|
||||
return this->m_x[m_p] - this->m_upper_bounds[m_p];
|
||||
}
|
||||
lp_unreachable();
|
||||
case column_type::lower_bound:
|
||||
if (this->x_below_low_bound(m_p)) {
|
||||
return this->m_x[m_p] - this->m_lower_bounds[m_p];
|
||||
}
|
||||
lp_unreachable();
|
||||
case column_type::upper_bound:
|
||||
if (this->x_above_upper_bound(m_p)) {
|
||||
return get_edge_steepness_for_upper_bound(m_p);
|
||||
}
|
||||
lp_unreachable();
|
||||
case column_type::fixed:
|
||||
return this->m_x[m_p] - this->m_upper_bounds[m_p];
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
lp_unreachable();
|
||||
return zero_of_type<T>();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::restore_d() {
|
||||
this->m_d[m_p] = numeric_traits<T>::zero();
|
||||
for (auto j : this->non_basis()) {
|
||||
this->m_d[j] += m_theta_D * this->m_pivot_row[j];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::d_is_correct() {
|
||||
this->solve_yB(this->m_y);
|
||||
for (auto j : this->non_basis()) {
|
||||
T d = this->m_costs[j] - this->m_A.dot_product_with_column(this->m_y, j);
|
||||
if (numeric_traits<T>::get_double(abs(d - this->m_d[j])) >= 0.001) {
|
||||
LP_OUT(this->m_settings, "total_iterations = " << this->total_iterations() << std::endl
|
||||
<< "d[" << j << "] = " << this->m_d[j] << " but should be " << d << std::endl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::xb_minus_delta_p_pivot_column() {
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
this->m_x[this->m_basis[i]] -= m_theta_P * this->m_ed[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::update_betas() { // page 194 of Progress ... todo - once in a while betas have to be reinitialized
|
||||
T one_over_arq = numeric_traits<T>::one() / this->m_pivot_row[m_q];
|
||||
T beta_r = this->m_betas[m_r] = std::max(T(0.0001), (m_betas[m_r] * one_over_arq) * one_over_arq);
|
||||
T k = -2 * one_over_arq;
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
if (static_cast<int>(i) == m_r) continue;
|
||||
T a = this->m_ed[i];
|
||||
m_betas[i] += a * (a * beta_r + k * this->m_pivot_row_of_B_1[i]);
|
||||
if (m_betas[i] < T(0.0001))
|
||||
m_betas[i] = T(0.0001);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::apply_flips() {
|
||||
for (unsigned j : m_flipped_boxed) {
|
||||
lp_assert(this->x_is_at_bound(j));
|
||||
if (this->x_is_at_lower_bound(j)) {
|
||||
this->m_x[j] = this->m_upper_bounds[j];
|
||||
} else {
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::snap_xN_column_to_bounds(unsigned j) {
|
||||
switch (this->m_column_type[j]) {
|
||||
case column_type::fixed:
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::boxed:
|
||||
if (this->x_is_at_lower_bound(j)) {
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
} else {
|
||||
this->m_x[j] = this->m_upper_bounds[j];
|
||||
}
|
||||
break;
|
||||
case column_type::lower_bound:
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
this->m_x[j] = this->m_upper_bounds[j];
|
||||
break;
|
||||
case column_type::free_column:
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::snap_xN_to_bounds() {
|
||||
for (auto j : this->non_basis()) {
|
||||
snap_xN_column_to_bounds(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::init_beta_precisely(unsigned i) {
|
||||
vector<T> vec(this->m_m(), numeric_traits<T>::zero());
|
||||
vec[i] = numeric_traits<T>::one();
|
||||
this->m_factorization->solve_yB_with_error_check(vec, this->m_basis);
|
||||
T beta = numeric_traits<T>::zero();
|
||||
for (T & v : vec) {
|
||||
beta += v * v;
|
||||
}
|
||||
this->m_betas[i] =beta;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::init_betas_precisely() {
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
init_beta_precisely(i);
|
||||
}
|
||||
}
|
||||
|
||||
// step 7 of the algorithm from Progress
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::basis_change_and_update() {
|
||||
update_betas();
|
||||
update_d_and_xB();
|
||||
// m_theta_P = m_delta / this->m_ed[m_r];
|
||||
m_theta_P = m_delta / this->m_pivot_row[m_q];
|
||||
// xb_minus_delta_p_pivot_column();
|
||||
apply_flips();
|
||||
if (!this->update_basis_and_x(m_q, m_p, m_theta_P)) {
|
||||
init_betas_precisely();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (snap_runaway_nonbasic_column(m_p)) {
|
||||
if (!this->find_x_by_solving()) {
|
||||
revert_to_previous_basis();
|
||||
this->iters_with_no_cost_growing()++;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!problem_is_dual_feasible()) {
|
||||
// todo : shift the costs!!!!
|
||||
revert_to_previous_basis();
|
||||
this->iters_with_no_cost_growing()++;
|
||||
return false;
|
||||
}
|
||||
|
||||
lp_assert(d_is_correct());
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::recover_leaving() {
|
||||
switch (m_entering_boundary_position) {
|
||||
case at_lower_bound:
|
||||
case at_fixed:
|
||||
this->m_x[m_q] = this->m_lower_bounds[m_q];
|
||||
break;
|
||||
case at_upper_bound:
|
||||
this->m_x[m_q] = this->m_upper_bounds[m_q];
|
||||
break;
|
||||
case free_of_bounds:
|
||||
this->m_x[m_q] = zero_of_type<X>();
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::revert_to_previous_basis() {
|
||||
LP_OUT(this->m_settings, "revert to previous basis on ( " << m_p << ", " << m_q << ")" << std::endl);
|
||||
this->change_basis_unconditionally(m_p, m_q);
|
||||
init_factorization(this->m_factorization, this->m_A, this->m_basis, this->m_settings);
|
||||
if (this->m_factorization->get_status() != LU_status::OK) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR); // complete failure
|
||||
return;
|
||||
}
|
||||
recover_leaving();
|
||||
if (!this->find_x_by_solving()) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR);
|
||||
return;
|
||||
}
|
||||
recalculate_xB_and_d();
|
||||
init_betas_precisely();
|
||||
}
|
||||
|
||||
// returns true if the column has been snapped
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::snap_runaway_nonbasic_column(unsigned j) {
|
||||
switch (this->m_column_types[j]) {
|
||||
case column_type::fixed:
|
||||
case column_type::lower_bound:
|
||||
if (!this->x_is_at_lower_bound(j)) {
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case column_type::boxed:
|
||||
{
|
||||
bool closer_to_lower_bound = abs(this->m_lower_bounds[j] - this->m_x[j]) < abs(this->m_upper_bounds[j] - this->m_x[j]);
|
||||
if (closer_to_lower_bound) {
|
||||
if (!this->x_is_at_lower_bound(j)) {
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (!this->x_is_at_upper_bound(j)) {
|
||||
this->m_x[j] = this->m_lower_bounds[j];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case column_type::upper_bound:
|
||||
if (!this->x_is_at_upper_bound(j)) {
|
||||
this->m_x[j] = this->m_upper_bounds[j];
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::problem_is_dual_feasible() const {
|
||||
for (unsigned j : this->non_basis()){
|
||||
if (!this->column_is_dual_feasible(j)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> unsigned lp_dual_core_solver<T, X>::get_number_of_rows_to_try_for_leaving() {
|
||||
unsigned s = this->m_m();
|
||||
if (this->m_m() > 300) {
|
||||
s = (unsigned)((s / 100.0) * this->m_settings.percent_of_entering_to_check);
|
||||
}
|
||||
return this->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) {
|
||||
if (numeric_traits<T>::precise())
|
||||
return ((delta > numeric_traits<T>::zero()) && (initial_delta_sign == 1)) ||
|
||||
((delta < numeric_traits<T>::zero()) && (initial_delta_sign == -1));
|
||||
|
||||
double del = numeric_traits<T>::get_double(delta);
|
||||
return ( (del > this->m_settings.zero_tolerance) && (initial_delta_sign == 1)) ||
|
||||
((del < - this->m_settings.zero_tolerance) && (initial_delta_sign == -1));
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::set_status_to_tentative_dual_unbounded_or_dual_unbounded() {
|
||||
if (this->get_status() == lp_status::TENTATIVE_DUAL_UNBOUNDED) {
|
||||
this->set_status(lp_status::DUAL_UNBOUNDED);
|
||||
} else {
|
||||
this->set_status(lp_status::TENTATIVE_DUAL_UNBOUNDED);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::add_tight_breakpoints_and_q_to_flipped_set() {
|
||||
m_flipped_boxed.insert(m_q);
|
||||
for (auto j : m_tight_set) {
|
||||
m_flipped_boxed.insert(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::delta_lost_on_flips_of_tight_breakpoints() {
|
||||
T ret = abs(this->bound_span(m_q) * this->m_pivot_row[m_q]);
|
||||
for (auto j : m_tight_set) {
|
||||
ret += abs(this->bound_span(j) * this->m_pivot_row[j]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::tight_breakpoinst_are_all_boxed() {
|
||||
if (this->m_column_types[m_q] != column_type::boxed) return false;
|
||||
for (auto j : m_tight_set) {
|
||||
if (this->m_column_types[j] != column_type::boxed) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_dual_core_solver<T, X>::calculate_harris_delta_on_breakpoint_set() {
|
||||
bool first_time = true;
|
||||
T ret = zero_of_type<T>();
|
||||
lp_assert(m_breakpoint_set.size() > 0);
|
||||
for (auto j : m_breakpoint_set) {
|
||||
T t;
|
||||
if (this->x_is_at_lower_bound(j)) {
|
||||
t = abs((std::max(this->m_d[j], numeric_traits<T>::zero()) + m_harris_tolerance) / this->m_pivot_row[j]);
|
||||
} else {
|
||||
t = abs((std::min(this->m_d[j], numeric_traits<T>::zero()) - m_harris_tolerance) / this->m_pivot_row[j]);
|
||||
}
|
||||
if (first_time) {
|
||||
ret = t;
|
||||
first_time = false;
|
||||
} else if (t < ret) {
|
||||
ret = t;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::fill_tight_set_on_harris_delta(const T & harris_delta ){
|
||||
m_tight_set.clear();
|
||||
for (auto j : m_breakpoint_set) {
|
||||
if (this->x_is_at_lower_bound(j)) {
|
||||
if (abs(std::max(this->m_d[j], numeric_traits<T>::zero()) / this->m_pivot_row[j]) <= harris_delta){
|
||||
m_tight_set.insert(j);
|
||||
}
|
||||
} else {
|
||||
if (abs(std::min(this->m_d[j], numeric_traits<T>::zero() ) / this->m_pivot_row[j]) <= harris_delta){
|
||||
m_tight_set.insert(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::find_q_on_tight_set() {
|
||||
m_q = -1;
|
||||
T max_pivot;
|
||||
for (auto j : m_tight_set) {
|
||||
T r = abs(this->m_pivot_row[j]);
|
||||
if (m_q != -1) {
|
||||
if (r > max_pivot) {
|
||||
max_pivot = r;
|
||||
m_q = j;
|
||||
}
|
||||
} else {
|
||||
max_pivot = r;
|
||||
m_q = j;
|
||||
}
|
||||
}
|
||||
m_tight_set.erase(m_q);
|
||||
lp_assert(m_q != -1);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::find_q_and_tight_set() {
|
||||
T harris_del = calculate_harris_delta_on_breakpoint_set();
|
||||
fill_tight_set_on_harris_delta(harris_del);
|
||||
find_q_on_tight_set();
|
||||
m_entering_boundary_position = this->get_non_basic_column_value_position(m_q);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::erase_tight_breakpoints_and_q_from_breakpoint_set() {
|
||||
m_breakpoint_set.erase(m_q);
|
||||
for (auto j : m_tight_set) {
|
||||
m_breakpoint_set.erase(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_dual_core_solver<T, X>::ratio_test() {
|
||||
m_sign_of_alpha_r = define_sign_of_alpha_r();
|
||||
fill_breakpoint_set();
|
||||
m_flipped_boxed.clear();
|
||||
int initial_delta_sign = m_delta >= numeric_traits<T>::zero()? 1: -1;
|
||||
do {
|
||||
if (m_breakpoint_set.empty()) {
|
||||
set_status_to_tentative_dual_unbounded_or_dual_unbounded();
|
||||
return false;
|
||||
}
|
||||
this->set_status(lp_status::FEASIBLE);
|
||||
find_q_and_tight_set();
|
||||
if (!tight_breakpoinst_are_all_boxed()) break;
|
||||
T del = m_delta - delta_lost_on_flips_of_tight_breakpoints() * initial_delta_sign;
|
||||
if (!delta_keeps_the_sign(initial_delta_sign, del)) break;
|
||||
if (m_tight_set.size() + 1 == m_breakpoint_set.size()) {
|
||||
break; // deciding not to flip since we might get stuck without finding m_q, the column entering the basis
|
||||
}
|
||||
// we can flip m_q together with the tight set and look for another breakpoint candidate for m_q and another tight set
|
||||
add_tight_breakpoints_and_q_to_flipped_set();
|
||||
m_delta = del;
|
||||
erase_tight_breakpoints_and_q_from_breakpoint_set();
|
||||
} while (true);
|
||||
m_theta_D = this->m_d[m_q] / this->m_pivot_row[m_q];
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::process_flipped() {
|
||||
init_a_wave_by_zeros();
|
||||
for (auto j : m_flipped_boxed) {
|
||||
update_a_wave(signed_span_of_boxed(j), j);
|
||||
}
|
||||
}
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::update_d_and_xB() {
|
||||
for (auto j : this->non_basis()) {
|
||||
this->m_d[j] -= m_theta_D * this->m_pivot_row[j];
|
||||
}
|
||||
this->m_d[m_p] = - m_theta_D;
|
||||
if (!m_flipped_boxed.empty()) {
|
||||
process_flipped();
|
||||
update_xb_after_bound_flips();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::calculate_beta_r_precisely() {
|
||||
T t = numeric_traits<T>::zero();
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
T b = this->m_pivot_row_of_B_1[i];
|
||||
t += b * b;
|
||||
}
|
||||
m_betas[m_r] = t;
|
||||
}
|
||||
// see "Progress in the dual simplex method for large scale LP problems: practical dual phase 1 algorithms"
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::update_xb_after_bound_flips() {
|
||||
this->m_factorization->solve_By(m_a_wave);
|
||||
unsigned i = this->m_m();
|
||||
while (i--) {
|
||||
this->m_x[this->m_basis[i]] -= m_a_wave[i];
|
||||
}
|
||||
}
|
||||
|
||||
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 offset_in_rows = this->m_settings.random_next() % this->m_m();
|
||||
if (this->get_status() == lp_status::TENTATIVE_DUAL_UNBOUNDED) {
|
||||
number_of_rows_to_try = this->m_m();
|
||||
} else {
|
||||
this->set_status(lp_status::FEASIBLE);
|
||||
}
|
||||
pricing_loop(number_of_rows_to_try, offset_in_rows);
|
||||
lp_assert(problem_is_dual_feasible());
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_core_solver<T, X>::solve() { // see the page 35
|
||||
lp_assert(d_is_correct());
|
||||
lp_assert(problem_is_dual_feasible());
|
||||
lp_assert(this->basis_heading_is_correct());
|
||||
//this->set_total_iterations(0);
|
||||
this->iters_with_no_cost_growing() = 0;
|
||||
do {
|
||||
if (this->print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over("", *this->m_settings.get_message_ostream())){
|
||||
return;
|
||||
}
|
||||
one_iteration();
|
||||
} while (this->get_status() != lp_status::FLOATING_POINT_ERROR && this->get_status() != lp_status::DUAL_UNBOUNDED && this->get_status() != lp_status::OPTIMAL &&
|
||||
this->iters_with_no_cost_growing() <= this->m_settings.max_number_of_iterations_with_no_improvements
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include "math/lp/lp_dual_simplex_def.h"
|
||||
template lp::mpq lp::lp_dual_simplex<lp::mpq, lp::mpq>::get_current_cost() const;
|
||||
template void lp::lp_dual_simplex<lp::mpq, lp::mpq>::find_maximal_solution();
|
||||
template double lp::lp_dual_simplex<double, double>::get_current_cost() const;
|
||||
template void lp::lp_dual_simplex<double, double>::find_maximal_solution();
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/lp_solver.h"
|
||||
#include "math/lp/lp_dual_core_solver.h"
|
||||
namespace lp {
|
||||
|
||||
template <typename T, typename X>
|
||||
class lp_dual_simplex: public lp_solver<T, X> {
|
||||
lp_dual_core_solver<T, X> * m_core_solver;
|
||||
vector<T> m_b_copy;
|
||||
vector<T> m_lower_bounds; // We don't have a convention here that all low bounds are zeros. At least it does not hold for the first stage solver
|
||||
vector<column_type> m_column_types_of_core_solver;
|
||||
vector<column_type> m_column_types_of_logicals;
|
||||
vector<bool> m_can_enter_basis;
|
||||
public:
|
||||
~lp_dual_simplex() override {
|
||||
delete m_core_solver;
|
||||
}
|
||||
|
||||
lp_dual_simplex() : m_core_solver(nullptr) {}
|
||||
|
||||
|
||||
void decide_on_status_after_stage1();
|
||||
|
||||
void fix_logical_for_stage2(unsigned j);
|
||||
|
||||
void fix_structural_for_stage2(unsigned j);
|
||||
|
||||
void unmark_boxed_and_fixed_columns_and_fix_structural_costs();
|
||||
|
||||
void restore_right_sides();
|
||||
|
||||
void solve_for_stage2();
|
||||
|
||||
void fill_x_with_zeros();
|
||||
|
||||
void stage1();
|
||||
|
||||
void stage2();
|
||||
|
||||
void fill_first_stage_solver_fields();
|
||||
|
||||
column_type get_column_type(unsigned j);
|
||||
|
||||
void fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_structural_column(unsigned j);
|
||||
|
||||
void fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_logical_column(unsigned j);
|
||||
|
||||
void fill_costs_and_bounds_and_column_types_for_the_first_stage_solver();
|
||||
|
||||
void set_type_for_logical(unsigned j, column_type col_type) {
|
||||
this->m_column_types_of_logicals[j - this->number_of_core_structurals()] = col_type;
|
||||
}
|
||||
|
||||
void fill_first_stage_solver_fields_for_row_slack_and_artificial(unsigned row,
|
||||
unsigned & slack_var,
|
||||
unsigned & artificial);
|
||||
|
||||
void augment_matrix_A_and_fill_x_and_allocate_some_fields();
|
||||
|
||||
|
||||
|
||||
void copy_m_b_aside_and_set_it_to_zeros();
|
||||
|
||||
void find_maximal_solution() override;
|
||||
|
||||
T get_column_value(unsigned column) const override {
|
||||
return this->get_column_value_with_core_solver(column, m_core_solver);
|
||||
}
|
||||
|
||||
T get_current_cost() const override;
|
||||
};
|
||||
}
|
||||
|
|
@ -1,376 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "math/lp/lp_dual_simplex.h"
|
||||
namespace lp{
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::decide_on_status_after_stage1() {
|
||||
switch (m_core_solver->get_status()) {
|
||||
case lp_status::OPTIMAL:
|
||||
if (this->m_settings.abs_val_is_smaller_than_artificial_tolerance(m_core_solver->get_cost())) {
|
||||
this->m_status = lp_status::FEASIBLE;
|
||||
} else {
|
||||
this->m_status = lp_status::UNBOUNDED;
|
||||
}
|
||||
break;
|
||||
case lp_status::DUAL_UNBOUNDED:
|
||||
lp_unreachable();
|
||||
case lp_status::TIME_EXHAUSTED:
|
||||
this->m_status = lp_status::TIME_EXHAUSTED;
|
||||
break;
|
||||
case lp_status::FLOATING_POINT_ERROR:
|
||||
this->m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fix_logical_for_stage2(unsigned j) {
|
||||
lp_assert(j >= this->number_of_core_structurals());
|
||||
switch (m_column_types_of_logicals[j - this->number_of_core_structurals()]) {
|
||||
case column_type::lower_bound:
|
||||
m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
m_column_types_of_core_solver[j] = column_type::lower_bound;
|
||||
m_can_enter_basis[j] = true;
|
||||
break;
|
||||
case column_type::fixed:
|
||||
this->m_upper_bounds[j] = m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
m_column_types_of_core_solver[j] = column_type::fixed;
|
||||
m_can_enter_basis[j] = false;
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fix_structural_for_stage2(unsigned j) {
|
||||
column_info<T> * ci = this->m_map_from_var_index_to_column_info[this->m_core_solver_columns_to_external_columns[j]];
|
||||
switch (ci->get_column_type()) {
|
||||
case column_type::lower_bound:
|
||||
m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
m_column_types_of_core_solver[j] = column_type::lower_bound;
|
||||
m_can_enter_basis[j] = true;
|
||||
break;
|
||||
case column_type::fixed:
|
||||
case column_type::upper_bound:
|
||||
lp_unreachable();
|
||||
case column_type::boxed:
|
||||
this->m_upper_bounds[j] = ci->get_adjusted_upper_bound() / this->m_column_scale[j];
|
||||
m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
m_column_types_of_core_solver[j] = column_type::boxed;
|
||||
m_can_enter_basis[j] = true;
|
||||
break;
|
||||
case column_type::free_column:
|
||||
m_can_enter_basis[j] = true;
|
||||
m_column_types_of_core_solver[j] = column_type::free_column;
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
// T cost_was = this->m_costs[j];
|
||||
this->set_scaled_cost(j);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::unmark_boxed_and_fixed_columns_and_fix_structural_costs() {
|
||||
unsigned j = this->m_A->column_count();
|
||||
while (j-- > this->number_of_core_structurals()) {
|
||||
fix_logical_for_stage2(j);
|
||||
}
|
||||
j = this->number_of_core_structurals();
|
||||
while (j--) {
|
||||
fix_structural_for_stage2(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::restore_right_sides() {
|
||||
unsigned i = this->m_A->row_count();
|
||||
while (i--) {
|
||||
this->m_b[i] = m_b_copy[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::solve_for_stage2() {
|
||||
m_core_solver->restore_non_basis();
|
||||
m_core_solver->solve_yB(m_core_solver->m_y);
|
||||
m_core_solver->fill_reduced_costs_from_m_y_by_rows();
|
||||
m_core_solver->start_with_initial_basis_and_make_it_dual_feasible();
|
||||
m_core_solver->set_status(lp_status::FEASIBLE);
|
||||
m_core_solver->solve();
|
||||
switch (m_core_solver->get_status()) {
|
||||
case lp_status::OPTIMAL:
|
||||
this->m_status = lp_status::OPTIMAL;
|
||||
break;
|
||||
case lp_status::DUAL_UNBOUNDED:
|
||||
this->m_status = lp_status::INFEASIBLE;
|
||||
break;
|
||||
case lp_status::TIME_EXHAUSTED:
|
||||
this->m_status = lp_status::TIME_EXHAUSTED;
|
||||
break;
|
||||
case lp_status::FLOATING_POINT_ERROR:
|
||||
this->m_status = lp_status::FLOATING_POINT_ERROR;
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
this->m_second_stage_iterations = m_core_solver->total_iterations();
|
||||
this->m_total_iterations = (this->m_first_stage_iterations + this->m_second_stage_iterations);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_x_with_zeros() {
|
||||
unsigned j = this->m_A->column_count();
|
||||
while (j--) {
|
||||
this->m_x[j] = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::stage1() {
|
||||
lp_assert(m_core_solver == nullptr);
|
||||
this->m_x.resize(this->m_A->column_count(), numeric_traits<T>::zero());
|
||||
if (this->m_settings.get_message_ostream() != nullptr)
|
||||
this->print_statistics_on_A(*this->m_settings.get_message_ostream());
|
||||
m_core_solver = new lp_dual_core_solver<T, X>(
|
||||
*this->m_A,
|
||||
m_can_enter_basis,
|
||||
this->m_b, // the right side vector
|
||||
this->m_x,
|
||||
this->m_basis,
|
||||
this->m_nbasis,
|
||||
this->m_heading,
|
||||
this->m_costs,
|
||||
this->m_column_types_of_core_solver,
|
||||
this->m_lower_bounds,
|
||||
this->m_upper_bounds,
|
||||
this->m_settings,
|
||||
*this);
|
||||
m_core_solver->fill_reduced_costs_from_m_y_by_rows();
|
||||
m_core_solver->start_with_initial_basis_and_make_it_dual_feasible();
|
||||
if (this->m_settings.abs_val_is_smaller_than_artificial_tolerance(m_core_solver->get_cost())) {
|
||||
// skipping stage 1
|
||||
m_core_solver->set_status(lp_status::OPTIMAL);
|
||||
m_core_solver->set_total_iterations(0);
|
||||
} else {
|
||||
m_core_solver->solve();
|
||||
}
|
||||
decide_on_status_after_stage1();
|
||||
this->m_first_stage_iterations = m_core_solver->total_iterations();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::stage2() {
|
||||
unmark_boxed_and_fixed_columns_and_fix_structural_costs();
|
||||
restore_right_sides();
|
||||
solve_for_stage2();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_first_stage_solver_fields() {
|
||||
unsigned slack_var = this->number_of_core_structurals();
|
||||
unsigned artificial = this->number_of_core_structurals() + this->m_slacks;
|
||||
|
||||
for (unsigned row = 0; row < this->row_count(); row++) {
|
||||
fill_first_stage_solver_fields_for_row_slack_and_artificial(row, slack_var, artificial);
|
||||
}
|
||||
fill_costs_and_bounds_and_column_types_for_the_first_stage_solver();
|
||||
}
|
||||
|
||||
template <typename T, typename X> column_type lp_dual_simplex<T, X>::get_column_type(unsigned j) {
|
||||
lp_assert(j < this->m_A->column_count());
|
||||
if (j >= this->number_of_core_structurals()) {
|
||||
return m_column_types_of_logicals[j - this->number_of_core_structurals()];
|
||||
}
|
||||
return this->m_map_from_var_index_to_column_info[this->m_core_solver_columns_to_external_columns[j]]->get_column_type();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_structural_column(unsigned j) {
|
||||
// see 4.7 in the dissertation of Achim Koberstein
|
||||
lp_assert(this->m_core_solver_columns_to_external_columns.find(j) !=
|
||||
this->m_core_solver_columns_to_external_columns.end());
|
||||
|
||||
T free_bound = T(1e4); // see 4.8
|
||||
unsigned jj = this->m_core_solver_columns_to_external_columns[j];
|
||||
lp_assert(this->m_map_from_var_index_to_column_info.find(jj) != this->m_map_from_var_index_to_column_info.end());
|
||||
column_info<T> * ci = this->m_map_from_var_index_to_column_info[jj];
|
||||
switch (ci->get_column_type()) {
|
||||
case column_type::upper_bound: {
|
||||
std::stringstream s;
|
||||
s << "unexpected bound type " << j << " "
|
||||
<< column_type_to_string(get_column_type(j));
|
||||
throw_exception(s.str());
|
||||
break;
|
||||
}
|
||||
case column_type::lower_bound: {
|
||||
m_can_enter_basis[j] = true;
|
||||
this->set_scaled_cost(j);
|
||||
this->m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
this->m_upper_bounds[j] = numeric_traits<T>::one();
|
||||
break;
|
||||
}
|
||||
case column_type::free_column: {
|
||||
m_can_enter_basis[j] = true;
|
||||
this->set_scaled_cost(j);
|
||||
this->m_upper_bounds[j] = free_bound;
|
||||
this->m_lower_bounds[j] = -free_bound;
|
||||
break;
|
||||
}
|
||||
case column_type::boxed:
|
||||
m_can_enter_basis[j] = false;
|
||||
this->m_costs[j] = numeric_traits<T>::zero();
|
||||
this->m_upper_bounds[j] = this->m_lower_bounds[j] = numeric_traits<T>::zero(); // is it needed?
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
m_column_types_of_core_solver[j] = column_type::boxed;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_logical_column(unsigned j) {
|
||||
this->m_costs[j] = 0;
|
||||
lp_assert(get_column_type(j) != column_type::upper_bound);
|
||||
if ((m_can_enter_basis[j] = (get_column_type(j) == column_type::lower_bound))) {
|
||||
m_column_types_of_core_solver[j] = column_type::boxed;
|
||||
this->m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
this->m_upper_bounds[j] = numeric_traits<T>::one();
|
||||
} else {
|
||||
m_column_types_of_core_solver[j] = column_type::fixed;
|
||||
this->m_lower_bounds[j] = numeric_traits<T>::zero();
|
||||
this->m_upper_bounds[j] = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_costs_and_bounds_and_column_types_for_the_first_stage_solver() {
|
||||
unsigned j = this->m_A->column_count();
|
||||
while (j-- > this->number_of_core_structurals()) { // go over logicals here
|
||||
fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_logical_column(j);
|
||||
}
|
||||
j = this->number_of_core_structurals();
|
||||
while (j--) {
|
||||
fill_costs_bounds_types_and_can_enter_basis_for_the_first_stage_solver_structural_column(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::fill_first_stage_solver_fields_for_row_slack_and_artificial(unsigned row,
|
||||
unsigned & slack_var,
|
||||
unsigned & artificial) {
|
||||
lp_assert(row < this->row_count());
|
||||
auto & constraint = this->m_constraints[this->m_core_solver_rows_to_external_rows[row]];
|
||||
// we need to bring the program to the form Ax = b
|
||||
T rs = this->m_b[row];
|
||||
switch (constraint.m_relation) {
|
||||
case Equal: // no slack variable here
|
||||
set_type_for_logical(artificial, column_type::fixed);
|
||||
this->m_basis[row] = artificial;
|
||||
this->m_costs[artificial] = numeric_traits<T>::zero();
|
||||
(*this->m_A)(row, artificial) = numeric_traits<T>::one();
|
||||
artificial++;
|
||||
break;
|
||||
|
||||
case Greater_or_equal:
|
||||
set_type_for_logical(slack_var, column_type::lower_bound);
|
||||
(*this->m_A)(row, slack_var) = - numeric_traits<T>::one();
|
||||
if (rs > 0) {
|
||||
// adding one artificial
|
||||
set_type_for_logical(artificial, column_type::fixed);
|
||||
(*this->m_A)(row, artificial) = numeric_traits<T>::one();
|
||||
this->m_basis[row] = artificial;
|
||||
this->m_costs[artificial] = numeric_traits<T>::zero();
|
||||
artificial++;
|
||||
} else {
|
||||
// we can put a slack_var into the basis, and avoid adding an artificial variable
|
||||
this->m_basis[row] = slack_var;
|
||||
this->m_costs[slack_var] = numeric_traits<T>::zero();
|
||||
}
|
||||
slack_var++;
|
||||
break;
|
||||
case Less_or_equal:
|
||||
// introduce a non-negative slack variable
|
||||
set_type_for_logical(slack_var, column_type::lower_bound);
|
||||
(*this->m_A)(row, slack_var) = numeric_traits<T>::one();
|
||||
if (rs < 0) {
|
||||
// adding one artificial
|
||||
set_type_for_logical(artificial, column_type::fixed);
|
||||
(*this->m_A)(row, artificial) = - numeric_traits<T>::one();
|
||||
this->m_basis[row] = artificial;
|
||||
this->m_costs[artificial] = numeric_traits<T>::zero();
|
||||
artificial++;
|
||||
} else {
|
||||
// we can put slack_var into the basis, and avoid adding an artificial variable
|
||||
this->m_basis[row] = slack_var;
|
||||
this->m_costs[slack_var] = numeric_traits<T>::zero();
|
||||
}
|
||||
slack_var++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::augment_matrix_A_and_fill_x_and_allocate_some_fields() {
|
||||
this->count_slacks_and_artificials();
|
||||
this->m_A->add_columns_at_the_end(this->m_slacks + this->m_artificials);
|
||||
unsigned n = this->m_A->column_count();
|
||||
this->m_column_types_of_core_solver.resize(n);
|
||||
m_column_types_of_logicals.resize(this->m_slacks + this->m_artificials);
|
||||
this->m_costs.resize(n);
|
||||
this->m_upper_bounds.resize(n);
|
||||
this->m_lower_bounds.resize(n);
|
||||
m_can_enter_basis.resize(n);
|
||||
this->m_basis.resize(this->m_A->row_count());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::copy_m_b_aside_and_set_it_to_zeros() {
|
||||
for (unsigned i = 0; i < this->m_b.size(); i++) {
|
||||
m_b_copy.push_back(this->m_b[i]);
|
||||
this->m_b[i] = numeric_traits<T>::zero(); // preparing for the first stage
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_dual_simplex<T, X>::find_maximal_solution(){
|
||||
if (this->problem_is_empty()) {
|
||||
this->m_status = lp_status::EMPTY;
|
||||
return;
|
||||
}
|
||||
|
||||
this->flip_costs(); // do it for now, todo ( remove the flipping)
|
||||
|
||||
this->cleanup();
|
||||
if (this->m_status == lp_status::INFEASIBLE) {
|
||||
return;
|
||||
}
|
||||
this->fill_matrix_A_and_init_right_side();
|
||||
this->fill_m_b();
|
||||
this->scale();
|
||||
augment_matrix_A_and_fill_x_and_allocate_some_fields();
|
||||
fill_first_stage_solver_fields();
|
||||
copy_m_b_aside_and_set_it_to_zeros();
|
||||
stage1();
|
||||
if (this->m_status == lp_status::FEASIBLE) {
|
||||
stage2();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> T lp_dual_simplex<T, X>::get_current_cost() const {
|
||||
T ret = numeric_traits<T>::zero();
|
||||
for (auto it : this->m_map_from_var_index_to_column_info) {
|
||||
ret += this->get_column_cost_value(it.first, it.second);
|
||||
}
|
||||
return -ret; // we flip costs for now
|
||||
}
|
||||
}
|
||||
|
|
@ -27,17 +27,11 @@ Revision History:
|
|||
#include "math/lp/lp_primal_core_solver_tableau_def.h"
|
||||
namespace lp {
|
||||
|
||||
template void lp_primal_core_solver<double, double>::find_feasible_solution();
|
||||
template void lp::lp_primal_core_solver<lp::mpq, lp::numeric_pair<lp::mpq> >::find_feasible_solution();
|
||||
|
||||
template unsigned lp_primal_core_solver<double, double>::solve();
|
||||
template unsigned lp_primal_core_solver<double, double>::solve_with_tableau();
|
||||
template unsigned lp_primal_core_solver<mpq, mpq>::solve();
|
||||
template unsigned lp_primal_core_solver<mpq, numeric_pair<mpq> >::solve();
|
||||
template void lp::lp_primal_core_solver<double, double>::clear_breakpoints();
|
||||
template bool lp::lp_primal_core_solver<lp::mpq, lp::mpq>::update_basis_and_x_tableau(int, int, lp::mpq const&);
|
||||
template bool lp::lp_primal_core_solver<double, double>::update_basis_and_x_tableau(int, int, double const&);
|
||||
template bool lp::lp_primal_core_solver<lp::mpq, lp::numeric_pair<lp::mpq> >::update_basis_and_x_tableau(int, int, lp::numeric_pair<lp::mpq> const&);
|
||||
template void lp::lp_primal_core_solver<rational, lp::numeric_pair<rational> >::update_inf_cost_for_column_tableau(unsigned);
|
||||
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -43,19 +43,11 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::advance_on_e
|
|||
}
|
||||
advance_on_entering_and_leaving_tableau(entering, leaving, t);
|
||||
}
|
||||
/*
|
||||
template <typename T, typename X> int lp_primal_core_solver<T, X>::choose_entering_column_tableau_rows() {
|
||||
int i = find_inf_row();
|
||||
if (i == -1)
|
||||
return -1;
|
||||
return find_shortest_beneficial_column_in_row(i);
|
||||
}
|
||||
*/
|
||||
|
||||
template <typename T, typename X> int lp_primal_core_solver<T, X>::choose_entering_column_tableau() {
|
||||
//this moment m_y = cB * B(-1)
|
||||
unsigned number_of_benefitial_columns_to_go_over = get_number_of_non_basic_column_to_try_for_enter();
|
||||
|
||||
lp_assert(numeric_traits<T>::precise());
|
||||
if (number_of_benefitial_columns_to_go_over == 0)
|
||||
return -1;
|
||||
if (this->m_basis_sort_counter == 0) {
|
||||
|
|
@ -88,31 +80,23 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::choose_enteri
|
|||
return -1;
|
||||
unsigned entering = *entering_iter;
|
||||
m_sign_of_entering_delta = this->m_d[entering] > 0 ? 1 : -1;
|
||||
if (this->using_infeas_costs() && this->m_settings.use_breakpoints_in_feasibility_search)
|
||||
m_sign_of_entering_delta = -m_sign_of_entering_delta;
|
||||
m_non_basis_list.erase(entering_iter);
|
||||
m_non_basis_list.push_back(entering);
|
||||
return entering;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename X>
|
||||
unsigned lp_primal_core_solver<T, X>::solve_with_tableau() {
|
||||
unsigned lp_primal_core_solver<T, X>::solve() {
|
||||
TRACE("lar_solver", tout << "solve " << this->get_status() << "\n";);
|
||||
init_run_tableau();
|
||||
if (this->current_x_is_feasible() && this->m_look_for_feasible_solution_only) {
|
||||
this->set_status(lp_status::FEASIBLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((!numeric_traits<T>::precise()) && this->A_mult_x_is_off()) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR);
|
||||
return 0;
|
||||
}
|
||||
do {
|
||||
if (this->print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over((this->using_infeas_costs()? "inf t" : "feas t"), * this->m_settings.get_message_ostream())) {
|
||||
if (this->print_statistics_with_iterations_and_nonzeroes_and_cost_and_check_that_the_time_is_over( "feas t", * this->m_settings.get_message_ostream())) {
|
||||
return this->total_iterations();
|
||||
}
|
||||
if (this->m_settings.use_tableau_rows()) {
|
||||
|
|
@ -122,60 +106,17 @@ unsigned lp_primal_core_solver<T, X>::solve_with_tableau() {
|
|||
}
|
||||
TRACE("lar_solver", tout << "one iteration tableau " << this->get_status() << "\n";);
|
||||
switch (this->get_status()) {
|
||||
case lp_status::OPTIMAL: // double check that we are at optimum
|
||||
case lp_status::INFEASIBLE:
|
||||
if (this->m_look_for_feasible_solution_only && this->current_x_is_feasible())
|
||||
break;
|
||||
if (!numeric_traits<T>::precise()) {
|
||||
if(this->m_look_for_feasible_solution_only)
|
||||
break;
|
||||
this->init_lu();
|
||||
|
||||
if (this->m_factorization->get_status() != LU_status::OK) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR);
|
||||
break;
|
||||
}
|
||||
init_reduced_costs();
|
||||
if (choose_entering_column(1) == -1) {
|
||||
decide_on_status_when_cannot_find_entering();
|
||||
break;
|
||||
}
|
||||
this->set_status(lp_status::UNKNOWN);
|
||||
} else { // precise case
|
||||
if ((!this->infeasibility_costs_are_correct())) {
|
||||
init_reduced_costs_tableau(); // forcing recalc
|
||||
if (choose_entering_column_tableau() == -1) {
|
||||
decide_on_status_when_cannot_find_entering();
|
||||
break;
|
||||
}
|
||||
this->set_status(lp_status::UNKNOWN);
|
||||
}
|
||||
}
|
||||
case lp_status::OPTIMAL: // check again that we are at optimum
|
||||
break;
|
||||
case lp_status::TENTATIVE_UNBOUNDED:
|
||||
this->init_lu();
|
||||
if (this->m_factorization->get_status() != LU_status::OK) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR);
|
||||
break;
|
||||
}
|
||||
|
||||
init_reduced_costs();
|
||||
UNREACHABLE();
|
||||
break;
|
||||
case lp_status::UNBOUNDED:
|
||||
if (this->current_x_is_infeasible()) {
|
||||
init_reduced_costs_tableau();
|
||||
this->set_status(lp_status::UNKNOWN);
|
||||
}
|
||||
lp_assert (this->current_x_is_feasible());
|
||||
break;
|
||||
|
||||
case lp_status::UNSTABLE:
|
||||
lp_assert(! (numeric_traits<T>::precise()));
|
||||
this->init_lu();
|
||||
if (this->m_factorization->get_status() != LU_status::OK) {
|
||||
this->set_status(lp_status::FLOATING_POINT_ERROR);
|
||||
break;
|
||||
}
|
||||
init_reduced_costs();
|
||||
UNREACHABLE();
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -188,8 +129,7 @@ unsigned lp_primal_core_solver<T, X>::solve_with_tableau() {
|
|||
this->set_status(lp_status::CANCELLED);
|
||||
break; // from the loop
|
||||
}
|
||||
} while (this->get_status() != lp_status::FLOATING_POINT_ERROR
|
||||
&&
|
||||
} while (
|
||||
this->get_status() != lp_status::UNBOUNDED
|
||||
&&
|
||||
this->get_status() != lp_status::OPTIMAL
|
||||
|
|
@ -199,8 +139,7 @@ unsigned lp_primal_core_solver<T, X>::solve_with_tableau() {
|
|||
!(this->current_x_is_feasible() && this->m_look_for_feasible_solution_only)
|
||||
);
|
||||
|
||||
lp_assert(this->get_status() == lp_status::FLOATING_POINT_ERROR
|
||||
||
|
||||
lp_assert(
|
||||
this->get_status() == lp_status::CANCELLED
|
||||
||
|
||||
this->current_x_is_feasible() == false
|
||||
|
|
@ -210,24 +149,22 @@ unsigned lp_primal_core_solver<T, X>::solve_with_tableau() {
|
|||
|
||||
}
|
||||
template <typename T, typename X>void lp_primal_core_solver<T, X>::advance_on_entering_and_leaving_tableau(int entering, int leaving, X & t) {
|
||||
CASSERT("A_off", this->A_mult_x_is_off() == false);
|
||||
lp_assert(leaving >= 0 && entering >= 0);
|
||||
lp_assert((this->m_settings.simplex_strategy() ==
|
||||
simplex_strategy_enum::tableau_rows) ||
|
||||
m_non_basis_list.back() == static_cast<unsigned>(entering));
|
||||
lp_assert(this->using_infeas_costs() || !is_neg(t));
|
||||
lp_assert(!is_neg(t));
|
||||
lp_assert(entering != leaving || !is_zero(t)); // otherwise nothing changes
|
||||
if (entering == leaving) {
|
||||
advance_on_entering_equal_leaving_tableau(entering, t);
|
||||
return;
|
||||
}
|
||||
if (!is_zero(t)) {
|
||||
if (this->current_x_is_feasible() || !this->m_settings.use_breakpoints_in_feasibility_search ) {
|
||||
if (this->current_x_is_feasible() ) {
|
||||
if (m_sign_of_entering_delta == -1)
|
||||
t = -t;
|
||||
}
|
||||
this->update_basis_and_x_tableau(entering, leaving, t);
|
||||
CASSERT("A_off", this->A_mult_x_is_off() == false);
|
||||
this->iters_with_no_cost_growing() = 0;
|
||||
} else {
|
||||
this->pivot_column_tableau(entering, this->m_basis_heading[leaving]);
|
||||
|
|
@ -238,11 +175,6 @@ template <typename T, typename X>void lp_primal_core_solver<T, X>::advance_on_en
|
|||
return;
|
||||
|
||||
if (this->m_settings.simplex_strategy() != simplex_strategy_enum::tableau_rows) {
|
||||
if (need_to_switch_costs()) {
|
||||
this->init_reduced_costs_tableau();
|
||||
}
|
||||
|
||||
lp_assert(!need_to_switch_costs());
|
||||
std::list<unsigned>::iterator it = m_non_basis_list.end();
|
||||
it--;
|
||||
* it = static_cast<unsigned>(leaving);
|
||||
|
|
@ -251,14 +183,11 @@ template <typename T, typename X>void lp_primal_core_solver<T, X>::advance_on_en
|
|||
|
||||
template <typename T, typename X>
|
||||
void lp_primal_core_solver<T, X>::advance_on_entering_equal_leaving_tableau(int entering, X & t) {
|
||||
CASSERT("A_off", !this->A_mult_x_is_off() );
|
||||
this->update_x_tableau(entering, t * m_sign_of_entering_delta);
|
||||
if (this->m_look_for_feasible_solution_only && this->current_x_is_feasible())
|
||||
return;
|
||||
|
||||
if (need_to_switch_costs()) {
|
||||
init_reduced_costs_tableau();
|
||||
}
|
||||
|
||||
this->iters_with_no_cost_growing() = 0;
|
||||
}
|
||||
template <typename T, typename X> int lp_primal_core_solver<T, X>::find_leaving_and_t_tableau(unsigned entering, X & t) {
|
||||
|
|
@ -323,7 +252,6 @@ template <typename T, typename X> int lp_primal_core_solver<T, X>::find_leaving_
|
|||
return m_leaving_candidates[k];
|
||||
}
|
||||
template <typename T, typename X> void lp_primal_core_solver<T, X>::init_run_tableau() {
|
||||
CASSERT("A_off", this->A_mult_x_is_off() == false);
|
||||
lp_assert(basis_columns_are_set_correctly());
|
||||
this->m_basis_sort_counter = 0; // to initiate the sort of the basis
|
||||
// this->set_total_iterations(0);
|
||||
|
|
@ -333,13 +261,7 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::init_run_tab
|
|||
return;
|
||||
if (this->m_settings.backup_costs)
|
||||
backup_and_normalize_costs();
|
||||
m_epsilon_of_reduced_cost = numeric_traits<X>::precise() ? zero_of_type<T>() : T(1) / T(10000000);
|
||||
if (this->m_settings.use_breakpoints_in_feasibility_search)
|
||||
m_breakpoint_indices_queue.resize(this->m_n());
|
||||
if (!numeric_traits<X>::precise()) {
|
||||
this->m_column_norm_update_counter = 0;
|
||||
init_column_norms();
|
||||
}
|
||||
|
||||
if (this->m_settings.simplex_strategy() == simplex_strategy_enum::tableau_rows)
|
||||
init_tableau_rows();
|
||||
lp_assert(this->reduced_costs_are_correct_tableau());
|
||||
|
|
@ -348,62 +270,25 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::init_run_tab
|
|||
|
||||
template <typename T, typename X> bool lp_primal_core_solver<T, X>::
|
||||
update_basis_and_x_tableau(int entering, int leaving, X const & tt) {
|
||||
lp_assert(this->use_tableau());
|
||||
lp_assert(entering != leaving);
|
||||
update_x_tableau(entering, tt);
|
||||
this->pivot_column_tableau(entering, this->m_basis_heading[leaving]);
|
||||
this->change_basis(entering, leaving);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_core_solver<T, X>::
|
||||
update_x_tableau(unsigned entering, const X& delta) {
|
||||
this->add_delta_to_x(entering, delta);
|
||||
if (!this->using_infeas_costs()) {
|
||||
for (const auto & c : this->m_A.m_columns[entering]) {
|
||||
unsigned i = c.var();
|
||||
this->add_delta_to_x_and_track_feasibility(this->m_basis[i], - delta * this->m_A.get_val(c));
|
||||
}
|
||||
} else { // using_infeas_costs() == true
|
||||
lp_assert(this->column_is_feasible(entering));
|
||||
lp_assert(this->m_costs[entering] == zero_of_type<T>());
|
||||
// m_d[entering] can change because of the cost change for basic columns.
|
||||
for (const auto & c : this->m_A.m_columns[entering]) {
|
||||
unsigned i = c.var();
|
||||
unsigned j = this->m_basis[i];
|
||||
this->add_delta_to_x(j, -delta * this->m_A.get_val(c));
|
||||
update_inf_cost_for_column_tableau(j);
|
||||
if (is_zero(this->m_costs[j]))
|
||||
this->remove_column_from_inf_set(j);
|
||||
else
|
||||
this->insert_column_into_inf_set(j);
|
||||
}
|
||||
for (const auto & c : this->m_A.m_columns[entering]) {
|
||||
unsigned i = c.var();
|
||||
this->add_delta_to_x_and_track_feasibility(this->m_basis[i], - delta * this->m_A.get_val(c));
|
||||
}
|
||||
CASSERT("A_off", this->A_mult_x_is_off() == false);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_core_solver<T, X>::
|
||||
update_inf_cost_for_column_tableau(unsigned j) {
|
||||
lp_assert(this->m_settings.simplex_strategy() != simplex_strategy_enum::tableau_rows);
|
||||
|
||||
lp_assert(this->using_infeas_costs());
|
||||
|
||||
T new_cost = get_infeasibility_cost_for_column(j);
|
||||
T delta = this->m_costs[j] - new_cost;
|
||||
if (is_zero(delta))
|
||||
return;
|
||||
this->m_costs[j] = new_cost;
|
||||
update_reduced_cost_for_basic_column_cost_change(delta, j);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_core_solver<T, X>::init_reduced_costs_tableau() {
|
||||
if (this->current_x_is_infeasible() && !this->using_infeas_costs()) {
|
||||
init_infeasibility_costs();
|
||||
} else if (this->current_x_is_feasible() && this->using_infeas_costs()) {
|
||||
if (this->m_look_for_feasible_solution_only)
|
||||
return;
|
||||
this->m_costs = m_costs_backup;
|
||||
this->set_using_infeas_costs(false);
|
||||
}
|
||||
|
||||
unsigned size = this->m_basis_heading.size();
|
||||
for (unsigned j = 0; j < size; j++) {
|
||||
if (this->m_basis_heading[j] >= 0)
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include <functional>
|
||||
#include "math/lp/lp_primal_simplex_def.h"
|
||||
template bool lp::lp_primal_simplex<double, double>::bounds_hold(std::unordered_map<std::string, double, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, double> > > const&);
|
||||
template bool lp::lp_primal_simplex<double, double>::row_constraints_hold(std::unordered_map<std::string, double, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, double> > > const&);
|
||||
template double lp::lp_primal_simplex<double, double>::get_current_cost() const;
|
||||
template double lp::lp_primal_simplex<double, double>::get_column_value(unsigned int) const;
|
||||
template lp::lp_primal_simplex<double, double>::~lp_primal_simplex();
|
||||
template lp::lp_primal_simplex<lp::mpq, lp::mpq>::~lp_primal_simplex();
|
||||
template lp::mpq lp::lp_primal_simplex<lp::mpq, lp::mpq>::get_current_cost() const;
|
||||
template lp::mpq lp::lp_primal_simplex<lp::mpq, lp::mpq>::get_column_value(unsigned int) const;
|
||||
template void lp::lp_primal_simplex<double, double>::find_maximal_solution();
|
||||
template void lp::lp_primal_simplex<lp::mpq, lp::mpq>::find_maximal_solution();
|
||||
|
|
@ -1,106 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/column_info.h"
|
||||
#include "math/lp/lp_primal_core_solver.h"
|
||||
#include "math/lp/lp_solver.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
class lp_primal_simplex: public lp_solver<T, X> {
|
||||
lp_primal_core_solver<T, X> * m_core_solver;
|
||||
vector<X> m_lower_bounds;
|
||||
private:
|
||||
unsigned original_rows() { return this->m_external_rows_to_core_solver_rows.size(); }
|
||||
|
||||
void fill_costs_and_x_for_first_stage_solver(unsigned original_number_of_columns);
|
||||
|
||||
void init_buffer(unsigned k, vector<T> & r);
|
||||
|
||||
void refactor();
|
||||
|
||||
void set_scaled_costs();
|
||||
public:
|
||||
lp_primal_simplex(): m_core_solver(nullptr) {}
|
||||
|
||||
column_info<T> * get_or_create_column_info(unsigned column);
|
||||
|
||||
void set_status(lp_status status) {
|
||||
this->m_status = status;
|
||||
}
|
||||
|
||||
lp_status get_status() {
|
||||
return this->m_status;
|
||||
}
|
||||
|
||||
void fill_acceptable_values_for_x();
|
||||
|
||||
|
||||
void set_zero_bound(bool * bound_is_set, T * bounds, unsigned i);
|
||||
|
||||
void fill_costs_and_x_for_first_stage_solver_for_row(
|
||||
int row,
|
||||
unsigned & slack_var,
|
||||
unsigned & artificial);
|
||||
|
||||
|
||||
|
||||
|
||||
void set_core_solver_bounds();
|
||||
|
||||
void find_maximal_solution() override;
|
||||
|
||||
void fill_A_x_and_basis_for_stage_one_total_inf();
|
||||
|
||||
void fill_A_x_and_basis_for_stage_one_total_inf_for_row(unsigned row);
|
||||
|
||||
void solve_with_total_inf();
|
||||
|
||||
|
||||
~lp_primal_simplex() override;
|
||||
|
||||
bool bounds_hold(std::unordered_map<std::string, T> const & solution);
|
||||
|
||||
T get_row_value(unsigned i, std::unordered_map<std::string, T> const & solution, std::ostream * out);
|
||||
|
||||
bool row_constraint_holds(unsigned i, std::unordered_map<std::string, T> const & solution, std::ostream * out);
|
||||
|
||||
bool row_constraints_hold(std::unordered_map<std::string, T> const & solution);
|
||||
|
||||
|
||||
T * get_array_from_map(std::unordered_map<std::string, T> const & solution);
|
||||
|
||||
bool solution_is_feasible(std::unordered_map<std::string, T> const & solution) {
|
||||
return bounds_hold(solution) && row_constraints_hold(solution);
|
||||
}
|
||||
|
||||
T get_column_value(unsigned column) const override {
|
||||
return this->get_column_value_with_core_solver(column, m_core_solver);
|
||||
}
|
||||
|
||||
T get_current_cost() const override;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
|
@ -1,367 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_primal_simplex.h"
|
||||
|
||||
namespace lp {
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::fill_costs_and_x_for_first_stage_solver(unsigned original_number_of_columns) {
|
||||
unsigned slack_var = original_number_of_columns;
|
||||
unsigned artificial = original_number_of_columns + this->m_slacks;
|
||||
|
||||
for (unsigned row = 0; row < this->row_count(); row++) {
|
||||
fill_costs_and_x_for_first_stage_solver_for_row(row, slack_var, artificial);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::init_buffer(unsigned k, vector<T> & r) {
|
||||
for (unsigned i = 0; i < k; i++) {
|
||||
r[i] = 0;
|
||||
}
|
||||
r[k] = 1;
|
||||
for (unsigned i = this->row_count() -1; i > k; i--) {
|
||||
r[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::refactor() {
|
||||
m_core_solver->init_lu();
|
||||
if (m_core_solver->factorization()->get_status() != LU_status::OK) {
|
||||
throw_exception("cannot refactor");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::set_scaled_costs() {
|
||||
unsigned j = this->number_of_core_structurals();
|
||||
while (j-- > 0) {
|
||||
this->set_scaled_cost(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> column_info<T> * lp_primal_simplex<T, X>::get_or_create_column_info(unsigned column) {
|
||||
auto it = this->m_columns.find(column);
|
||||
return (it == this->m_columns.end())? ( this->m_columns[column] = new column_info<T>) : it->second;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::fill_acceptable_values_for_x() {
|
||||
for (auto t : this->m_core_solver_columns_to_external_columns) {
|
||||
this->m_x[t.first] = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::set_zero_bound(bool * bound_is_set, T * bounds, unsigned i) {
|
||||
bound_is_set[i] = true;
|
||||
bounds[i] = numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::fill_costs_and_x_for_first_stage_solver_for_row(
|
||||
int row,
|
||||
unsigned & slack_var,
|
||||
unsigned & artificial) {
|
||||
lp_assert(row >= 0 && row < this->row_count());
|
||||
auto & constraint = this->m_constraints[this->m_core_solver_rows_to_external_rows[row]];
|
||||
// we need to bring the program to the form Ax = b
|
||||
T rs = this->m_b[row];
|
||||
T artificial_cost = - numeric_traits<T>::one();
|
||||
switch (constraint.m_relation) {
|
||||
case Equal: // no slack variable here
|
||||
this->m_column_types[artificial] = column_type::lower_bound;
|
||||
this->m_costs[artificial] = artificial_cost; // we are maximizing, so the artificial, which is non-negatiive, will be pushed to zero
|
||||
this->m_basis[row] = artificial;
|
||||
if (rs >= 0) {
|
||||
(*this->m_A)(row, artificial) = numeric_traits<T>::one();
|
||||
this->m_x[artificial] = rs;
|
||||
} else {
|
||||
(*this->m_A)(row, artificial) = - numeric_traits<T>::one();
|
||||
this->m_x[artificial] = - rs;
|
||||
}
|
||||
artificial++;
|
||||
break;
|
||||
|
||||
case Greater_or_equal:
|
||||
this->m_column_types[slack_var] = column_type::lower_bound;
|
||||
(*this->m_A)(row, slack_var) = - numeric_traits<T>::one();
|
||||
|
||||
if (rs > 0) {
|
||||
lp_assert(numeric_traits<T>::is_zero(this->m_x[slack_var]));
|
||||
// adding one artificial
|
||||
this->m_column_types[artificial] = column_type::lower_bound;
|
||||
(*this->m_A)(row, artificial) = numeric_traits<T>::one();
|
||||
this->m_costs[artificial] = artificial_cost;
|
||||
this->m_basis[row] = artificial;
|
||||
this->m_x[artificial] = rs;
|
||||
artificial++;
|
||||
} else {
|
||||
// we can put a slack_var into the basis, and atemplate <typename T, typename X> void lp_primal_simplex<T, X>::adding an artificial variable
|
||||
this->m_basis[row] = slack_var;
|
||||
this->m_x[slack_var] = - rs;
|
||||
}
|
||||
slack_var++;
|
||||
break;
|
||||
case Less_or_equal:
|
||||
// introduce a non-negative slack variable
|
||||
this->m_column_types[slack_var] = column_type::lower_bound;
|
||||
(*this->m_A)(row, slack_var) = numeric_traits<T>::one();
|
||||
|
||||
if (rs < 0) {
|
||||
// adding one artificial
|
||||
lp_assert(numeric_traits<T>::is_zero(this->m_x[slack_var]));
|
||||
this->m_column_types[artificial] = column_type::lower_bound;
|
||||
(*this->m_A)(row, artificial) = - numeric_traits<T>::one();
|
||||
this->m_costs[artificial] = artificial_cost;
|
||||
this->m_x[artificial] = - rs;
|
||||
this->m_basis[row] = artificial++;
|
||||
} else {
|
||||
// we can put slack_var into the basis, and atemplate <typename T, typename X> void lp_primal_simplex<T, X>::adding an artificial variable
|
||||
this->m_basis[row] = slack_var;
|
||||
this->m_x[slack_var] = rs;
|
||||
}
|
||||
slack_var++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::set_core_solver_bounds() {
|
||||
unsigned total_vars = this->m_A->column_count() + this->m_slacks + this->m_artificials;
|
||||
this->m_column_types.resize(total_vars);
|
||||
this->m_upper_bounds.resize(total_vars);
|
||||
for (auto cit : this->m_map_from_var_index_to_column_info) {
|
||||
column_info<T> * ci = cit.second;
|
||||
unsigned j = ci->get_column_index();
|
||||
if (!is_valid(j))
|
||||
continue; // the variable is not mapped to a column
|
||||
switch (this->m_column_types[j] = ci->get_column_type()){
|
||||
case column_type::fixed:
|
||||
this->m_upper_bounds[j] = numeric_traits<T>::zero();
|
||||
break;
|
||||
case column_type::boxed:
|
||||
this->m_upper_bounds[j] = ci->get_adjusted_upper_bound() / this->m_column_scale[j];
|
||||
break;
|
||||
|
||||
default: break; // do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::find_maximal_solution() {
|
||||
if (this->problem_is_empty()) {
|
||||
this->m_status = lp_status::EMPTY;
|
||||
return;
|
||||
}
|
||||
|
||||
this->cleanup();
|
||||
this->fill_matrix_A_and_init_right_side();
|
||||
if (this->m_status == lp_status::INFEASIBLE) {
|
||||
return;
|
||||
}
|
||||
this->m_x.resize(this->m_A->column_count());
|
||||
this->fill_m_b();
|
||||
this->scale();
|
||||
fill_acceptable_values_for_x();
|
||||
this->count_slacks_and_artificials();
|
||||
set_core_solver_bounds();
|
||||
solve_with_total_inf();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::fill_A_x_and_basis_for_stage_one_total_inf() {
|
||||
for (unsigned row = 0; row < this->row_count(); row++)
|
||||
fill_A_x_and_basis_for_stage_one_total_inf_for_row(row);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::fill_A_x_and_basis_for_stage_one_total_inf_for_row(unsigned row) {
|
||||
lp_assert(row < this->row_count());
|
||||
auto ext_row_it = this->m_core_solver_rows_to_external_rows.find(row);
|
||||
lp_assert(ext_row_it != this->m_core_solver_rows_to_external_rows.end());
|
||||
unsigned ext_row = ext_row_it->second;
|
||||
auto constr_it = this->m_constraints.find(ext_row);
|
||||
lp_assert(constr_it != this->m_constraints.end());
|
||||
auto & constraint = constr_it->second;
|
||||
unsigned j = this->m_A->column_count(); // j is a slack variable
|
||||
this->m_A->add_column();
|
||||
// we need to bring the program to the form Ax = b
|
||||
this->m_basis[row] = j;
|
||||
switch (constraint.m_relation) {
|
||||
case Equal:
|
||||
this->m_x[j] = this->m_b[row];
|
||||
(*this->m_A)(row, j) = numeric_traits<T>::one();
|
||||
this->m_column_types[j] = column_type::fixed;
|
||||
this->m_upper_bounds[j] = m_lower_bounds[j] = zero_of_type<X>();
|
||||
break;
|
||||
|
||||
case Greater_or_equal:
|
||||
this->m_x[j] = - this->m_b[row];
|
||||
(*this->m_A)(row, j) = - numeric_traits<T>::one();
|
||||
this->m_column_types[j] = column_type::lower_bound;
|
||||
this->m_upper_bounds[j] = zero_of_type<X>();
|
||||
break;
|
||||
case Less_or_equal:
|
||||
this->m_x[j] = this->m_b[row];
|
||||
(*this->m_A)(row, j) = numeric_traits<T>::one();
|
||||
this->m_column_types[j] = column_type::lower_bound;
|
||||
this->m_upper_bounds[j] = m_lower_bounds[j] = zero_of_type<X>();
|
||||
break;
|
||||
default:
|
||||
lp_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_primal_simplex<T, X>::solve_with_total_inf() {
|
||||
int total_vars = this->m_A->column_count() + this->row_count();
|
||||
if (total_vars == 0) {
|
||||
this->m_status = lp_status::OPTIMAL;
|
||||
return;
|
||||
}
|
||||
m_lower_bounds.clear();
|
||||
m_lower_bounds.resize(total_vars, zero_of_type<X>()); // low bounds are shifted ot zero
|
||||
this->m_x.resize(total_vars, numeric_traits<T>::zero());
|
||||
this->m_basis.resize(this->row_count());
|
||||
this->m_costs.clear();
|
||||
this->m_costs.resize(total_vars, zero_of_type<T>());
|
||||
fill_A_x_and_basis_for_stage_one_total_inf();
|
||||
if (this->m_settings.get_message_ostream() != nullptr)
|
||||
this->print_statistics_on_A(*this->m_settings.get_message_ostream());
|
||||
set_scaled_costs();
|
||||
|
||||
m_core_solver = new lp_primal_core_solver<T, X>(*this->m_A,
|
||||
this->m_b,
|
||||
this->m_x,
|
||||
this->m_basis,
|
||||
this->m_nbasis,
|
||||
this->m_heading,
|
||||
this->m_costs,
|
||||
this->m_column_types,
|
||||
m_lower_bounds,
|
||||
this->m_upper_bounds,
|
||||
this->m_settings, *this);
|
||||
m_core_solver->solve();
|
||||
this->set_status(m_core_solver->get_status());
|
||||
this->m_total_iterations = m_core_solver->total_iterations();
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> lp_primal_simplex<T, X>::~lp_primal_simplex() {
|
||||
delete m_core_solver;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_primal_simplex<T, X>::bounds_hold(std::unordered_map<std::string, T> const & solution) {
|
||||
for (auto it : this->m_map_from_var_index_to_column_info) {
|
||||
auto sol_it = solution.find(it.second->get_name());
|
||||
if (sol_it == solution.end()) {
|
||||
std::stringstream s;
|
||||
s << "cannot find column " << it.first << " in solution";
|
||||
throw_exception(s.str() );
|
||||
}
|
||||
|
||||
if (!it.second->bounds_hold(sol_it->second)) {
|
||||
it.second->bounds_hold(sol_it->second);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_primal_simplex<T, X>::get_row_value(unsigned i, std::unordered_map<std::string, T> const & solution, std::ostream * out) {
|
||||
auto it = this->m_A_values.find(i);
|
||||
if (it == this->m_A_values.end()) {
|
||||
std::stringstream s;
|
||||
s << "cannot find row " << i;
|
||||
throw_exception(s.str() );
|
||||
}
|
||||
T ret = numeric_traits<T>::zero();
|
||||
for (auto & pair : it->second) {
|
||||
auto cit = this->m_map_from_var_index_to_column_info.find(pair.first);
|
||||
lp_assert(cit != this->m_map_from_var_index_to_column_info.end());
|
||||
column_info<T> * ci = cit->second;
|
||||
auto sol_it = solution.find(ci->get_name());
|
||||
lp_assert(sol_it != solution.end());
|
||||
T column_val = sol_it->second;
|
||||
if (out != nullptr) {
|
||||
(*out) << pair.second << "(" << ci->get_name() << "=" << column_val << ") ";
|
||||
}
|
||||
ret += pair.second * column_val;
|
||||
}
|
||||
if (out != nullptr) {
|
||||
(*out) << " = " << ret << std::endl;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_primal_simplex<T, X>::row_constraint_holds(unsigned i, std::unordered_map<std::string, T> const & solution, std::ostream *out) {
|
||||
T row_val = get_row_value(i, solution, out);
|
||||
auto & constraint = this->m_constraints[i];
|
||||
T rs = constraint.m_rs;
|
||||
bool print = out != nullptr;
|
||||
switch (constraint.m_relation) {
|
||||
case Equal:
|
||||
if (fabs(numeric_traits<T>::get_double(row_val - rs)) > 0.00001) {
|
||||
if (print) {
|
||||
(*out) << "should be = " << rs << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
case Greater_or_equal:
|
||||
if (numeric_traits<T>::get_double(row_val - rs) < -0.00001) {
|
||||
if (print) {
|
||||
(*out) << "should be >= " << rs << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;;
|
||||
|
||||
case Less_or_equal:
|
||||
if (numeric_traits<T>::get_double(row_val - rs) > 0.00001) {
|
||||
if (print) {
|
||||
(*out) << "should be <= " << rs << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;;
|
||||
}
|
||||
lp_unreachable();
|
||||
return false; // it is unreachable
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_primal_simplex<T, X>::row_constraints_hold(std::unordered_map<std::string, T> const & solution) {
|
||||
for (auto it : this->m_A_values) {
|
||||
if (!row_constraint_holds(it.first, solution, nullptr)) {
|
||||
row_constraint_holds(it.first, solution, nullptr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_primal_simplex<T, X>::get_current_cost() const {
|
||||
T ret = numeric_traits<T>::zero();
|
||||
for (auto it : this->m_map_from_var_index_to_column_info) {
|
||||
ret += this->get_column_cost_value(it.first, it.second);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,6 @@ Revision History:
|
|||
#include "util/vector.h"
|
||||
#include "smt/params/smt_params_helper.hpp"
|
||||
#include "math/lp/lp_settings_def.h"
|
||||
template bool lp::vectors_are_equal<double>(vector<double> const&, vector<double> const&);
|
||||
template bool lp::vectors_are_equal<lp::mpq>(vector<lp::mpq > const&, vector<lp::mpq> const&);
|
||||
|
||||
void lp::lp_settings::updt_params(params_ref const& _p) {
|
||||
|
|
|
|||
|
|
@ -55,8 +55,7 @@ inline std::ostream& operator<<(std::ostream& out, column_type const& t) {
|
|||
enum class simplex_strategy_enum {
|
||||
undecided = 3,
|
||||
tableau_rows = 0,
|
||||
tableau_costs = 1,
|
||||
lu = 2
|
||||
tableau_costs = 1
|
||||
};
|
||||
|
||||
std::string column_type_to_string(column_type t);
|
||||
|
|
@ -70,7 +69,6 @@ enum class lp_status {
|
|||
DUAL_UNBOUNDED,
|
||||
OPTIMAL,
|
||||
FEASIBLE,
|
||||
FLOATING_POINT_ERROR,
|
||||
TIME_EXHAUSTED,
|
||||
EMPTY,
|
||||
UNSTABLE,
|
||||
|
|
@ -80,9 +78,8 @@ enum class lp_status {
|
|||
// when the ratio of the vector length to domain size to is greater than the return value we switch to solve_By_for_T_indexed_only
|
||||
template <typename X>
|
||||
unsigned ratio_of_index_size_to_all_size() {
|
||||
if (numeric_traits<X>::precise())
|
||||
return 10;
|
||||
return 120;
|
||||
|
||||
}
|
||||
|
||||
const char* lp_status_to_string(lp_status status);
|
||||
|
|
@ -93,9 +90,6 @@ inline std::ostream& operator<<(std::ostream& out, lp_status status) {
|
|||
|
||||
lp_status lp_status_from_string(std::string status);
|
||||
|
||||
enum non_basic_column_value_position { at_lower_bound, at_upper_bound, at_fixed, free_of_bounds, not_at_bound };
|
||||
|
||||
template <typename X> bool is_epsilon_small(const X & v, const double& eps); // forward definition
|
||||
|
||||
class lp_resource_limit {
|
||||
public:
|
||||
|
|
@ -127,6 +121,7 @@ struct statistics {
|
|||
unsigned m_grobner_calls;
|
||||
unsigned m_grobner_conflicts;
|
||||
unsigned m_offset_eqs;
|
||||
unsigned m_fixed_eqs;
|
||||
statistics() { reset(); }
|
||||
void reset() { memset(this, 0, sizeof(*this)); }
|
||||
void collect_statistics(::statistics& st) const {
|
||||
|
|
@ -148,6 +143,7 @@ struct statistics {
|
|||
st.update("arith-grobner-calls", m_grobner_calls);
|
||||
st.update("arith-grobner-conflicts", m_grobner_conflicts);
|
||||
st.update("arith-offset-eqs", m_offset_eqs);
|
||||
st.update("arith-fixed-eqs", m_fixed_eqs);
|
||||
|
||||
}
|
||||
};
|
||||
|
|
@ -167,11 +163,11 @@ private:
|
|||
};
|
||||
|
||||
default_lp_resource_limit m_default_resource_limit;
|
||||
lp_resource_limit* m_resource_limit;
|
||||
lp_resource_limit* m_resource_limit = nullptr;
|
||||
// used for debug output
|
||||
std::ostream* m_debug_out;
|
||||
std::ostream* m_debug_out = nullptr;
|
||||
// used for messages, for example, the computation progress messages
|
||||
std::ostream* m_message_out;
|
||||
std::ostream* m_message_out = nullptr;
|
||||
|
||||
statistics m_stats;
|
||||
random_gen m_rand;
|
||||
|
|
@ -182,66 +178,40 @@ public:
|
|||
unsigned nlsat_delay() const { return m_nlsat_delay; }
|
||||
bool int_run_gcd_test() const { return m_int_run_gcd_test; }
|
||||
bool& int_run_gcd_test() { return m_int_run_gcd_test; }
|
||||
unsigned reps_in_scaler { 20 };
|
||||
// when the absolute value of an element is less than pivot_epsilon
|
||||
// in pivoting, we treat it as a zero
|
||||
double pivot_epsilon { 0.00000001 };
|
||||
// see Chatal, page 115
|
||||
double positive_price_epsilon { 1e-7 };
|
||||
// a quotation "if some choice of the entering variable leads to an eta matrix
|
||||
// whose diagonal element in the eta column is less than e2 (entering_diag_epsilon) in magnitude, the this choice is rejected ...
|
||||
double entering_diag_epsilon { 1e-8 };
|
||||
int c_partial_pivoting { 10 }; // this is the constant c from page 410
|
||||
unsigned depth_of_rook_search { 4 };
|
||||
bool using_partial_pivoting { true };
|
||||
// dissertation of Achim Koberstein
|
||||
// if Bx - b is different at any component more that refactor_epsilon then we refactor
|
||||
double refactor_tolerance { 1e-4 };
|
||||
double pivot_tolerance { 1e-6 };
|
||||
double zero_tolerance { 1e-12 };
|
||||
double drop_tolerance { 1e-14 };
|
||||
double tolerance_for_artificials { 1e-4 };
|
||||
double can_be_taken_to_basis_tolerance { 0.00001 };
|
||||
|
||||
unsigned percent_of_entering_to_check { 5 }; // we try to find a profitable column in a percentage of the columns
|
||||
bool use_scaling { true };
|
||||
double scaling_maximum { 1.0 };
|
||||
double scaling_minimum { 0.5 };
|
||||
double harris_feasibility_tolerance { 1e-7 }; // page 179 of Istvan Maros
|
||||
double ignore_epsilon_of_harris { 10e-5 };
|
||||
unsigned max_number_of_iterations_with_no_improvements { 2000000 };
|
||||
double time_limit; // the maximum time limit of the total run time in seconds
|
||||
// dual section
|
||||
double dual_feasibility_tolerance { 1e-7 }; // page 71 of the PhD thesis of Achim Koberstein
|
||||
double primal_feasibility_tolerance { 1e-7 }; // page 71 of the PhD thesis of Achim Koberstein
|
||||
double relative_primal_feasibility_tolerance { 1e-9 }; // page 71 of the PhD thesis of Achim Koberstein
|
||||
unsigned reps_in_scaler = 20;
|
||||
int c_partial_pivoting = 10; // this is the constant c from page 410
|
||||
unsigned depth_of_rook_search = 4;
|
||||
bool using_partial_pivoting = true;
|
||||
|
||||
unsigned percent_of_entering_to_check = 5; // we try to find a profitable column in a percentage of the columns
|
||||
bool use_scaling = true;
|
||||
unsigned max_number_of_iterations_with_no_improvements = 2000000;
|
||||
double time_limit; // the maximum time limit of the total run time in seconds
|
||||
// end of dual section
|
||||
bool m_bound_propagation { true };
|
||||
bool presolve_with_double_solver_for_lar { true };
|
||||
bool m_bound_propagation = true;
|
||||
bool presolve_with_double_solver_for_lar = true;
|
||||
simplex_strategy_enum m_simplex_strategy;
|
||||
|
||||
int report_frequency { 1000 };
|
||||
bool print_statistics { false };
|
||||
unsigned column_norms_update_frequency { 12000 };
|
||||
bool scale_with_ratio { true };
|
||||
double density_threshold { 0.7 };
|
||||
bool use_breakpoints_in_feasibility_search { false };
|
||||
unsigned max_row_length_for_bound_propagation { 300 };
|
||||
bool backup_costs { true };
|
||||
unsigned column_number_threshold_for_using_lu_in_lar_solver { 4000 };
|
||||
unsigned m_int_gomory_cut_period { 4 };
|
||||
unsigned m_int_find_cube_period { 4 };
|
||||
int report_frequency = 1000;
|
||||
bool print_statistics = false;
|
||||
unsigned column_norms_update_frequency = 12000;
|
||||
bool scale_with_ratio = true;
|
||||
unsigned max_row_length_for_bound_propagation = 300;
|
||||
bool backup_costs = true;
|
||||
unsigned column_number_threshold_for_using_lu_in_lar_solver = 4000;
|
||||
unsigned m_int_gomory_cut_period = 4;
|
||||
unsigned m_int_find_cube_period = 4;
|
||||
private:
|
||||
unsigned m_hnf_cut_period { 4 };
|
||||
bool m_int_run_gcd_test { true };
|
||||
unsigned m_hnf_cut_period = 4;
|
||||
bool m_int_run_gcd_test = true;
|
||||
public:
|
||||
unsigned limit_on_rows_for_hnf_cutter { 75 };
|
||||
unsigned limit_on_columns_for_hnf_cutter { 150 };
|
||||
unsigned limit_on_rows_for_hnf_cutter = 75;
|
||||
unsigned limit_on_columns_for_hnf_cutter = 150;
|
||||
private:
|
||||
unsigned m_nlsat_delay;
|
||||
bool m_enable_hnf { true };
|
||||
bool m_print_external_var_name { false };
|
||||
bool m_propagate_eqs { false };
|
||||
bool m_enable_hnf = true;
|
||||
bool m_print_external_var_name = false;
|
||||
bool m_propagate_eqs = false;
|
||||
public:
|
||||
bool print_external_var_name() const { return m_print_external_var_name; }
|
||||
bool propagate_eqs() const { return m_propagate_eqs;}
|
||||
|
|
@ -274,84 +244,12 @@ public:
|
|||
std::ostream* get_debug_ostream() { return m_debug_out; }
|
||||
std::ostream* get_message_ostream() { return m_message_out; }
|
||||
statistics& stats() { return m_stats; }
|
||||
statistics const& stats() const { return m_stats; }
|
||||
|
||||
template <typename T> static bool is_eps_small_general(const T & t, const double & eps) {
|
||||
return (!numeric_traits<T>::precise())? is_epsilon_small<T>(t, eps) : numeric_traits<T>::is_zero(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_dual_feasibility_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, dual_feasibility_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_primal_feasibility_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, primal_feasibility_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_can_be_taken_to_basis_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, can_be_taken_to_basis_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_drop_tolerance(T const & t) const {
|
||||
return is_eps_small_general<T>(t, drop_tolerance);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_zero_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, zero_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_refactor_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, refactor_tolerance);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_pivot_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, pivot_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_harris_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, harris_feasibility_tolerance);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_ignore_epslilon_for_harris(T const & t) {
|
||||
return is_eps_small_general<T>(t, ignore_epsilon_of_harris);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool abs_val_is_smaller_than_artificial_tolerance(T const & t) {
|
||||
return is_eps_small_general<T>(t, tolerance_for_artificials);
|
||||
}
|
||||
statistics const& stats() const { return m_stats; }
|
||||
|
||||
// the method of lar solver to use
|
||||
simplex_strategy_enum simplex_strategy() const {
|
||||
return m_simplex_strategy;
|
||||
}
|
||||
|
||||
void set_simplex_strategy(simplex_strategy_enum s) {
|
||||
m_simplex_strategy = s;
|
||||
}
|
||||
|
||||
bool use_lu() const {
|
||||
return m_simplex_strategy == simplex_strategy_enum::lu;
|
||||
}
|
||||
|
||||
bool use_tableau() const {
|
||||
return m_simplex_strategy == simplex_strategy_enum::tableau_rows ||
|
||||
m_simplex_strategy == simplex_strategy_enum::tableau_costs;
|
||||
}
|
||||
|
||||
bool use_tableau_rows() const {
|
||||
return m_simplex_strategy == simplex_strategy_enum::tableau_rows;
|
||||
}
|
||||
simplex_strategy_enum simplex_strategy() const { return m_simplex_strategy; }
|
||||
void set_simplex_strategy(simplex_strategy_enum s) { m_simplex_strategy = s; }
|
||||
bool use_tableau_rows() const { return m_simplex_strategy == simplex_strategy_enum::tableau_rows; }
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
static unsigned ddd; // used for debugging
|
||||
|
|
@ -382,13 +280,6 @@ inline std::string T_to_string(const mpq & t) {
|
|||
return strs.str();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool val_is_smaller_than_eps(T const & t, double const & eps) {
|
||||
if (!numeric_traits<T>::precise()) {
|
||||
return numeric_traits<T>::get_double(t) < eps;
|
||||
}
|
||||
return t <= numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool vectors_are_equal(T * a, vector<T> &b, unsigned n);
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ std::string column_type_to_string(column_type t) {
|
|||
case column_type::lower_bound: return "lower_bound";
|
||||
case column_type::upper_bound: return "upper_bound";
|
||||
case column_type::free_column: return "free_column";
|
||||
default: lp_unreachable();
|
||||
default: UNREACHABLE();
|
||||
}
|
||||
return "unknown"; // it is unreachable
|
||||
}
|
||||
|
|
@ -45,13 +45,12 @@ const char* lp_status_to_string(lp_status status) {
|
|||
case lp_status::DUAL_UNBOUNDED: return "DUAL_UNBOUNDED";
|
||||
case lp_status::OPTIMAL: return "OPTIMAL";
|
||||
case lp_status::FEASIBLE: return "FEASIBLE";
|
||||
case lp_status::FLOATING_POINT_ERROR: return "FLOATING_POINT_ERROR";
|
||||
case lp_status::TIME_EXHAUSTED: return "TIME_EXHAUSTED";
|
||||
case lp_status::EMPTY: return "EMPTY";
|
||||
case lp_status::UNSTABLE: return "UNSTABLE";
|
||||
case lp_status::CANCELLED: return "CANCELLED";
|
||||
default:
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
}
|
||||
return "UNKNOWN"; // it is unreachable
|
||||
}
|
||||
|
|
@ -62,29 +61,21 @@ lp_status lp_status_from_string(std::string status) {
|
|||
if (status == "UNBOUNDED") return lp_status::UNBOUNDED;
|
||||
if (status == "OPTIMAL") return lp_status::OPTIMAL;
|
||||
if (status == "FEASIBLE") return lp_status::FEASIBLE;
|
||||
if (status == "FLOATING_POINT_ERROR") return lp_status::FLOATING_POINT_ERROR;
|
||||
if (status == "TIME_EXHAUSTED") return lp_status::TIME_EXHAUSTED;
|
||||
if (status == "EMPTY") return lp_status::EMPTY;
|
||||
lp_unreachable();
|
||||
UNREACHABLE();
|
||||
return lp_status::UNKNOWN; // it is unreachable
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool vectors_are_equal(T * a, vector<T> &b, unsigned n) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (unsigned i = 0; i < n; i ++){
|
||||
if (!numeric_traits<T>::is_zero(a[i] - b[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < n; i ++){
|
||||
if (std::abs(numeric_traits<T>::get_double(a[i] - b[i])) > 0.000001) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -93,27 +84,12 @@ template <typename T>
|
|||
bool vectors_are_equal(const vector<T> & a, const vector<T> &b) {
|
||||
unsigned n = static_cast<unsigned>(a.size());
|
||||
if (n != b.size()) return false;
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (unsigned i = 0; i < n; i ++){
|
||||
if (!numeric_traits<T>::is_zero(a[i] - b[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < n; i ++){
|
||||
double da = numeric_traits<T>::get_double(a[i]);
|
||||
double db = numeric_traits<T>::get_double(b[i]);
|
||||
double amax = std::max(fabs(da), fabs(db));
|
||||
if (amax > 1) {
|
||||
da /= amax;
|
||||
db /= amax;
|
||||
}
|
||||
|
||||
if (fabs(da - db) > 0.000001) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <string>
|
||||
#include "math/lp/lp_solver_def.h"
|
||||
template void lp::lp_solver<double, double>::add_constraint(lp::lp_relation, double, unsigned int);
|
||||
template void lp::lp_solver<double, double>::cleanup();
|
||||
template void lp::lp_solver<double, double>::count_slacks_and_artificials();
|
||||
template void lp::lp_solver<double, double>::fill_m_b();
|
||||
template void lp::lp_solver<double, double>::fill_matrix_A_and_init_right_side();
|
||||
template void lp::lp_solver<double, double>::flip_costs();
|
||||
template double lp::lp_solver<double, double>::get_column_cost_value(unsigned int, lp::column_info<double>*) const;
|
||||
template int lp::lp_solver<double, double>::get_column_index_by_name(std::string) const;
|
||||
template double lp::lp_solver<double, double>::get_column_value_with_core_solver(unsigned int, lp::lp_core_solver_base<double, double>*) const;
|
||||
template lp::column_info<double>* lp::lp_solver<double, double>::get_or_create_column_info(unsigned int);
|
||||
template void lp::lp_solver<double, double>::give_symbolic_name_to_column(std::string, unsigned int);
|
||||
template void lp::lp_solver<double, double>::print_statistics_on_A(std::ostream & out);
|
||||
template bool lp::lp_solver<double, double>::problem_is_empty();
|
||||
template void lp::lp_solver<double, double>::scale();
|
||||
template void lp::lp_solver<double, double>::set_scaled_cost(unsigned int);
|
||||
template lp::lp_solver<double, double>::~lp_solver();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::add_constraint(lp::lp_relation, lp::mpq, unsigned int);
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::cleanup();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::count_slacks_and_artificials();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::fill_m_b();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::fill_matrix_A_and_init_right_side();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::flip_costs();
|
||||
template lp::mpq lp::lp_solver<lp::mpq, lp::mpq>::get_column_cost_value(unsigned int, lp::column_info<lp::mpq>*) const;
|
||||
template int lp::lp_solver<lp::mpq, lp::mpq>::get_column_index_by_name(std::string) const;
|
||||
template lp::mpq lp::lp_solver<lp::mpq, lp::mpq>::get_column_value_by_name(std::string) const;
|
||||
template lp::mpq lp::lp_solver<lp::mpq, lp::mpq>::get_column_value_with_core_solver(unsigned int, lp::lp_core_solver_base<lp::mpq, lp::mpq>*) const;
|
||||
template lp::column_info<lp::mpq>* lp::lp_solver<lp::mpq, lp::mpq>::get_or_create_column_info(unsigned int);
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::give_symbolic_name_to_column(std::string, unsigned int);
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::print_statistics_on_A(std::ostream & out);
|
||||
template bool lp::lp_solver<lp::mpq, lp::mpq>::problem_is_empty();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::scale();
|
||||
template void lp::lp_solver<lp::mpq, lp::mpq>::set_scaled_cost(unsigned int);
|
||||
template lp::lp_solver<lp::mpq, lp::mpq>::~lp_solver();
|
||||
template double lp::lp_solver<double, double>::get_column_value_by_name(std::string) const;
|
||||
|
|
@ -1,260 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include "math/lp/column_info.h"
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include "math/lp/lp_core_solver_base.h"
|
||||
#include "math/lp/scaler.h"
|
||||
#include "math/lp/bound_analyzer_on_row.h"
|
||||
namespace lp {
|
||||
enum lp_relation {
|
||||
Less_or_equal,
|
||||
Equal,
|
||||
Greater_or_equal
|
||||
};
|
||||
|
||||
template <typename T, typename X>
|
||||
struct lp_constraint {
|
||||
X m_rs; // right side of the constraint
|
||||
lp_relation m_relation;
|
||||
lp_constraint() {} // empty constructor
|
||||
lp_constraint(T rs, lp_relation relation): m_rs(rs), m_relation(relation) {}
|
||||
};
|
||||
|
||||
|
||||
template <typename T, typename X>
|
||||
class lp_solver : public column_namer {
|
||||
column_info<T> * get_or_create_column_info(unsigned column);
|
||||
|
||||
protected:
|
||||
T get_column_cost_value(unsigned j, column_info<T> * ci) const;
|
||||
public:
|
||||
unsigned m_total_iterations;
|
||||
static_matrix<T, X>* m_A; // this is the matrix of constraints
|
||||
vector<T> m_b; // the right side vector
|
||||
unsigned m_first_stage_iterations;
|
||||
unsigned m_second_stage_iterations;
|
||||
std::unordered_map<unsigned, lp_constraint<T, X>> m_constraints;
|
||||
std::unordered_map<var_index, column_info<T>*> m_map_from_var_index_to_column_info;
|
||||
std::unordered_map<unsigned, std::unordered_map<unsigned, T> > m_A_values;
|
||||
std::unordered_map<std::string, unsigned> m_names_to_columns; // don't have to use it
|
||||
std::unordered_map<unsigned, unsigned> m_external_rows_to_core_solver_rows;
|
||||
std::unordered_map<unsigned, unsigned> m_core_solver_rows_to_external_rows;
|
||||
std::unordered_map<unsigned, unsigned> m_core_solver_columns_to_external_columns;
|
||||
vector<T> m_column_scale;
|
||||
std::unordered_map<unsigned, std::string> m_name_map;
|
||||
unsigned m_artificials;
|
||||
unsigned m_slacks;
|
||||
vector<column_type> m_column_types;
|
||||
vector<T> m_costs;
|
||||
vector<T> m_x;
|
||||
vector<T> m_upper_bounds;
|
||||
vector<unsigned> m_basis;
|
||||
vector<unsigned> m_nbasis;
|
||||
vector<int> m_heading;
|
||||
|
||||
|
||||
lp_status m_status;
|
||||
|
||||
lp_settings m_settings;
|
||||
lp_solver():
|
||||
m_A(nullptr), // this is the matrix of constraints
|
||||
m_first_stage_iterations (0),
|
||||
m_second_stage_iterations (0),
|
||||
m_artificials (0),
|
||||
m_slacks (0),
|
||||
m_status(lp_status::UNKNOWN)
|
||||
{}
|
||||
|
||||
unsigned row_count() const { return this->m_A->row_count(); }
|
||||
|
||||
void add_constraint(lp_relation relation, T right_side, unsigned row_index);
|
||||
|
||||
void set_cost_for_column(unsigned column, T column_cost) {
|
||||
get_or_create_column_info(column)->set_cost(column_cost);
|
||||
}
|
||||
std::string get_variable_name(unsigned j) const override;
|
||||
|
||||
void set_row_column_coefficient(unsigned row, unsigned column, T const & val) {
|
||||
m_A_values[row][column] = val;
|
||||
}
|
||||
// returns the current cost
|
||||
virtual T get_current_cost() const = 0;
|
||||
// do not have to call it
|
||||
void give_symbolic_name_to_column(std::string name, unsigned column);
|
||||
|
||||
virtual T get_column_value(unsigned column) const = 0;
|
||||
|
||||
T get_column_value_by_name(std::string name) const;
|
||||
|
||||
// returns -1 if not found
|
||||
virtual int get_column_index_by_name(std::string name) const;
|
||||
|
||||
void set_lower_bound(unsigned i, T bound) {
|
||||
column_info<T> *ci = get_or_create_column_info(i);
|
||||
ci->set_lower_bound(bound);
|
||||
}
|
||||
|
||||
void set_upper_bound(unsigned i, T bound) {
|
||||
column_info<T> *ci = get_or_create_column_info(i);
|
||||
ci->set_upper_bound(bound);
|
||||
}
|
||||
|
||||
void unset_lower_bound(unsigned i) {
|
||||
get_or_create_column_info(i)->unset_lower_bound();
|
||||
}
|
||||
|
||||
void unset_upper_bound(unsigned i) {
|
||||
get_or_create_column_info(i)->unset_upper_bound();
|
||||
}
|
||||
|
||||
void set_fixed_value(unsigned i, T val) {
|
||||
column_info<T> *ci = get_or_create_column_info(i);
|
||||
ci->set_fixed_value(val);
|
||||
}
|
||||
|
||||
void unset_fixed_value(unsigned i) {
|
||||
get_or_create_column_info(i)->unset_fixed();
|
||||
}
|
||||
|
||||
lp_status get_status() const {
|
||||
return m_status;
|
||||
}
|
||||
|
||||
void set_status(lp_status st) {
|
||||
m_status = st;
|
||||
}
|
||||
|
||||
|
||||
~lp_solver() override;
|
||||
|
||||
void flip_costs();
|
||||
|
||||
virtual void find_maximal_solution() = 0;
|
||||
void set_time_limit(unsigned time_limit_in_seconds) {
|
||||
m_settings.time_limit = time_limit_in_seconds;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
bool problem_is_empty();
|
||||
|
||||
void scale();
|
||||
|
||||
|
||||
void print_rows_scale_stats(std::ostream & out);
|
||||
|
||||
void print_columns_scale_stats(std::ostream & out);
|
||||
|
||||
void print_row_scale_stats(unsigned i, std::ostream & out);
|
||||
|
||||
void print_column_scale_stats(unsigned j, std::ostream & out);
|
||||
|
||||
void print_scale_stats(std::ostream & out);
|
||||
|
||||
void get_max_abs_in_row(std::unordered_map<unsigned, T> & row_map);
|
||||
|
||||
void pin_vars_down_on_row(std::unordered_map<unsigned, T> & row) {
|
||||
pin_vars_on_row_with_sign(row, - numeric_traits<T>::one());
|
||||
}
|
||||
|
||||
void pin_vars_up_on_row(std::unordered_map<unsigned, T> & row) {
|
||||
pin_vars_on_row_with_sign(row, numeric_traits<T>::one());
|
||||
}
|
||||
|
||||
void pin_vars_on_row_with_sign(std::unordered_map<unsigned, T> & row, T sign );
|
||||
|
||||
bool get_minimal_row_value(std::unordered_map<unsigned, T> & row, T & lower_bound);
|
||||
|
||||
bool get_maximal_row_value(std::unordered_map<unsigned, T> & row, T & lower_bound);
|
||||
|
||||
bool row_is_zero(std::unordered_map<unsigned, T> & row);
|
||||
|
||||
bool row_e_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index);
|
||||
|
||||
bool row_ge_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index);
|
||||
|
||||
bool row_le_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index);
|
||||
|
||||
// analyse possible max and min values that are derived from var boundaries
|
||||
// Let us say that the we have a "ge" constraint, and the min value is equal to the rs.
|
||||
// Then we know what values of the variables are. For each positive coeff of the row it has to be
|
||||
// the low boundary of the var and for a negative - the upper.
|
||||
|
||||
// this routing also pins the variables to the boundaries
|
||||
bool row_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index );
|
||||
|
||||
void remove_fixed_or_zero_columns();
|
||||
|
||||
void remove_fixed_or_zero_columns_from_row(unsigned i, std::unordered_map<unsigned, T> & row);
|
||||
|
||||
unsigned try_to_remove_some_rows();
|
||||
|
||||
void cleanup();
|
||||
|
||||
void map_external_rows_to_core_solver_rows();
|
||||
|
||||
void map_external_columns_to_core_solver_columns();
|
||||
|
||||
unsigned number_of_core_structurals() {
|
||||
return static_cast<unsigned>(m_core_solver_columns_to_external_columns.size());
|
||||
}
|
||||
|
||||
void restore_column_scales_to_one() {
|
||||
for (unsigned i = 0; i < m_column_scale.size(); i++) m_column_scale[i] = numeric_traits<T>::one();
|
||||
}
|
||||
|
||||
void unscale();
|
||||
|
||||
void fill_A_from_A_values();
|
||||
|
||||
void fill_matrix_A_and_init_right_side();
|
||||
|
||||
void count_slacks_and_artificials();
|
||||
|
||||
void count_slacks_and_artificials_for_row(unsigned i);
|
||||
|
||||
T lower_bound_shift_for_row(unsigned i);
|
||||
|
||||
void fill_m_b();
|
||||
|
||||
T get_column_value_with_core_solver(unsigned column, lp_core_solver_base<T, X> * core_solver) const;
|
||||
void set_scaled_cost(unsigned j);
|
||||
void print_statistics_on_A(std::ostream & out) {
|
||||
out << "extended A[" << this->m_A->row_count() << "," << this->m_A->column_count() << "]" << std::endl;
|
||||
}
|
||||
|
||||
public:
|
||||
lp_settings & settings() { return m_settings;}
|
||||
void print_model(std::ostream & s) const {
|
||||
s << "objective = " << get_current_cost() << std::endl;
|
||||
s << "column values\n";
|
||||
for (auto & it : m_names_to_columns) {
|
||||
s << it.first << " = " << get_column_value(it.second) << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,571 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_solver.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X> column_info<T> * lp_solver<T, X>::get_or_create_column_info(unsigned column) {
|
||||
auto it = m_map_from_var_index_to_column_info.find(column);
|
||||
return (it == m_map_from_var_index_to_column_info.end())? (m_map_from_var_index_to_column_info[column] = new column_info<T>()) : it->second;
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
std::string lp_solver<T, X>::get_variable_name(unsigned j) const { // j here is the core solver index
|
||||
if (!m_settings.print_external_var_name())
|
||||
return std::string("j")+T_to_string(j);
|
||||
auto it = this->m_core_solver_columns_to_external_columns.find(j);
|
||||
if (it == this->m_core_solver_columns_to_external_columns.end())
|
||||
return std::string("x")+T_to_string(j);
|
||||
unsigned external_j = it->second;
|
||||
auto t = this->m_map_from_var_index_to_column_info.find(external_j);
|
||||
if (t == this->m_map_from_var_index_to_column_info.end()) {
|
||||
return std::string("x") +T_to_string(external_j);
|
||||
}
|
||||
return t->second->get_name();
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_solver<T, X>::get_column_cost_value(unsigned j, column_info<T> * ci) const {
|
||||
if (ci->is_fixed()) {
|
||||
return ci->get_cost() * ci->get_fixed_value();
|
||||
}
|
||||
return ci->get_cost() * get_column_value(j);
|
||||
}
|
||||
template <typename T, typename X> void lp_solver<T, X>::add_constraint(lp_relation relation, T right_side, unsigned row_index) {
|
||||
lp_assert(m_constraints.find(row_index) == m_constraints.end());
|
||||
lp_constraint<T, X> cs(right_side, relation);
|
||||
m_constraints[row_index] = cs;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::give_symbolic_name_to_column(std::string name, unsigned column) {
|
||||
auto it = m_map_from_var_index_to_column_info.find(column);
|
||||
column_info<T> *ci;
|
||||
if (it == m_map_from_var_index_to_column_info.end()){
|
||||
m_map_from_var_index_to_column_info[column] = ci = new column_info<T>;
|
||||
} else {
|
||||
ci = it->second;
|
||||
}
|
||||
ci->set_name(name);
|
||||
m_names_to_columns[name] = column;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> T lp_solver<T, X>::get_column_value_by_name(std::string name) const {
|
||||
auto it = m_names_to_columns.find(name);
|
||||
if (it == m_names_to_columns.end()) {
|
||||
std::stringstream s;
|
||||
s << "get_column_value_by_name " << name;
|
||||
throw_exception(s.str());
|
||||
}
|
||||
return get_column_value(it -> second);
|
||||
}
|
||||
|
||||
// returns -1 if not found
|
||||
template <typename T, typename X> int lp_solver<T, X>::get_column_index_by_name(std::string name) const {
|
||||
auto t = m_names_to_columns.find(name);
|
||||
if (t == m_names_to_columns.end()) {
|
||||
return -1;
|
||||
}
|
||||
return t->second;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> lp_solver<T, X>::~lp_solver(){
|
||||
delete m_A;
|
||||
for (auto t : m_map_from_var_index_to_column_info) {
|
||||
delete t.second;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::flip_costs() {
|
||||
for (auto t : m_map_from_var_index_to_column_info) {
|
||||
column_info<T> *ci = t.second;
|
||||
ci->set_cost(-ci->get_cost());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::problem_is_empty() {
|
||||
for (auto & c : m_A_values)
|
||||
if (!c.second.empty())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::scale() {
|
||||
if (numeric_traits<T>::precise() || m_settings.use_scaling == false) {
|
||||
m_column_scale.clear();
|
||||
m_column_scale.resize(m_A->column_count(), one_of_type<T>());
|
||||
return;
|
||||
}
|
||||
|
||||
T smin = T(m_settings.scaling_minimum);
|
||||
T smax = T(m_settings.scaling_maximum);
|
||||
|
||||
scaler<T, X> scaler(m_b, *m_A, smin, smax, m_column_scale, this->m_settings);
|
||||
if (!scaler.scale()) {
|
||||
unscale();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::print_rows_scale_stats(std::ostream & out) {
|
||||
out << "rows max" << std::endl;
|
||||
for (unsigned i = 0; i < m_A->row_count(); i++) {
|
||||
print_row_scale_stats(i, out);
|
||||
}
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::print_columns_scale_stats(std::ostream & out) {
|
||||
out << "columns max" << std::endl;
|
||||
for (unsigned i = 0; i < m_A->column_count(); i++) {
|
||||
print_column_scale_stats(i, out);
|
||||
}
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::print_row_scale_stats(unsigned i, std::ostream & out) {
|
||||
out << "(" << std::min(m_A->get_min_abs_in_row(i), abs(m_b[i])) << " ";
|
||||
out << std::max(m_A->get_max_abs_in_row(i), abs(m_b[i])) << ")";
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::print_column_scale_stats(unsigned j, std::ostream & out) {
|
||||
out << "(" << m_A->get_min_abs_in_row(j) << " ";
|
||||
out << m_A->get_max_abs_in_column(j) << ")";
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::print_scale_stats(std::ostream & out) {
|
||||
print_rows_scale_stats(out);
|
||||
print_columns_scale_stats(out);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::get_max_abs_in_row(std::unordered_map<unsigned, T> & row_map) {
|
||||
T ret = numeric_traits<T>::zero();
|
||||
for (auto jp : row_map) {
|
||||
T ac = numeric_traits<T>::abs(jp->second);
|
||||
if (ac > ret) {
|
||||
ret = ac;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::pin_vars_on_row_with_sign(std::unordered_map<unsigned, T> & row, T sign ) {
|
||||
for (auto t : row) {
|
||||
unsigned j = t.first;
|
||||
column_info<T> * ci = m_map_from_var_index_to_column_info[j];
|
||||
T a = t.second;
|
||||
if (a * sign > numeric_traits<T>::zero()) {
|
||||
lp_assert(ci->upper_bound_is_set());
|
||||
ci->set_fixed_value(ci->get_upper_bound());
|
||||
} else {
|
||||
lp_assert(ci->lower_bound_is_set());
|
||||
ci->set_fixed_value(ci->get_lower_bound());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::get_minimal_row_value(std::unordered_map<unsigned, T> & row, T & lower_bound) {
|
||||
lower_bound = numeric_traits<T>::zero();
|
||||
for (auto & t : row) {
|
||||
T a = t.second;
|
||||
column_info<T> * ci = m_map_from_var_index_to_column_info[t.first];
|
||||
if (a > numeric_traits<T>::zero()) {
|
||||
if (ci->lower_bound_is_set()) {
|
||||
lower_bound += ci->get_lower_bound() * a;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (ci->upper_bound_is_set()) {
|
||||
lower_bound += ci->get_upper_bound() * a;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::get_maximal_row_value(std::unordered_map<unsigned, T> & row, T & lower_bound) {
|
||||
lower_bound = numeric_traits<T>::zero();
|
||||
for (auto & t : row) {
|
||||
T a = t.second;
|
||||
column_info<T> * ci = m_map_from_var_index_to_column_info[t.first];
|
||||
if (a < numeric_traits<T>::zero()) {
|
||||
if (ci->lower_bound_is_set()) {
|
||||
lower_bound += ci->get_lower_bound() * a;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (ci->upper_bound_is_set()) {
|
||||
lower_bound += ci->get_upper_bound() * a;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::row_is_zero(std::unordered_map<unsigned, T> & row) {
|
||||
for (auto & t : row) {
|
||||
if (!is_zero(t.second))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::row_e_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index) {
|
||||
T rs = m_constraints[row_index].m_rs;
|
||||
if (row_is_zero(row)) {
|
||||
if (!is_zero(rs))
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
|
||||
T lower_bound;
|
||||
bool lb = get_minimal_row_value(row, lower_bound);
|
||||
if (lb) {
|
||||
T diff = lower_bound - rs;
|
||||
if (!val_is_smaller_than_eps(diff, m_settings.refactor_tolerance)){
|
||||
// lower_bound > rs + m_settings.refactor_epsilon
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
if (val_is_smaller_than_eps(-diff, m_settings.refactor_tolerance)){
|
||||
pin_vars_down_on_row(row);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
T upper_bound;
|
||||
bool ub = get_maximal_row_value(row, upper_bound);
|
||||
if (ub) {
|
||||
T diff = rs - upper_bound;
|
||||
if (!val_is_smaller_than_eps(diff, m_settings.refactor_tolerance)) {
|
||||
// upper_bound < rs - m_settings.refactor_tolerance
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
if (val_is_smaller_than_eps(-diff, m_settings.refactor_tolerance)){
|
||||
pin_vars_up_on_row(row);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::row_ge_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index) {
|
||||
T rs = m_constraints[row_index].m_rs;
|
||||
if (row_is_zero(row)) {
|
||||
if (rs > zero_of_type<X>())
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
|
||||
T upper_bound;
|
||||
if (get_maximal_row_value(row, upper_bound)) {
|
||||
T diff = rs - upper_bound;
|
||||
if (!val_is_smaller_than_eps(diff, m_settings.refactor_tolerance)) {
|
||||
// upper_bound < rs - m_settings.refactor_tolerance
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
if (val_is_smaller_than_eps(-diff, m_settings.refactor_tolerance)){
|
||||
pin_vars_up_on_row(row);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool lp_solver<T, X>::row_le_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index) {
|
||||
T lower_bound;
|
||||
T rs = m_constraints[row_index].m_rs;
|
||||
if (row_is_zero(row)) {
|
||||
if (rs < zero_of_type<X>())
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (get_minimal_row_value(row, lower_bound)) {
|
||||
T diff = lower_bound - rs;
|
||||
if (!val_is_smaller_than_eps(diff, m_settings.refactor_tolerance)){
|
||||
// lower_bound > rs + m_settings.refactor_tolerance
|
||||
m_status = lp_status::INFEASIBLE;
|
||||
return true;
|
||||
}
|
||||
if (val_is_smaller_than_eps(-diff, m_settings.refactor_tolerance)){
|
||||
pin_vars_down_on_row(row);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// analyse possible max and min values that are derived from var boundaries
|
||||
// Let us say that the we have a "ge" constraint, and the min value is equal to the rs.
|
||||
// Then we know what values of the variables are. For each positive coeff of the row it has to be
|
||||
// the low boundary of the var and for a negative - the upper.
|
||||
|
||||
// this routing also pins the variables to the boundaries
|
||||
template <typename T, typename X> bool lp_solver<T, X>::row_is_obsolete(std::unordered_map<unsigned, T> & row, unsigned row_index ) {
|
||||
auto & constraint = m_constraints[row_index];
|
||||
switch (constraint.m_relation) {
|
||||
case lp_relation::Equal:
|
||||
return row_e_is_obsolete(row, row_index);
|
||||
|
||||
case lp_relation::Greater_or_equal:
|
||||
return row_ge_is_obsolete(row, row_index);
|
||||
|
||||
case lp_relation::Less_or_equal:
|
||||
return row_le_is_obsolete(row, row_index);
|
||||
}
|
||||
lp_unreachable();
|
||||
return false; // it is unreachable
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::remove_fixed_or_zero_columns() {
|
||||
for (auto & i_row : m_A_values) {
|
||||
remove_fixed_or_zero_columns_from_row(i_row.first, i_row.second);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::remove_fixed_or_zero_columns_from_row(unsigned i, std::unordered_map<unsigned, T> & row) {
|
||||
auto & constraint = m_constraints[i];
|
||||
vector<unsigned> removed;
|
||||
for (auto & col : row) {
|
||||
unsigned j = col.first;
|
||||
lp_assert(m_map_from_var_index_to_column_info.find(j) != m_map_from_var_index_to_column_info.end());
|
||||
column_info<T> * ci = m_map_from_var_index_to_column_info[j];
|
||||
if (ci->is_fixed()) {
|
||||
removed.push_back(j);
|
||||
T aj = col.second;
|
||||
constraint.m_rs -= aj * ci->get_fixed_value();
|
||||
} else {
|
||||
if (numeric_traits<T>::is_zero(col.second)){
|
||||
removed.push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto j : removed) {
|
||||
row.erase(j);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> unsigned lp_solver<T, X>::try_to_remove_some_rows() {
|
||||
vector<unsigned> rows_to_delete;
|
||||
for (auto & t : m_A_values) {
|
||||
if (row_is_obsolete(t.second, t.first)) {
|
||||
rows_to_delete.push_back(t.first);
|
||||
}
|
||||
|
||||
if (m_status == lp_status::INFEASIBLE) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (!rows_to_delete.empty()) {
|
||||
for (unsigned k : rows_to_delete) {
|
||||
m_A_values.erase(k);
|
||||
}
|
||||
}
|
||||
remove_fixed_or_zero_columns();
|
||||
return static_cast<unsigned>(rows_to_delete.size());
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::cleanup() {
|
||||
int n = 0; // number of deleted rows
|
||||
int d;
|
||||
while ((d = try_to_remove_some_rows()) > 0)
|
||||
n += d;
|
||||
|
||||
if (n == 1) {
|
||||
LP_OUT(m_settings, "deleted one row" << std::endl);
|
||||
} else if (n) {
|
||||
LP_OUT(m_settings, "deleted " << n << " rows" << std::endl);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::map_external_rows_to_core_solver_rows() {
|
||||
unsigned size = 0;
|
||||
for (auto & row : m_A_values) {
|
||||
m_external_rows_to_core_solver_rows[row.first] = size;
|
||||
m_core_solver_rows_to_external_rows[size] = row.first;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::map_external_columns_to_core_solver_columns() {
|
||||
unsigned size = 0;
|
||||
for (auto & row : m_A_values) {
|
||||
for (auto & col : row.second) {
|
||||
if (col.second == numeric_traits<T>::zero() || m_map_from_var_index_to_column_info[col.first]->is_fixed()) {
|
||||
throw_exception("found fixed column");
|
||||
}
|
||||
unsigned j = col.first;
|
||||
auto column_info_it = m_map_from_var_index_to_column_info.find(j);
|
||||
lp_assert(column_info_it != m_map_from_var_index_to_column_info.end());
|
||||
|
||||
auto j_column = column_info_it->second->get_column_index();
|
||||
if (!is_valid(j_column)) { // j is a newcomer
|
||||
m_map_from_var_index_to_column_info[j]->set_column_index(size);
|
||||
m_core_solver_columns_to_external_columns[size++] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::unscale() {
|
||||
delete m_A;
|
||||
m_A = nullptr;
|
||||
fill_A_from_A_values();
|
||||
restore_column_scales_to_one();
|
||||
fill_m_b();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::fill_A_from_A_values() {
|
||||
m_A = new static_matrix<T, X>(static_cast<unsigned>(m_A_values.size()), number_of_core_structurals());
|
||||
for (auto & t : m_A_values) {
|
||||
auto row_it = m_external_rows_to_core_solver_rows.find(t.first);
|
||||
lp_assert(row_it != m_external_rows_to_core_solver_rows.end());
|
||||
unsigned row = row_it->second;
|
||||
for (auto k : t.second) {
|
||||
auto column_info_it = m_map_from_var_index_to_column_info.find(k.first);
|
||||
lp_assert(column_info_it != m_map_from_var_index_to_column_info.end());
|
||||
column_info<T> *ci = column_info_it->second;
|
||||
unsigned col = ci->get_column_index();
|
||||
lp_assert(is_valid(col));
|
||||
bool col_is_flipped = m_map_from_var_index_to_column_info[k.first]->is_flipped();
|
||||
if (!col_is_flipped) {
|
||||
(*m_A)(row, col) = k.second;
|
||||
} else {
|
||||
(*m_A)(row, col) = - k.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::fill_matrix_A_and_init_right_side() {
|
||||
map_external_rows_to_core_solver_rows();
|
||||
map_external_columns_to_core_solver_columns();
|
||||
lp_assert(m_A == nullptr);
|
||||
fill_A_from_A_values();
|
||||
m_b.resize(m_A->row_count());
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::count_slacks_and_artificials() {
|
||||
for (int i = row_count() - 1; i >= 0; i--) {
|
||||
count_slacks_and_artificials_for_row(i);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::count_slacks_and_artificials_for_row(unsigned i) {
|
||||
lp_assert(this->m_constraints.find(this->m_core_solver_rows_to_external_rows[i]) != this->m_constraints.end());
|
||||
auto & constraint = this->m_constraints[this->m_core_solver_rows_to_external_rows[i]];
|
||||
switch (constraint.m_relation) {
|
||||
case Equal:
|
||||
m_artificials++;
|
||||
break;
|
||||
case Greater_or_equal:
|
||||
m_slacks++;
|
||||
if (this->m_b[i] > 0) {
|
||||
m_artificials++;
|
||||
}
|
||||
break;
|
||||
case Less_or_equal:
|
||||
m_slacks++;
|
||||
if (this->m_b[i] < 0) {
|
||||
m_artificials++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_solver<T, X>::lower_bound_shift_for_row(unsigned i) {
|
||||
T ret = numeric_traits<T>::zero();
|
||||
|
||||
auto row = this->m_A_values.find(i);
|
||||
if (row == this->m_A_values.end()) {
|
||||
throw_exception("cannot find row");
|
||||
}
|
||||
for (auto col : row->second) {
|
||||
ret += col.second * this->m_map_from_var_index_to_column_info[col.first]->get_shift();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::fill_m_b() {
|
||||
for (int i = this->row_count() - 1; i >= 0; i--) {
|
||||
lp_assert(this->m_constraints.find(this->m_core_solver_rows_to_external_rows[i]) != this->m_constraints.end());
|
||||
unsigned external_i = this->m_core_solver_rows_to_external_rows[i];
|
||||
auto & constraint = this->m_constraints[external_i];
|
||||
this->m_b[i] = constraint.m_rs - lower_bound_shift_for_row(external_i);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> T lp_solver<T, X>::get_column_value_with_core_solver(unsigned column, lp_core_solver_base<T, X> * core_solver) const {
|
||||
auto cit = this->m_map_from_var_index_to_column_info.find(column);
|
||||
if (cit == this->m_map_from_var_index_to_column_info.end()) {
|
||||
return numeric_traits<T>::zero();
|
||||
}
|
||||
|
||||
column_info<T> * ci = cit->second;
|
||||
|
||||
if (ci->is_fixed()) {
|
||||
return ci->get_fixed_value();
|
||||
}
|
||||
|
||||
unsigned cj = ci->get_column_index();
|
||||
if (cj != static_cast<unsigned>(-1)) {
|
||||
T v = core_solver->get_var_value(cj) * this->m_column_scale[cj];
|
||||
if (ci->is_free()) {
|
||||
return v;
|
||||
}
|
||||
if (!ci->is_flipped()) {
|
||||
return v + ci->get_lower_bound();
|
||||
}
|
||||
|
||||
// the flipped case when there is only upper bound
|
||||
return -v + ci->get_upper_bound(); //
|
||||
}
|
||||
|
||||
return numeric_traits<T>::zero(); // returns zero for out of boundary columns
|
||||
}
|
||||
|
||||
template <typename T, typename X> void lp_solver<T, X>::set_scaled_cost(unsigned j) {
|
||||
// grab original costs but modify it with the column scales
|
||||
lp_assert(j < this->m_column_scale.size());
|
||||
column_info<T> * ci = this->m_map_from_var_index_to_column_info[this->m_core_solver_columns_to_external_columns[j]];
|
||||
T cost = ci->get_cost();
|
||||
if (ci->is_flipped()){
|
||||
cost *= T(-1);
|
||||
}
|
||||
lp_assert(ci->is_fixed() == false);
|
||||
this->m_costs[j] = cost * this->m_column_scale[j];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include "math/lp/lp_utils.h"
|
||||
#ifdef lp_for_z3
|
||||
namespace lp {
|
||||
double numeric_traits<double>::g_zero = 0.0;
|
||||
double numeric_traits<double>::g_one = 1.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -141,7 +141,6 @@ inline void throw_exception(std::string && str) {
|
|||
typedef z3_exception exception;
|
||||
|
||||
#define lp_assert(_x_) { SASSERT(_x_); }
|
||||
inline void lp_unreachable() { lp_assert(false); }
|
||||
template <typename X> inline X zero_of_type() { return numeric_traits<X>::zero(); }
|
||||
template <typename X> inline X one_of_type() { return numeric_traits<X>::one(); }
|
||||
template <typename X> inline bool is_zero(const X & v) { return numeric_traits<X>::is_zero(v); }
|
||||
|
|
@ -153,9 +152,6 @@ template <typename X> inline X ceil_ratio(const X & a, const X & b) { return num
|
|||
template <typename X> inline X floor_ratio(const X & a, const X & b) { return numeric_traits<X>::floor_ratio(a, b); }
|
||||
|
||||
|
||||
template <typename X> inline bool precise() { return numeric_traits<X>::precise(); }
|
||||
|
||||
|
||||
// returns true if a factor of b
|
||||
template <typename T>
|
||||
bool is_proper_factor(const T & a, const T & b) {
|
||||
|
|
|
|||
|
|
@ -1,84 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include "util/debug.h"
|
||||
#include "math/lp/lu_def.h"
|
||||
namespace lp {
|
||||
template double dot_product<double, double>(vector<double> const&, vector<double> const&);
|
||||
template lu<static_matrix<double, double>>::lu(static_matrix<double, double> const&, vector<unsigned int>&, lp_settings&);
|
||||
template void lu<static_matrix<double, double>>::push_matrix_to_tail(tail_matrix<double, double>*);
|
||||
template void lu<static_matrix<double, double>>::replace_column(double, indexed_vector<double>&, unsigned);
|
||||
template void lu<static_matrix<double, double>>::solve_Bd(unsigned int, indexed_vector<double>&, indexed_vector<double>&);
|
||||
template lu<static_matrix<double, double>>::~lu();
|
||||
template void lu<static_matrix<mpq, mpq>>::push_matrix_to_tail(tail_matrix<mpq, mpq>*);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_Bd(unsigned int, indexed_vector<mpq>&, indexed_vector<mpq>&);
|
||||
template lu<static_matrix<mpq, mpq>>::~lu();
|
||||
template void lu<static_matrix<mpq, impq>>::push_matrix_to_tail(tail_matrix<mpq, impq >*);
|
||||
template void lu<static_matrix<mpq, impq>>::solve_Bd(unsigned int, indexed_vector<mpq>&, indexed_vector<mpq>&);
|
||||
template lu<static_matrix<mpq, impq>>::~lu();
|
||||
template mpq dot_product<mpq, mpq>(vector<mpq > const&, vector<mpq > const&);
|
||||
template void init_factorization<static_matrix<double, double>>
|
||||
(lu<static_matrix<double, double>>*&, static_matrix<double, double>&, vector<unsigned int>&, lp_settings&);
|
||||
template void init_factorization<static_matrix<mpq, mpq>>
|
||||
(lu<static_matrix<mpq,mpq>>*&, static_matrix<mpq, mpq>&, vector<unsigned int>&, lp_settings&);
|
||||
template void init_factorization<static_matrix<mpq, impq>>(lu<static_matrix<mpq, impq> >*&, static_matrix<mpq, impq >&, vector<unsigned int>&, lp_settings&);
|
||||
template void print_matrix<square_sparse_matrix<double, double>>(square_sparse_matrix<double, double>&, std::ostream & out);
|
||||
template void print_matrix<static_matrix<mpq,mpq>>(static_matrix<mpq, mpq>&, std::ostream&);
|
||||
template void print_matrix<static_matrix<mpq, impq> >(static_matrix<mpq, impq >&, std::ostream&);
|
||||
template void print_matrix<static_matrix<double, double>>(static_matrix<double, double>&, std::ostream & out);
|
||||
#ifdef Z3DEBUG
|
||||
template bool lu<static_matrix<double, double>>::is_correct(const vector<unsigned>& basis);
|
||||
template bool lu<static_matrix<mpq, impq>>::is_correct( vector<unsigned int> const &);
|
||||
template dense_matrix<double, double> get_B<static_matrix<double, double>>(lu<static_matrix<double, double>>&, const vector<unsigned>& basis);
|
||||
template dense_matrix<mpq, mpq> get_B<static_matrix<mpq, mpq>>(lu<static_matrix<mpq, mpq>>&, vector<unsigned int> const&);
|
||||
|
||||
#endif
|
||||
|
||||
template bool lu<static_matrix<double, double>>::pivot_the_row(int); // NOLINT
|
||||
template void lu<static_matrix<double, double>>::init_vector_w(unsigned int, indexed_vector<double>&);
|
||||
template void lu<static_matrix<double, double>>::solve_By(vector<double>&);
|
||||
template void lu<static_matrix<double, double>>::solve_By_when_y_is_ready_for_X(vector<double>&);
|
||||
template void lu<static_matrix<double, double>>::solve_yB_with_error_check(vector<double>&, const vector<unsigned>& basis);
|
||||
template void lu<static_matrix<double, double>>::solve_yB_with_error_check_indexed(indexed_vector<double>&, vector<int> const&, const vector<unsigned> & basis, const lp_settings&);
|
||||
template void lu<static_matrix<mpq, mpq>>::replace_column(mpq, indexed_vector<mpq>&, unsigned);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_By(vector<mpq >&);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_By_when_y_is_ready_for_X(vector<mpq >&);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_yB_with_error_check(vector<mpq >&, const vector<unsigned>& basis);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_yB_with_error_check_indexed(indexed_vector<mpq>&, vector< int > const&, const vector<unsigned> & basis, const lp_settings&);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_yB_with_error_check_indexed(indexed_vector<mpq>&, vector< int > const&, const vector<unsigned> & basis, const lp_settings&);
|
||||
template void lu<static_matrix<mpq, impq> >::init_vector_w(unsigned int, indexed_vector<mpq>&);
|
||||
template void lu<static_matrix<mpq, impq> >::replace_column(mpq, indexed_vector<mpq>&, unsigned);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_Bd_faster(unsigned int, indexed_vector<mpq>&);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_By(vector<impq >&);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_By_when_y_is_ready_for_X(vector<impq >&);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_yB_with_error_check(vector<mpq >&, const vector<unsigned>& basis);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_By(indexed_vector<mpq>&);
|
||||
template void lu<static_matrix<double, double>>::solve_By(indexed_vector<double>&);
|
||||
template void lu<static_matrix<double, double>>::solve_yB_indexed(indexed_vector<double>&);
|
||||
template void lu<static_matrix<mpq, impq> >::solve_yB_indexed(indexed_vector<mpq>&);
|
||||
template void lu<static_matrix<mpq, mpq>>::solve_By_for_T_indexed_only(indexed_vector<mpq>&, lp_settings const&);
|
||||
template void lu<static_matrix<double, double>>::solve_By_for_T_indexed_only(indexed_vector<double>&, lp_settings const&);
|
||||
#ifdef Z3DEBUG
|
||||
template void print_matrix<tail_matrix<double, double>>(tail_matrix<double, double>&, std::ostream&);
|
||||
#endif
|
||||
}
|
||||
383
src/math/lp/lu.h
383
src/math/lp/lu.h
|
|
@ -1,383 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
for matrix B we have
|
||||
t0*...*tn-1*B = Q*U*R
|
||||
here ti are matrices corresponding to pivot operations,
|
||||
including columns and rows swaps,
|
||||
or a multiplication matrix row by a number
|
||||
Q, R - permutations and U is an upper triangular matrix
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/vector.h"
|
||||
#include "util/debug.h"
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include "math/lp/square_sparse_matrix.h"
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include <string>
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include <ostream>
|
||||
#include <fstream>
|
||||
#include "math/lp/row_eta_matrix.h"
|
||||
#include "math/lp/square_dense_submatrix.h"
|
||||
#include "math/lp/dense_matrix.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X> // print the nr x nc submatrix at the top left corner
|
||||
void print_submatrix(square_sparse_matrix<T, X> & m, unsigned mr, unsigned nc);
|
||||
|
||||
template <typename M>
|
||||
void print_matrix(M &m, std::ostream & out);
|
||||
|
||||
template <typename T, typename X>
|
||||
X dot_product(const vector<T> & a, const vector<X> & b) {
|
||||
lp_assert(a.size() == b.size());
|
||||
auto r = zero_of_type<X>();
|
||||
for (unsigned i = 0; i < a.size(); i++) {
|
||||
r += a[i] * b[i];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X>
|
||||
class one_elem_on_diag: public tail_matrix<T, X> {
|
||||
unsigned m_i;
|
||||
T m_val;
|
||||
public:
|
||||
one_elem_on_diag(unsigned i, T val) : m_i(i), m_val(val) {
|
||||
#ifdef Z3DEBUG
|
||||
m_one_over_val = numeric_traits<T>::one() / m_val;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool is_dense() const override { return false; }
|
||||
|
||||
one_elem_on_diag(const one_elem_on_diag & o);
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
unsigned m_m;
|
||||
unsigned m_n;
|
||||
void set_number_of_rows(unsigned m) override { m_m = m; m_n = m; }
|
||||
void set_number_of_columns(unsigned n) override { m_m = n; m_n = n; }
|
||||
T m_one_over_val;
|
||||
|
||||
T get_elem (unsigned i, unsigned j) const override;
|
||||
|
||||
unsigned row_count() const override { return m_m; } // not defined }
|
||||
unsigned column_count() const override { return m_m; } // not defined }
|
||||
#endif
|
||||
void apply_from_left(vector<X> & w, lp_settings &) override {
|
||||
w[m_i] /= m_val;
|
||||
}
|
||||
|
||||
void apply_from_right(vector<T> & w) override {
|
||||
w[m_i] /= m_val;
|
||||
}
|
||||
|
||||
void apply_from_right(indexed_vector<T> & w) override {
|
||||
if (is_zero(w.m_data[m_i]))
|
||||
return;
|
||||
auto & v = w.m_data[m_i] /= m_val;
|
||||
if (lp_settings::is_eps_small_general(v, 1e-14)) {
|
||||
w.erase_from_index(m_i);
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) override;
|
||||
|
||||
void conjugate_by_permutation(permutation_matrix<T, X> & p) {
|
||||
// this = p * this * p(-1)
|
||||
#ifdef Z3DEBUG
|
||||
// auto rev = p.get_reverse();
|
||||
// auto deb = ((*this) * rev);
|
||||
// deb = p * deb;
|
||||
#endif
|
||||
m_i = p.apply_reverse(m_i);
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(*this == deb);
|
||||
#endif
|
||||
}
|
||||
}; // end of one_elem_on_diag
|
||||
|
||||
enum class LU_status { OK, Degenerated};
|
||||
|
||||
// This class supports updates of the columns of B, and solves systems Bx=b,and yB=c
|
||||
// Using Suhl-Suhl method described in the dissertation of Achim Koberstein, Chapter 5
|
||||
template <typename M>
|
||||
class lu {
|
||||
LU_status m_status;
|
||||
public:
|
||||
typedef typename M::coefftype T;
|
||||
typedef typename M::argtype X;
|
||||
|
||||
// the fields
|
||||
unsigned m_dim;
|
||||
const M & m_A;
|
||||
permutation_matrix<T, X> m_Q;
|
||||
permutation_matrix<T, X> m_R;
|
||||
permutation_matrix<T, X> m_r_wave;
|
||||
square_sparse_matrix<T, X> m_U;
|
||||
square_dense_submatrix<T, X>* m_dense_LU;
|
||||
|
||||
vector<tail_matrix<T, X> *> m_tail;
|
||||
lp_settings & m_settings;
|
||||
bool m_failure;
|
||||
indexed_vector<T> m_row_eta_work_vector;
|
||||
indexed_vector<T> m_w_for_extension;
|
||||
indexed_vector<T> m_y_copy;
|
||||
indexed_vector<unsigned> m_ii; //to optimize the work with the m_index fields
|
||||
unsigned m_refactor_counter;
|
||||
// constructor
|
||||
// if A is an m by n matrix then basis has length m and values in [0,n); the values are all different
|
||||
// they represent the set of m columns
|
||||
lu(const M & A,
|
||||
vector<unsigned>& basis,
|
||||
lp_settings & settings);
|
||||
lu(const M & A, lp_settings&);
|
||||
void debug_test_of_basis(const M & A, vector<unsigned> & basis);
|
||||
void solve_Bd_when_w_is_ready(vector<T> & d, indexed_vector<T>& w );
|
||||
void solve_By(indexed_vector<X> & y);
|
||||
|
||||
void solve_By(vector<X> & y);
|
||||
|
||||
void solve_By_for_T_indexed_only(indexed_vector<T>& y, const lp_settings &);
|
||||
|
||||
template <typename L>
|
||||
void solve_By_when_y_is_ready(indexed_vector<L> & y);
|
||||
void solve_By_when_y_is_ready_for_X(vector<X> & y);
|
||||
void solve_By_when_y_is_ready_for_T(vector<T> & y, vector<unsigned> & index);
|
||||
void print_indexed_vector(indexed_vector<T> & w, std::ofstream & f);
|
||||
|
||||
void print_matrix_compact(std::ostream & f);
|
||||
|
||||
void print(indexed_vector<T> & w, const vector<unsigned>& basis);
|
||||
void solve_Bd(unsigned a_column, vector<T> & d, indexed_vector<T> & w);
|
||||
void solve_Bd(unsigned a_column, indexed_vector<T> & d, indexed_vector<T> & w);
|
||||
void solve_Bd_faster(unsigned a_column, indexed_vector<T> & d); // d is the right side on the input and the solution at the exit
|
||||
|
||||
void solve_yB(vector<T>& y);
|
||||
|
||||
void solve_yB_indexed(indexed_vector<T>& y);
|
||||
|
||||
void add_delta_to_solution_indexed(indexed_vector<T>& y);
|
||||
|
||||
void add_delta_to_solution(const vector<T>& yc, vector<T>& y);
|
||||
|
||||
|
||||
void find_error_of_yB(vector<T>& yc, const vector<T>& y,
|
||||
const vector<unsigned>& basis);
|
||||
|
||||
void find_error_of_yB_indexed(const indexed_vector<T>& y,
|
||||
const vector<int>& heading, const lp_settings& settings);
|
||||
|
||||
|
||||
void solve_yB_with_error_check(vector<T> & y, const vector<unsigned>& basis);
|
||||
|
||||
void solve_yB_with_error_check_indexed(indexed_vector<T> & y, const vector<int>& heading, const vector<unsigned> & basis, const lp_settings &);
|
||||
|
||||
void apply_Q_R_to_U(permutation_matrix<T, X> & r_wave);
|
||||
|
||||
|
||||
LU_status get_status() { return m_status; }
|
||||
|
||||
void set_status(LU_status status) {
|
||||
m_status = status;
|
||||
}
|
||||
|
||||
~lu();
|
||||
|
||||
void init_vector_y(vector<X> & y);
|
||||
|
||||
void perform_transformations_on_w(indexed_vector<T>& w);
|
||||
|
||||
void init_vector_w(unsigned entering, indexed_vector<T> & w);
|
||||
void apply_lp_list_to_w(indexed_vector<T> & w);
|
||||
void apply_lp_list_to_y(vector<X>& y);
|
||||
|
||||
void swap_rows(int j, int k);
|
||||
|
||||
void swap_columns(int j, int pivot_column);
|
||||
|
||||
void push_matrix_to_tail(tail_matrix<T, X>* tm) {
|
||||
m_tail.push_back(tm);
|
||||
}
|
||||
|
||||
bool pivot_the_row(int row);
|
||||
|
||||
eta_matrix<T, X> * get_eta_matrix_for_pivot(unsigned j);
|
||||
// we're processing the column j now
|
||||
eta_matrix<T, X> * get_eta_matrix_for_pivot(unsigned j, square_sparse_matrix<T, X>& copy_of_U);
|
||||
|
||||
// see page 407 of Chvatal
|
||||
unsigned transform_U_to_V_by_replacing_column(indexed_vector<T> & w, unsigned leaving_column_of_U);
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
void check_vector_w(unsigned entering);
|
||||
|
||||
void check_apply_matrix_to_vector(matrix<T, X> *lp, T *w);
|
||||
|
||||
void check_apply_lp_lists_to_w(T * w);
|
||||
|
||||
// provide some access operators for testing
|
||||
permutation_matrix<T, X> & Q() { return m_Q; }
|
||||
permutation_matrix<T, X> & R() { return m_R; }
|
||||
matrix<T, X> & U() { return m_U; }
|
||||
unsigned tail_size() { return m_tail.size(); }
|
||||
|
||||
tail_matrix<T, X> * get_lp_matrix(unsigned i) {
|
||||
return m_tail[i];
|
||||
}
|
||||
|
||||
T B_(unsigned i, unsigned j, const vector<unsigned>& basis) {
|
||||
return m_A[i][basis[j]];
|
||||
}
|
||||
|
||||
unsigned dimension() { return m_dim; }
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
unsigned get_number_of_nonzeroes() {
|
||||
return m_U.get_number_of_nonzeroes();
|
||||
}
|
||||
|
||||
|
||||
void process_column(int j);
|
||||
|
||||
bool is_correct(const vector<unsigned>& basis);
|
||||
bool is_correct();
|
||||
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
dense_matrix<T, X> tail_product();
|
||||
dense_matrix<T, X> get_left_side(const vector<unsigned>& basis);
|
||||
dense_matrix<T, X> get_left_side();
|
||||
|
||||
dense_matrix<T, X> get_right_side();
|
||||
#endif
|
||||
|
||||
// needed for debugging purposes
|
||||
void copy_w(T *buffer, indexed_vector<T> & w);
|
||||
|
||||
// needed for debugging purposes
|
||||
void restore_w(T *buffer, indexed_vector<T> & w);
|
||||
bool all_columns_and_rows_are_active();
|
||||
|
||||
bool too_dense(unsigned j) const;
|
||||
|
||||
void pivot_in_dense_mode(unsigned i);
|
||||
|
||||
void create_initial_factorization();
|
||||
|
||||
void calculate_r_wave_and_update_U(unsigned bump_start, unsigned bump_end, permutation_matrix<T, X> & r_wave);
|
||||
|
||||
void scan_last_row_to_work_vector(unsigned lowest_row_of_the_bump);
|
||||
|
||||
bool diagonal_element_is_off(T /* diag_element */) { return false; }
|
||||
|
||||
void pivot_and_solve_the_system(unsigned replaced_column, unsigned lowest_row_of_the_bump);
|
||||
// see Achim Koberstein's thesis page 58, but here we solve the system and pivot to the last
|
||||
// row at the same time
|
||||
row_eta_matrix<T, X> *get_row_eta_matrix_and_set_row_vector(unsigned replaced_column, unsigned lowest_row_of_the_bump, const T & pivot_elem_for_checking);
|
||||
|
||||
void replace_column(T pivot_elem, indexed_vector<T> & w, unsigned leaving_column_of_U);
|
||||
|
||||
void calculate_Lwave_Pwave_for_bump(unsigned replaced_column, unsigned lowest_row_of_the_bump);
|
||||
|
||||
void calculate_Lwave_Pwave_for_last_row(unsigned lowest_row_of_the_bump, T diagonal_element);
|
||||
|
||||
void prepare_entering(unsigned entering, indexed_vector<T> & w) {
|
||||
init_vector_w(entering, w);
|
||||
}
|
||||
bool need_to_refactor() { return m_refactor_counter >= 200; }
|
||||
|
||||
void adjust_dimension_with_matrix_A() {
|
||||
lp_assert(m_A.row_count() >= m_dim);
|
||||
m_dim = m_A.row_count();
|
||||
m_U.resize(m_dim);
|
||||
m_Q.resize(m_dim);
|
||||
m_R.resize(m_dim);
|
||||
m_row_eta_work_vector.resize(m_dim);
|
||||
}
|
||||
|
||||
|
||||
std::unordered_set<unsigned> get_set_of_columns_to_replace_for_add_last_rows(const vector<int> & heading) const {
|
||||
std::unordered_set<unsigned> columns_to_replace;
|
||||
unsigned m = m_A.row_count();
|
||||
unsigned m_prev = m_U.dimension();
|
||||
|
||||
lp_assert(m_A.column_count() == heading.size());
|
||||
|
||||
for (unsigned i = m_prev; i < m; i++) {
|
||||
for (const row_cell<T> & c : m_A.m_rows[i]) {
|
||||
int h = heading[c.var()];
|
||||
if (h < 0) {
|
||||
continue;
|
||||
}
|
||||
columns_to_replace.insert(c.var());
|
||||
}
|
||||
}
|
||||
return columns_to_replace;
|
||||
}
|
||||
|
||||
void add_last_rows_to_B(const vector<int> & heading, const std::unordered_set<unsigned> & columns_to_replace) {
|
||||
unsigned m = m_A.row_count();
|
||||
lp_assert(m_A.column_count() == heading.size());
|
||||
adjust_dimension_with_matrix_A();
|
||||
m_w_for_extension.resize(m);
|
||||
// At this moment the LU is correct
|
||||
// for B extended by only by ones at the diagonal in the lower right corner
|
||||
|
||||
for (unsigned j :columns_to_replace) {
|
||||
lp_assert(heading[j] >= 0);
|
||||
replace_column_with_only_change_at_last_rows(j, heading[j]);
|
||||
if (get_status() == LU_status::Degenerated)
|
||||
break;
|
||||
}
|
||||
}
|
||||
// column j is a basis column, and there is a change in the last rows
|
||||
void replace_column_with_only_change_at_last_rows(unsigned j, unsigned column_to_change_in_U) {
|
||||
init_vector_w(j, m_w_for_extension);
|
||||
replace_column(zero_of_type<T>(), m_w_for_extension, column_to_change_in_U);
|
||||
}
|
||||
|
||||
bool has_dense_submatrix() const {
|
||||
for (auto m : m_tail)
|
||||
if (m->is_dense())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}; // end of lu
|
||||
|
||||
template <typename M>
|
||||
void init_factorization(lu<M>* & factorization, M & m_A, vector<unsigned> & m_basis, lp_settings &m_settings);
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T, typename X, typename M>
|
||||
dense_matrix<T, X> get_B(lu<M>& f, const vector<unsigned>& basis);
|
||||
|
||||
template <typename T, typename X, typename M>
|
||||
dense_matrix<T, X> get_B(lu<M>& f);
|
||||
#endif
|
||||
}
|
||||
|
|
@ -1,992 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include "util/vector.h"
|
||||
#include <utility>
|
||||
#include "util/debug.h"
|
||||
#include "math/lp/lu.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X, typename M> // print the nr x nc submatrix at the top left corner
|
||||
void print_submatrix(square_sparse_matrix<T, X> & m, unsigned mr, unsigned nc, std::ostream & out) {
|
||||
vector<vector<std::string>> A;
|
||||
vector<unsigned> widths;
|
||||
for (unsigned i = 0; i < m.row_count() && i < mr ; i++) {
|
||||
A.push_back(vector<std::string>());
|
||||
for (unsigned j = 0; j < m.column_count() && j < nc; j++) {
|
||||
A[i].push_back(T_to_string(static_cast<T>(m(i, j))));
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned j = 0; j < m.column_count() && j < nc; j++) {
|
||||
widths.push_back(get_width_of_column(j, A));
|
||||
}
|
||||
|
||||
print_matrix_with_widths(A, widths, out);
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
void print_matrix(M &m, std::ostream & out) {
|
||||
vector<vector<std::string>> A;
|
||||
vector<unsigned> widths;
|
||||
for (unsigned i = 0; i < m.row_count(); i++) {
|
||||
A.push_back(vector<std::string>());
|
||||
for (unsigned j = 0; j < m.column_count(); j++) {
|
||||
A[i].push_back(T_to_string(m[i][j]));
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned j = 0; j < m.column_count(); j++) {
|
||||
widths.push_back(get_width_of_column(j, A));
|
||||
}
|
||||
|
||||
print_matrix_with_widths(A, widths, out);
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
one_elem_on_diag<T, X>::one_elem_on_diag(const one_elem_on_diag & o) {
|
||||
m_i = o.m_i;
|
||||
m_val = o.m_val;
|
||||
#ifdef Z3DEBUG
|
||||
m_m = m_n = o.m_m;
|
||||
m_one_over_val = numeric_traits<T>::one() / o.m_val;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T, typename X>
|
||||
T one_elem_on_diag<T, X>::get_elem(unsigned i, unsigned j) const {
|
||||
if (i == j){
|
||||
if (j == m_i) {
|
||||
return m_one_over_val;
|
||||
}
|
||||
return numeric_traits<T>::one();
|
||||
}
|
||||
|
||||
return numeric_traits<T>::zero();
|
||||
}
|
||||
#endif
|
||||
template <typename T, typename X>
|
||||
void one_elem_on_diag<T, X>::apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) {
|
||||
T & t = w[m_i];
|
||||
if (numeric_traits<T>::is_zero(t)) {
|
||||
return;
|
||||
}
|
||||
t /= m_val;
|
||||
if (numeric_traits<T>::precise()) return;
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(t)) {
|
||||
w.erase_from_index(m_i);
|
||||
t = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
|
||||
// This class supports updates of the columns of B, and solves systems Bx=b,and yB=c
|
||||
// Using Suhl-Suhl method described in the dissertation of Achim Koberstein, Chapter 5
|
||||
template <typename M>
|
||||
lu<M>::lu(const M& A,
|
||||
vector<unsigned>& basis,
|
||||
lp_settings & settings):
|
||||
m_status(LU_status::OK),
|
||||
m_dim(A.row_count()),
|
||||
m_A(A),
|
||||
m_Q(m_dim),
|
||||
m_R(m_dim),
|
||||
m_r_wave(m_dim),
|
||||
m_U(A, basis), // create the square matrix that eventually will be factorized
|
||||
m_settings(settings),
|
||||
m_failure(false),
|
||||
m_row_eta_work_vector(A.row_count()),
|
||||
m_refactor_counter(0) {
|
||||
lp_assert(!(numeric_traits<T>::precise() && settings.use_tableau()));
|
||||
#ifdef Z3DEBUG
|
||||
debug_test_of_basis(A, basis);
|
||||
#endif
|
||||
++m_settings.stats().m_num_factorizations;
|
||||
create_initial_factorization();
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(check_correctness());
|
||||
#endif
|
||||
}
|
||||
template <typename M>
|
||||
lu<M>::lu(const M& A,
|
||||
lp_settings & settings):
|
||||
m_status(LU_status::OK),
|
||||
m_dim(A.row_count()),
|
||||
m_A(A),
|
||||
m_Q(m_dim),
|
||||
m_R(m_dim),
|
||||
m_r_wave(m_dim),
|
||||
m_U(A), // create the square matrix that eventually will be factorized
|
||||
m_settings(settings),
|
||||
m_failure(false),
|
||||
m_row_eta_work_vector(A.row_count()),
|
||||
m_refactor_counter(0) {
|
||||
lp_assert(A.row_count() == A.column_count());
|
||||
create_initial_factorization();
|
||||
#ifdef Z3DEBUG
|
||||
lp_assert(is_correct());
|
||||
#endif
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::debug_test_of_basis( M const & A, vector<unsigned> & basis) {
|
||||
std::set<unsigned> set;
|
||||
for (unsigned i = 0; i < A.row_count(); i++) {
|
||||
lp_assert(basis[i]< A.column_count());
|
||||
set.insert(basis[i]);
|
||||
}
|
||||
lp_assert(set.size() == A.row_count());
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::solve_By(indexed_vector<X> & y) {
|
||||
lp_assert(false); // not implemented
|
||||
// init_vector_y(y);
|
||||
// solve_By_when_y_is_ready(y);
|
||||
}
|
||||
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::solve_By(vector<X> & y) {
|
||||
init_vector_y(y);
|
||||
solve_By_when_y_is_ready_for_X(y);
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::solve_By_when_y_is_ready_for_X(vector<X> & y) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
m_U.solve_U_y(y);
|
||||
m_R.apply_reverse_from_left_to_X(y); // see 24.3 from Chvatal
|
||||
return;
|
||||
}
|
||||
m_U.double_solve_U_y(y);
|
||||
m_R.apply_reverse_from_left_to_X(y); // see 24.3 from Chvatal
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
if (is_zero(y[i])) continue;
|
||||
if (m_settings.abs_val_is_smaller_than_drop_tolerance(y[i])){
|
||||
y[i] = zero_of_type<X>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::solve_By_when_y_is_ready_for_T(vector<T> & y, vector<unsigned> & index) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
m_U.solve_U_y(y);
|
||||
m_R.apply_reverse_from_left_to_T(y); // see 24.3 from Chvatal
|
||||
unsigned j = m_dim;
|
||||
while (j--) {
|
||||
if (!is_zero(y[j]))
|
||||
index.push_back(j);
|
||||
}
|
||||
return;
|
||||
}
|
||||
m_U.double_solve_U_y(y);
|
||||
m_R.apply_reverse_from_left_to_T(y); // see 24.3 from Chvatal
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
if (is_zero(y[i])) continue;
|
||||
if (m_settings.abs_val_is_smaller_than_drop_tolerance(y[i])){
|
||||
y[i] = zero_of_type<T>();
|
||||
} else {
|
||||
index.push_back(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::solve_By_for_T_indexed_only(indexed_vector<T> & y, const lp_settings & settings) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
vector<unsigned> active_rows;
|
||||
m_U.solve_U_y_indexed_only(y, settings, active_rows);
|
||||
m_R.apply_reverse_from_left(y); // see 24.3 from Chvatal
|
||||
return;
|
||||
}
|
||||
m_U.double_solve_U_y(y, m_settings);
|
||||
m_R.apply_reverse_from_left(y); // see 24.3 from Chvatal
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::print_matrix_compact(std::ostream & f) {
|
||||
f << "matrix_start" << std::endl;
|
||||
f << "nrows " << m_A.row_count() << std::endl;
|
||||
f << "ncolumns " << m_A.column_count() << std::endl;
|
||||
for (unsigned i = 0; i < m_A.row_count(); i++) {
|
||||
auto & row = m_A.m_rows[i];
|
||||
f << "row " << i << std::endl;
|
||||
for (auto & t : row) {
|
||||
f << "column " << t.m_j << " value " << t.m_value << std::endl;
|
||||
}
|
||||
f << "row_end" << std::endl;
|
||||
}
|
||||
f << "matrix_end" << std::endl;
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::print(indexed_vector<T> & w, const vector<unsigned>& basis) {
|
||||
std::string dump_file_name("/tmp/lu");
|
||||
remove(dump_file_name.c_str());
|
||||
std::ofstream f(dump_file_name);
|
||||
if (!f.is_open()) {
|
||||
LP_OUT(m_settings, "cannot open file " << dump_file_name << std::endl);
|
||||
return;
|
||||
}
|
||||
LP_OUT(m_settings, "writing lu dump to " << dump_file_name << std::endl);
|
||||
print_matrix_compact(f);
|
||||
print_vector(basis, f);
|
||||
print_indexed_vector(w, f);
|
||||
f.close();
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::solve_Bd(unsigned a_column, indexed_vector<T> & d, indexed_vector<T> & w) {
|
||||
init_vector_w(a_column, w);
|
||||
|
||||
if (w.m_index.size() * ratio_of_index_size_to_all_size<T>() < d.m_data.size()) { // this const might need some tuning
|
||||
d = w;
|
||||
solve_By_for_T_indexed_only(d, m_settings);
|
||||
} else {
|
||||
d.m_data = w.m_data;
|
||||
d.m_index.clear();
|
||||
solve_By_when_y_is_ready_for_T(d.m_data, d.m_index);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::solve_Bd_faster(unsigned a_column, indexed_vector<T> & d) { // puts the a_column into d
|
||||
init_vector_w(a_column, d);
|
||||
solve_By_for_T_indexed_only(d, m_settings);
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::solve_yB(vector<T>& y) {
|
||||
// first solve yU = cb*R(-1)
|
||||
m_R.apply_reverse_from_right_to_T(y); // got y = cb*R(-1)
|
||||
m_U.solve_y_U(y); // got y*U=cb*R(-1)
|
||||
m_Q.apply_reverse_from_right_to_T(y); //
|
||||
for (auto e = m_tail.rbegin(); e != m_tail.rend(); ++e) {
|
||||
#ifdef Z3DEBUG
|
||||
(*e)->set_number_of_columns(m_dim);
|
||||
#endif
|
||||
(*e)->apply_from_right(y);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::solve_yB_indexed(indexed_vector<T>& y) {
|
||||
lp_assert(y.is_OK());
|
||||
// first solve yU = cb*R(-1)
|
||||
m_R.apply_reverse_from_right_to_T(y); // got y = cb*R(-1)
|
||||
lp_assert(y.is_OK());
|
||||
m_U.solve_y_U_indexed(y, m_settings); // got y*U=cb*R(-1)
|
||||
lp_assert(y.is_OK());
|
||||
m_Q.apply_reverse_from_right_to_T(y);
|
||||
lp_assert(y.is_OK());
|
||||
for (auto e = m_tail.rbegin(); e != m_tail.rend(); ++e) {
|
||||
#ifdef Z3DEBUG
|
||||
(*e)->set_number_of_columns(m_dim);
|
||||
#endif
|
||||
(*e)->apply_from_right(y);
|
||||
lp_assert(y.is_OK());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::add_delta_to_solution(const vector<T>& yc, vector<T>& y){
|
||||
unsigned i = static_cast<unsigned>(y.size());
|
||||
while (i--)
|
||||
y[i]+=yc[i];
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::add_delta_to_solution_indexed(indexed_vector<T>& y) {
|
||||
// the delta sits in m_y_copy, put result into y
|
||||
lp_assert(y.is_OK());
|
||||
lp_assert(m_y_copy.is_OK());
|
||||
m_ii.clear();
|
||||
m_ii.resize(y.data_size());
|
||||
for (unsigned i : y.m_index)
|
||||
m_ii.set_value(1, i);
|
||||
for (unsigned i : m_y_copy.m_index) {
|
||||
y.m_data[i] += m_y_copy[i];
|
||||
if (m_ii[i] == 0)
|
||||
m_ii.set_value(1, i);
|
||||
}
|
||||
lp_assert(m_ii.is_OK());
|
||||
y.m_index.clear();
|
||||
|
||||
for (unsigned i : m_ii.m_index) {
|
||||
T & v = y.m_data[i];
|
||||
if (!lp_settings::is_eps_small_general(v, 1e-14))
|
||||
y.m_index.push_back(i);
|
||||
else if (!numeric_traits<T>::is_zero(v))
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
|
||||
lp_assert(y.is_OK());
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::find_error_of_yB(vector<T>& yc, const vector<T>& y, const vector<unsigned>& m_basis) {
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
yc[i] -= m_A.dot_product_with_column(y, m_basis[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::find_error_of_yB_indexed(const indexed_vector<T>& y, const vector<int>& heading, const lp_settings& settings) {
|
||||
#if 0 == 1
|
||||
// it is a non efficient version
|
||||
indexed_vector<T> yc = m_y_copy;
|
||||
yc.m_index.clear();
|
||||
lp_assert(!numeric_traits<T>::precise());
|
||||
{
|
||||
|
||||
vector<unsigned> d_basis(y.m_data.size());
|
||||
for (unsigned j = 0; j < heading.size(); j++) {
|
||||
if (heading[j] >= 0) {
|
||||
d_basis[heading[j]] = j;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
T & v = yc.m_data[i] -= m_A.dot_product_with_column(y.m_data, d_basis[i]);
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(v))
|
||||
v = zero_of_type<T>();
|
||||
else
|
||||
yc.m_index.push_back(i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
lp_assert(m_ii.is_OK());
|
||||
m_ii.clear();
|
||||
m_ii.resize(y.data_size());
|
||||
lp_assert(m_y_copy.is_OK());
|
||||
// put the error into m_y_copy
|
||||
for (auto k : y.m_index) {
|
||||
auto & row = m_A.m_rows[k];
|
||||
const T & y_k = y.m_data[k];
|
||||
for (auto & c : row) {
|
||||
unsigned j = c.var();
|
||||
int hj = heading[j];
|
||||
if (hj < 0) continue;
|
||||
if (m_ii.m_data[hj] == 0)
|
||||
m_ii.set_value(1, hj);
|
||||
m_y_copy.m_data[hj] -= c.coeff() * y_k;
|
||||
}
|
||||
}
|
||||
// add the index of m_y_copy to m_ii
|
||||
for (unsigned i : m_y_copy.m_index) {
|
||||
if (m_ii.m_data[i] == 0)
|
||||
m_ii.set_value(1, i);
|
||||
}
|
||||
|
||||
// there is no guarantee that m_y_copy is OK here, but its index
|
||||
// is contained in m_ii index
|
||||
m_y_copy.m_index.clear();
|
||||
// setup the index of m_y_copy
|
||||
for (auto k : m_ii.m_index) {
|
||||
T& v = m_y_copy.m_data[k];
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(v))
|
||||
v = zero_of_type<T>();
|
||||
else {
|
||||
m_y_copy.set_value(v, k);
|
||||
}
|
||||
}
|
||||
lp_assert(m_y_copy.is_OK());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// solves y*B = y
|
||||
// y is the input
|
||||
template <typename M>
|
||||
void lu< M>::solve_yB_with_error_check_indexed(indexed_vector<T> & y, const vector<int>& heading, const vector<unsigned> & basis, const lp_settings & settings) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
if (y.m_index.size() * ratio_of_index_size_to_all_size<T>() * 3 < m_A.column_count()) {
|
||||
solve_yB_indexed(y);
|
||||
} else {
|
||||
solve_yB(y.m_data);
|
||||
y.restore_index_and_clean_from_data();
|
||||
}
|
||||
return;
|
||||
}
|
||||
lp_assert(m_y_copy.is_OK());
|
||||
lp_assert(y.is_OK());
|
||||
if (y.m_index.size() * ratio_of_index_size_to_all_size<T>() < m_A.column_count()) {
|
||||
m_y_copy = y;
|
||||
solve_yB_indexed(y);
|
||||
lp_assert(y.is_OK());
|
||||
if (y.m_index.size() * ratio_of_index_size_to_all_size<T>() >= m_A.column_count()) {
|
||||
find_error_of_yB(m_y_copy.m_data, y.m_data, basis);
|
||||
solve_yB(m_y_copy.m_data);
|
||||
add_delta_to_solution(m_y_copy.m_data, y.m_data);
|
||||
y.restore_index_and_clean_from_data();
|
||||
m_y_copy.clear_all();
|
||||
} else {
|
||||
find_error_of_yB_indexed(y, heading, settings); // this works with m_y_copy
|
||||
solve_yB_indexed(m_y_copy);
|
||||
add_delta_to_solution_indexed(y);
|
||||
}
|
||||
lp_assert(m_y_copy.is_OK());
|
||||
} else {
|
||||
solve_yB_with_error_check(y.m_data, basis);
|
||||
y.restore_index_and_clean_from_data();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// solves y*B = y
|
||||
// y is the input
|
||||
template <typename M>
|
||||
void lu< M>::solve_yB_with_error_check(vector<T> & y, const vector<unsigned>& basis) {
|
||||
if (numeric_traits<T>::precise()) {
|
||||
solve_yB(y);
|
||||
return;
|
||||
}
|
||||
auto & yc = m_y_copy.m_data;
|
||||
yc =y; // copy y aside
|
||||
solve_yB(y);
|
||||
find_error_of_yB(yc, y, basis);
|
||||
solve_yB(yc);
|
||||
add_delta_to_solution(yc, y);
|
||||
m_y_copy.clear_all();
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::apply_Q_R_to_U(permutation_matrix<T, X> & r_wave) {
|
||||
m_U.multiply_from_right(r_wave);
|
||||
m_U.multiply_from_left_with_reverse(r_wave);
|
||||
}
|
||||
|
||||
|
||||
// Solving yB = cb to find the entering variable,
|
||||
// where cb is the cost vector projected to B.
|
||||
// The result is stored in cb.
|
||||
|
||||
// solving Bd = a ( to find the column d of B^{-1} A_N corresponding to the entering
|
||||
// variable
|
||||
template <typename M>
|
||||
lu< M>::~lu(){
|
||||
for (auto t : m_tail) {
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::init_vector_y(vector<X> & y) {
|
||||
apply_lp_list_to_y(y);
|
||||
m_Q.apply_reverse_from_left_to_X(y);
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::perform_transformations_on_w(indexed_vector<T>& w) {
|
||||
apply_lp_list_to_w(w);
|
||||
m_Q.apply_reverse_from_left(w);
|
||||
// TBD does not compile: lp_assert(numeric_traits<T>::precise() || check_vector_for_small_values(w, m_settings));
|
||||
}
|
||||
|
||||
// see Chvatal 24.3
|
||||
template <typename M>
|
||||
void lu< M>::init_vector_w(unsigned entering, indexed_vector<T> & w) {
|
||||
w.clear();
|
||||
m_A.copy_column_to_indexed_vector(entering, w); // w = a, the column
|
||||
perform_transformations_on_w(w);
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::apply_lp_list_to_w(indexed_vector<T> & w) {
|
||||
for (unsigned i = 0; i < m_tail.size(); i++) {
|
||||
m_tail[i]->apply_from_left_to_T(w, m_settings);
|
||||
// TBD does not compile: lp_assert(check_vector_for_small_values(w, m_settings));
|
||||
}
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::apply_lp_list_to_y(vector<X>& y) {
|
||||
for (unsigned i = 0; i < m_tail.size(); i++) {
|
||||
m_tail[i]->apply_from_left(y, m_settings);
|
||||
}
|
||||
}
|
||||
template <typename M>
|
||||
void lu< M>::swap_rows(int j, int k) {
|
||||
if (j != k) {
|
||||
m_Q.transpose_from_left(j, k);
|
||||
m_U.swap_rows(j, k);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu< M>::swap_columns(int j, int pivot_column) {
|
||||
if (j == pivot_column)
|
||||
return;
|
||||
m_R.transpose_from_right(j, pivot_column);
|
||||
m_U.swap_columns(j, pivot_column);
|
||||
}
|
||||
template <typename M>
|
||||
bool lu< M>::pivot_the_row(int row) {
|
||||
eta_matrix<T, X> * eta_matrix = get_eta_matrix_for_pivot(row);
|
||||
if (get_status() != LU_status::OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (eta_matrix == nullptr) {
|
||||
m_U.shorten_active_matrix(row, nullptr);
|
||||
return true;
|
||||
}
|
||||
if (!m_U.pivot_with_eta(row, eta_matrix, m_settings))
|
||||
return false;
|
||||
eta_matrix->conjugate_by_permutation(m_Q);
|
||||
push_matrix_to_tail(eta_matrix);
|
||||
return true;
|
||||
}
|
||||
// we're processing the column j now
|
||||
template <typename M>
|
||||
eta_matrix<typename M::coefftype, typename M::argtype> * lu< M>::get_eta_matrix_for_pivot(unsigned j) {
|
||||
eta_matrix<T, X> *ret;
|
||||
if(!m_U.fill_eta_matrix(j, &ret)) {
|
||||
set_status(LU_status::Degenerated);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// we're processing the column j now
|
||||
template <typename M>
|
||||
eta_matrix<typename M::coefftype, typename M::argtype> * lu<M>::get_eta_matrix_for_pivot(unsigned j, square_sparse_matrix<T, X>& copy_of_U) {
|
||||
eta_matrix<T, X> *ret;
|
||||
copy_of_U.fill_eta_matrix(j, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// see page 407 of Chvatal
|
||||
template <typename M>
|
||||
unsigned lu<M>::transform_U_to_V_by_replacing_column(indexed_vector<T> & w,
|
||||
unsigned leaving_column) {
|
||||
unsigned column_to_replace = m_R.apply_reverse(leaving_column);
|
||||
m_U.replace_column(column_to_replace, w, m_settings);
|
||||
return column_to_replace;
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename M>
|
||||
void lu<M>::check_vector_w(unsigned entering) {
|
||||
T * w = new T[m_dim];
|
||||
m_A.copy_column_to_vector(entering, w);
|
||||
check_apply_lp_lists_to_w(w);
|
||||
delete [] w;
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::check_apply_matrix_to_vector(matrix<T, X> *lp, T *w) {
|
||||
if (lp != nullptr) {
|
||||
lp -> set_number_of_rows(m_dim);
|
||||
lp -> set_number_of_columns(m_dim);
|
||||
apply_to_vector(*lp, w);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::check_apply_lp_lists_to_w(T * w) {
|
||||
for (unsigned i = 0; i < m_tail.size(); i++) {
|
||||
check_apply_matrix_to_vector(m_tail[i], w);
|
||||
}
|
||||
permutation_matrix<T, X> qr = m_Q.get_reverse();
|
||||
apply_to_vector(qr, w);
|
||||
for (int i = m_dim - 1; i >= 0; i--) {
|
||||
lp_assert(abs(w[i] - w[i]) < 0.0000001);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
template <typename M>
|
||||
void lu<M>::process_column(int j) {
|
||||
unsigned pi, pj;
|
||||
bool success = m_U.get_pivot_for_column(pi, pj, m_settings.c_partial_pivoting, j);
|
||||
if (!success) {
|
||||
// LP_OUT(m_settings, "get_pivot returned false: cannot find the pivot for column " << j << std::endl);
|
||||
m_failure = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (static_cast<int>(pi) == -1) {
|
||||
// LP_OUT(m_settings, "cannot find the pivot for column " << j << std::endl);
|
||||
m_failure = true;
|
||||
return;
|
||||
}
|
||||
swap_columns(j, pj);
|
||||
swap_rows(j, pi);
|
||||
if (!pivot_the_row(j)) {
|
||||
// LP_OUT(m_settings, "pivot_the_row(" << j << ") failed" << std::endl);
|
||||
m_failure = true;
|
||||
}
|
||||
}
|
||||
template <typename M>
|
||||
bool lu<M>::is_correct(const vector<unsigned>& basis) {
|
||||
#ifdef Z3DEBUG
|
||||
if (get_status() != LU_status::OK) {
|
||||
return false;
|
||||
}
|
||||
dense_matrix<T, X> left_side = get_left_side(basis);
|
||||
dense_matrix<T, X> right_side = get_right_side();
|
||||
return left_side == right_side;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
bool lu<M>::is_correct() {
|
||||
#ifdef Z3DEBUG
|
||||
if (get_status() != LU_status::OK) {
|
||||
return false;
|
||||
}
|
||||
dense_matrix<T, X> left_side = get_left_side();
|
||||
dense_matrix<T, X> right_side = get_right_side();
|
||||
return left_side == right_side;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> lu<M>::tail_product() {
|
||||
lp_assert(tail_size() > 0);
|
||||
dense_matrix<T, X> left_side = permutation_matrix<T, X>(m_dim);
|
||||
for (unsigned i = 0; i < tail_size(); i++) {
|
||||
matrix<T, X>* lp = get_lp_matrix(i);
|
||||
lp->set_number_of_rows(m_dim);
|
||||
lp->set_number_of_columns(m_dim);
|
||||
left_side = ((*lp) * left_side);
|
||||
}
|
||||
return left_side;
|
||||
}
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> lu<M>::get_left_side(const vector<unsigned>& basis) {
|
||||
dense_matrix<T, X> left_side = get_B(*this, basis);
|
||||
for (unsigned i = 0; i < tail_size(); i++) {
|
||||
matrix<T, X>* lp = get_lp_matrix(i);
|
||||
lp->set_number_of_rows(m_dim);
|
||||
lp->set_number_of_columns(m_dim);
|
||||
left_side = ((*lp) * left_side);
|
||||
}
|
||||
return left_side;
|
||||
}
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> lu<M>::get_left_side() {
|
||||
dense_matrix<T, X> left_side = get_B(*this);
|
||||
for (unsigned i = 0; i < tail_size(); i++) {
|
||||
matrix<T, X>* lp = get_lp_matrix(i);
|
||||
lp->set_number_of_rows(m_dim);
|
||||
lp->set_number_of_columns(m_dim);
|
||||
left_side = ((*lp) * left_side);
|
||||
}
|
||||
return left_side;
|
||||
}
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> lu<M>::get_right_side() {
|
||||
auto ret = U() * R();
|
||||
ret = Q() * ret;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
// needed for debugging purposes
|
||||
template <typename M>
|
||||
void lu<M>::copy_w(T *buffer, indexed_vector<T> & w) {
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
buffer[i] = w[i];
|
||||
}
|
||||
}
|
||||
|
||||
// needed for debugging purposes
|
||||
template <typename M>
|
||||
void lu<M>::restore_w(T *buffer, indexed_vector<T> & w) {
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
w[i] = buffer[i];
|
||||
}
|
||||
}
|
||||
template <typename M>
|
||||
bool lu<M>::all_columns_and_rows_are_active() {
|
||||
unsigned i = m_dim;
|
||||
while (i--) {
|
||||
lp_assert(m_U.col_is_active(i));
|
||||
lp_assert(m_U.row_is_active(i));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename M>
|
||||
bool lu<M>::too_dense(unsigned j) const {
|
||||
unsigned r = m_dim - j;
|
||||
if (r < 5)
|
||||
return false;
|
||||
// if (j * 5 < m_dim * 4) // start looking for dense only at the bottom of the rows
|
||||
// return false;
|
||||
// return r * r * m_settings.density_threshold <= m_U.get_number_of_nonzeroes_below_row(j);
|
||||
return r * r * m_settings.density_threshold <= m_U.get_n_of_active_elems();
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::pivot_in_dense_mode(unsigned i) {
|
||||
int j = m_dense_LU->find_pivot_column_in_row(i);
|
||||
if (j == -1) {
|
||||
m_failure = true;
|
||||
return;
|
||||
}
|
||||
if (i != static_cast<unsigned>(j)) {
|
||||
swap_columns(i, j);
|
||||
m_dense_LU->swap_columns(i, j);
|
||||
}
|
||||
m_dense_LU->pivot(i, m_settings);
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::create_initial_factorization(){
|
||||
m_U.prepare_for_factorization();
|
||||
unsigned j;
|
||||
for (j = 0; j < m_dim; j++) {
|
||||
process_column(j);
|
||||
if (m_failure) {
|
||||
set_status(LU_status::Degenerated);
|
||||
return;
|
||||
}
|
||||
if (too_dense(j)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == m_dim) {
|
||||
// TBD does not compile: lp_assert(m_U.is_upper_triangular_and_maximums_are_set_correctly_in_rows(m_settings));
|
||||
// lp_assert(is_correct());
|
||||
// lp_assert(m_U.is_upper_triangular_and_maximums_are_set_correctly_in_rows(m_settings));
|
||||
return;
|
||||
}
|
||||
j++;
|
||||
m_dense_LU = new square_dense_submatrix<T, X>(&m_U, j);
|
||||
for (; j < m_dim; j++) {
|
||||
pivot_in_dense_mode(j);
|
||||
if (m_failure) {
|
||||
set_status(LU_status::Degenerated);
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_dense_LU->update_parent_matrix(m_settings);
|
||||
lp_assert(m_dense_LU->is_L_matrix());
|
||||
m_dense_LU->conjugate_by_permutation(m_Q);
|
||||
push_matrix_to_tail(m_dense_LU);
|
||||
m_refactor_counter = 0;
|
||||
// lp_assert(is_correct());
|
||||
// lp_assert(m_U.is_upper_triangular_and_maximums_are_set_correctly_in_rows(m_settings));
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::calculate_r_wave_and_update_U(unsigned bump_start, unsigned bump_end, permutation_matrix<T, X> & r_wave) {
|
||||
if (bump_start > bump_end) {
|
||||
set_status(LU_status::Degenerated);
|
||||
return;
|
||||
}
|
||||
if (bump_start == bump_end) {
|
||||
return;
|
||||
}
|
||||
|
||||
r_wave[bump_start] = bump_end; // sending the offensive column to the end of the bump
|
||||
|
||||
for ( unsigned i = bump_start + 1 ; i <= bump_end; i++ ) {
|
||||
r_wave[i] = i - 1;
|
||||
}
|
||||
|
||||
m_U.multiply_from_right(r_wave);
|
||||
m_U.multiply_from_left_with_reverse(r_wave);
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::scan_last_row_to_work_vector(unsigned lowest_row_of_the_bump) {
|
||||
vector<indexed_value<T>> & last_row_vec = m_U.get_row_values(m_U.adjust_row(lowest_row_of_the_bump));
|
||||
for (auto & iv : last_row_vec) {
|
||||
if (is_zero(iv.m_value)) continue;
|
||||
lp_assert(!m_settings.abs_val_is_smaller_than_drop_tolerance(iv.m_value));
|
||||
unsigned adjusted_col = m_U.adjust_column_inverse(iv.m_index);
|
||||
if (adjusted_col < lowest_row_of_the_bump) {
|
||||
m_row_eta_work_vector.set_value(-iv.m_value, adjusted_col);
|
||||
} else {
|
||||
m_row_eta_work_vector.set_value(iv.m_value, adjusted_col); // preparing to calculate the real value in the matrix
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::pivot_and_solve_the_system(unsigned replaced_column, unsigned lowest_row_of_the_bump) {
|
||||
// we have the system right side at m_row_eta_work_vector now
|
||||
// solve the system column wise
|
||||
for (unsigned j = replaced_column; j < lowest_row_of_the_bump; j++) {
|
||||
T v = m_row_eta_work_vector[j];
|
||||
if (numeric_traits<T>::is_zero(v)) continue; // this column does not contribute to the solution
|
||||
unsigned aj = m_U.adjust_row(j);
|
||||
vector<indexed_value<T>> & row = m_U.get_row_values(aj);
|
||||
for (auto & iv : row) {
|
||||
unsigned col = m_U.adjust_column_inverse(iv.m_index);
|
||||
lp_assert(col >= j || numeric_traits<T>::is_zero(iv.m_value));
|
||||
if (col == j) continue;
|
||||
if (numeric_traits<T>::is_zero(iv.m_value)) {
|
||||
continue;
|
||||
}
|
||||
// the -v is for solving the system ( to zero the last row), and +v is for pivoting
|
||||
T delta = col < lowest_row_of_the_bump? -v * iv.m_value: v * iv.m_value;
|
||||
lp_assert(numeric_traits<T>::is_zero(delta) == false);
|
||||
|
||||
|
||||
|
||||
// m_row_eta_work_vector.add_value_at_index_with_drop_tolerance(col, delta);
|
||||
if (numeric_traits<T>::is_zero(m_row_eta_work_vector[col])) {
|
||||
if (!m_settings.abs_val_is_smaller_than_drop_tolerance(delta)){
|
||||
m_row_eta_work_vector.set_value(delta, col);
|
||||
}
|
||||
} else {
|
||||
T t = (m_row_eta_work_vector[col] += delta);
|
||||
if (m_settings.abs_val_is_smaller_than_drop_tolerance(t)){
|
||||
m_row_eta_work_vector[col] = numeric_traits<T>::zero();
|
||||
auto it = std::find(m_row_eta_work_vector.m_index.begin(), m_row_eta_work_vector.m_index.end(), col);
|
||||
if (it != m_row_eta_work_vector.m_index.end())
|
||||
m_row_eta_work_vector.m_index.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// see Achim Koberstein's thesis page 58, but here we solve the system and pivot to the last
|
||||
// row at the same time
|
||||
template <typename M>
|
||||
row_eta_matrix<typename M::coefftype, typename M::argtype> *lu<M>::get_row_eta_matrix_and_set_row_vector(unsigned replaced_column, unsigned lowest_row_of_the_bump, const T & pivot_elem_for_checking) {
|
||||
if (replaced_column == lowest_row_of_the_bump) return nullptr;
|
||||
scan_last_row_to_work_vector(lowest_row_of_the_bump);
|
||||
pivot_and_solve_the_system(replaced_column, lowest_row_of_the_bump);
|
||||
if (numeric_traits<T>::precise() == false && !is_zero(pivot_elem_for_checking)) {
|
||||
T denom = std::max(T(1), abs(pivot_elem_for_checking));
|
||||
if (
|
||||
!m_settings.abs_val_is_smaller_than_pivot_tolerance((m_row_eta_work_vector[lowest_row_of_the_bump] - pivot_elem_for_checking) / denom)) {
|
||||
set_status(LU_status::Degenerated);
|
||||
// LP_OUT(m_settings, "diagonal element is off" << std::endl);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
auto ret = new row_eta_matrix<typename M::coefftype, typename M::argtype>(replaced_column, lowest_row_of_the_bump, m_dim);
|
||||
#else
|
||||
auto ret = new row_eta_matrix<typename M::coefftype, typename M::argtype>(replaced_column, lowest_row_of_the_bump);
|
||||
#endif
|
||||
|
||||
for (auto j : m_row_eta_work_vector.m_index) {
|
||||
if (j < lowest_row_of_the_bump) {
|
||||
auto & v = m_row_eta_work_vector[j];
|
||||
if (!is_zero(v)) {
|
||||
if (!m_settings.abs_val_is_smaller_than_drop_tolerance(v)){
|
||||
ret->push_back(j, v);
|
||||
}
|
||||
v = numeric_traits<T>::zero();
|
||||
}
|
||||
}
|
||||
} // now the lowest_row_of_the_bump contains the rest of the row to the right of the bump with correct values
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::replace_column(T pivot_elem_for_checking, indexed_vector<T> & w, unsigned leaving_column_of_U){
|
||||
m_refactor_counter++;
|
||||
unsigned replaced_column = transform_U_to_V_by_replacing_column( w, leaving_column_of_U);
|
||||
unsigned lowest_row_of_the_bump = m_U.lowest_row_in_column(replaced_column);
|
||||
m_r_wave.init(m_dim);
|
||||
calculate_r_wave_and_update_U(replaced_column, lowest_row_of_the_bump, m_r_wave);
|
||||
auto row_eta = get_row_eta_matrix_and_set_row_vector(replaced_column, lowest_row_of_the_bump, pivot_elem_for_checking);
|
||||
|
||||
if (get_status() == LU_status::Degenerated) {
|
||||
m_row_eta_work_vector.clear_all();
|
||||
return;
|
||||
}
|
||||
m_Q.multiply_by_permutation_from_right(m_r_wave);
|
||||
m_R.multiply_by_permutation_reverse_from_left(m_r_wave);
|
||||
if (row_eta != nullptr) {
|
||||
row_eta->conjugate_by_permutation(m_Q);
|
||||
push_matrix_to_tail(row_eta);
|
||||
}
|
||||
calculate_Lwave_Pwave_for_bump(replaced_column, lowest_row_of_the_bump);
|
||||
// lp_assert(m_U.is_upper_triangular_and_maximums_are_set_correctly_in_rows(m_settings));
|
||||
// lp_assert(w.is_OK() && m_row_eta_work_vector.is_OK());
|
||||
}
|
||||
template <typename M>
|
||||
void lu<M>::calculate_Lwave_Pwave_for_bump(unsigned replaced_column, unsigned lowest_row_of_the_bump){
|
||||
T diagonal_elem;
|
||||
if (replaced_column < lowest_row_of_the_bump) {
|
||||
diagonal_elem = m_row_eta_work_vector[lowest_row_of_the_bump];
|
||||
// lp_assert(m_row_eta_work_vector.is_OK());
|
||||
m_U.set_row_from_work_vector_and_clean_work_vector_not_adjusted(m_U.adjust_row(lowest_row_of_the_bump), m_row_eta_work_vector, m_settings);
|
||||
} else {
|
||||
diagonal_elem = m_U(lowest_row_of_the_bump, lowest_row_of_the_bump); // todo - get it more efficiently
|
||||
}
|
||||
if (m_settings.abs_val_is_smaller_than_pivot_tolerance(diagonal_elem)) {
|
||||
set_status(LU_status::Degenerated);
|
||||
return;
|
||||
}
|
||||
|
||||
calculate_Lwave_Pwave_for_last_row(lowest_row_of_the_bump, diagonal_elem);
|
||||
// lp_assert(m_U.is_upper_triangular_and_maximums_are_set_correctly_in_rows(m_settings));
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void lu<M>::calculate_Lwave_Pwave_for_last_row(unsigned lowest_row_of_the_bump, T diagonal_element) {
|
||||
auto l = new one_elem_on_diag<T, X>(lowest_row_of_the_bump, diagonal_element);
|
||||
#ifdef Z3DEBUG
|
||||
l->set_number_of_columns(m_dim);
|
||||
#endif
|
||||
push_matrix_to_tail(l);
|
||||
m_U.divide_row_by_constant(lowest_row_of_the_bump, diagonal_element, m_settings);
|
||||
l->conjugate_by_permutation(m_Q);
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void init_factorization(lu<M>* & factorization, M & m_A, vector<unsigned> & m_basis, lp_settings &m_settings) {
|
||||
if (factorization != nullptr)
|
||||
delete factorization;
|
||||
factorization = new lu<M>(m_A, m_basis, m_settings);
|
||||
// if (factorization->get_status() != LU_status::OK)
|
||||
// LP_OUT(m_settings, "failing in init_factorization" << std::endl);
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> get_B(lu<M>& f, const vector<unsigned>& basis) {
|
||||
lp_assert(basis.size() == f.dimension());
|
||||
lp_assert(basis.size() == f.m_U.dimension());
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> B(f.dimension(), f.dimension());
|
||||
for (unsigned i = 0; i < f.dimension(); i++)
|
||||
for (unsigned j = 0; j < f.dimension(); j++)
|
||||
B.set_elem(i, j, f.B_(i, j, basis));
|
||||
|
||||
return B;
|
||||
}
|
||||
template <typename M>
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> get_B(lu<M>& f) {
|
||||
dense_matrix<typename M::coefftype, typename M::argtype> B(f.dimension(), f.dimension());
|
||||
for (unsigned i = 0; i < f.dimension(); i++)
|
||||
for (unsigned j = 0; j < f.dimension(); j++)
|
||||
B.set_elem(i, j, f.m_A[i][j]);
|
||||
|
||||
return B;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
@ -22,10 +22,8 @@ Revision History:
|
|||
#include "math/lp/static_matrix.h"
|
||||
#include <string>
|
||||
#ifdef Z3DEBUG
|
||||
template bool lp::matrix<double, double>::is_equal(lp::matrix<double, double> const&);
|
||||
template bool lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::is_equal(lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> > const&);
|
||||
template bool lp::matrix<lp::mpq, lp::mpq>::is_equal(lp::matrix<lp::mpq, lp::mpq> const&);
|
||||
#endif
|
||||
template void lp::print_matrix<double, double>(lp::matrix<double, double> const*, std::ostream & out);
|
||||
template void lp::print_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >(lp::matrix<lp::mpq, lp::numeric_pair<lp::mpq> > const *, std::basic_ostream<char, std::char_traits<char> > &);
|
||||
template void lp::print_matrix<lp::mpq, lp::mpq>(lp::matrix<lp::mpq, lp::mpq> const*, std::ostream&);
|
||||
|
|
|
|||
|
|
@ -32,16 +32,9 @@ bool matrix<T, X>::is_equal(const matrix<T, X>& other) {
|
|||
for (unsigned j = 0; j < column_count(); j++) {
|
||||
auto a = get_elem(i, j);
|
||||
auto b = other.get_elem(i, j);
|
||||
if (numeric_traits<T>::precise()) {
|
||||
if (a != b) return false;
|
||||
} else if (fabs(numeric_traits<T>::get_double(a - b)) > 0.000001) {
|
||||
// cout << "returning false from operator== of matrix comparison" << endl;
|
||||
// cout << "this matrix is " << endl;
|
||||
// print_matrix(*this);
|
||||
// cout << "other matrix is " << endl;
|
||||
// print_matrix(other);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (a != b) return false;
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -1,891 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// reads an MPS file representing a Mixed Integer Program
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include "util/vector.h"
|
||||
#include <unordered_map>
|
||||
#include <ostream>
|
||||
#include <fstream>
|
||||
#include <locale>
|
||||
#include "math/lp/lp_primal_simplex.h"
|
||||
#include "math/lp/lp_dual_simplex.h"
|
||||
#include "math/lp/lar_solver.h"
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/lp_solver.h"
|
||||
namespace lp {
|
||||
inline bool my_white_space(const char & a) {
|
||||
return a == ' ' || a == '\t';
|
||||
}
|
||||
inline size_t number_of_whites(const std::string & s) {
|
||||
size_t i = 0;
|
||||
for(;i < s.size(); i++)
|
||||
if (!my_white_space(s[i])) return i;
|
||||
return i;
|
||||
}
|
||||
inline size_t number_of_whites_from_end(const std::string & s) {
|
||||
size_t ret = 0;
|
||||
for(int i = static_cast<int>(s.size()) - 1;i >= 0; i--)
|
||||
if (my_white_space(s[i])) ret++;else break;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// trim from start
|
||||
inline std::string <rim(std::string &s) {
|
||||
s.erase(0, number_of_whites(s));
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// trim from end
|
||||
inline std::string &rtrim(std::string &s) {
|
||||
// s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||
s.erase(s.end() - number_of_whites_from_end(s), s.end());
|
||||
return s;
|
||||
}
|
||||
// trim from both ends
|
||||
inline std::string &trim(std::string &s) {
|
||||
return ltrim(rtrim(s));
|
||||
}
|
||||
|
||||
inline std::string trim(std::string const &r) {
|
||||
std::string s = r;
|
||||
return ltrim(rtrim(s));
|
||||
}
|
||||
|
||||
|
||||
inline vector<std::string> string_split(const std::string &source, const char *delimiter, bool keep_empty) {
|
||||
vector<std::string> results;
|
||||
size_t prev = 0;
|
||||
size_t next = 0;
|
||||
while ((next = source.find_first_of(delimiter, prev)) != std::string::npos) {
|
||||
if (keep_empty || (next - prev != 0)) {
|
||||
results.push_back(source.substr(prev, next - prev));
|
||||
}
|
||||
prev = next + 1;
|
||||
}
|
||||
if (prev < source.size()) {
|
||||
results.push_back(source.substr(prev));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
inline vector<std::string> split_and_trim(const std::string &line) {
|
||||
auto split = string_split(line, " \t", false);
|
||||
vector<std::string> ret;
|
||||
for (auto s : split) {
|
||||
ret.push_back(trim(s));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
class mps_reader {
|
||||
enum row_type { Cost, Less_or_equal, Greater_or_equal, Equal };
|
||||
struct bound {
|
||||
T m_low;
|
||||
T m_upper;
|
||||
bool m_low_is_set;
|
||||
bool m_upper_is_set;
|
||||
bool m_value_is_fixed;
|
||||
T m_fixed_value;
|
||||
bool m_free;
|
||||
// constructor
|
||||
bound() : m_low(numeric_traits<T>::zero()),
|
||||
m_low_is_set(true),
|
||||
m_upper_is_set(false),
|
||||
m_value_is_fixed(false),
|
||||
m_free(false) {} // it seems all mps files I have seen have the default low value 0 on a variable
|
||||
};
|
||||
|
||||
struct column {
|
||||
std::string m_name;
|
||||
bound * m_bound;
|
||||
unsigned m_index;
|
||||
column(const std::string &name, unsigned index): m_name(name),
|
||||
m_bound(nullptr),
|
||||
m_index(index) {
|
||||
}
|
||||
};
|
||||
|
||||
struct row {
|
||||
row_type m_type;
|
||||
std::string m_name;
|
||||
std::unordered_map<std::string, T> m_row_columns;
|
||||
unsigned m_index;
|
||||
T m_right_side;
|
||||
T m_range;
|
||||
row(row_type type, const std::string &name, unsigned index) :
|
||||
m_type(type),
|
||||
m_name(name),
|
||||
m_index(index),
|
||||
m_right_side(zero_of_type<T>()),
|
||||
m_range(zero_of_type<T>())
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
bool m_is_OK;
|
||||
std::string m_file_name;
|
||||
std::unordered_map<std::string, row *> m_rows;
|
||||
std::unordered_map<std::string, column *> m_columns;
|
||||
std::unordered_map<std::string, unsigned> m_names_to_var_index;
|
||||
std::string m_line;
|
||||
std::string m_name;
|
||||
std::string m_cost_row_name;
|
||||
std::ifstream m_file_stream;
|
||||
// needed to adjust the index row
|
||||
unsigned m_cost_line_count;
|
||||
unsigned m_line_number;
|
||||
std::ostream * m_message_stream;
|
||||
|
||||
void set_m_ok_to_false() {
|
||||
*m_message_stream << "setting m_is_OK to false" << std::endl;
|
||||
m_is_OK = false;
|
||||
}
|
||||
|
||||
std::string get_string_from_position(unsigned offset) {
|
||||
unsigned i = offset;
|
||||
for (; i < m_line.size(); i++){
|
||||
if (m_line[i] == ' ')
|
||||
break;
|
||||
}
|
||||
lp_assert(m_line.size() >= offset);
|
||||
lp_assert(m_line.size() >> i);
|
||||
lp_assert(i >= offset);
|
||||
return m_line.substr(offset, i - offset);
|
||||
}
|
||||
|
||||
void set_boundary_for_column(unsigned col, bound * b, lp_solver<T, X> * solver){
|
||||
if (b == nullptr) {
|
||||
solver->set_lower_bound(col, numeric_traits<T>::zero());
|
||||
return;
|
||||
}
|
||||
|
||||
if (b->m_free) {
|
||||
return;
|
||||
}
|
||||
if (b->m_low_is_set) {
|
||||
solver->set_lower_bound(col, b->m_low);
|
||||
}
|
||||
if (b->m_upper_is_set) {
|
||||
solver->set_upper_bound(col, b->m_upper);
|
||||
}
|
||||
|
||||
if (b->m_value_is_fixed) {
|
||||
solver->set_fixed_value(col, b->m_fixed_value);
|
||||
}
|
||||
}
|
||||
|
||||
bool all_white_space() {
|
||||
for (unsigned i = 0; i < m_line.size(); i++) {
|
||||
char c = m_line[i];
|
||||
if (c != ' ' && c != '\t') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void read_line() {
|
||||
while (m_is_OK) {
|
||||
if (!getline(m_file_stream, m_line)) {
|
||||
m_line_number++;
|
||||
set_m_ok_to_false();
|
||||
*m_message_stream << "cannot read from file" << std::endl;
|
||||
}
|
||||
m_line_number++;
|
||||
if (!m_line.empty() && m_line[0] != '*' && !all_white_space())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void read_name() {
|
||||
do {
|
||||
read_line();
|
||||
if (m_line.find("NAME") != 0) {
|
||||
continue;
|
||||
}
|
||||
m_line = m_line.substr(4);
|
||||
m_name = trim(m_line);
|
||||
break;
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
void read_rows() {
|
||||
// look for start of the rows
|
||||
read_line();
|
||||
do {
|
||||
if (static_cast<int>(m_line.find("ROWS")) >= 0) {
|
||||
break;
|
||||
}
|
||||
} while (m_is_OK);
|
||||
do {
|
||||
read_line();
|
||||
if (m_line.find("COLUMNS") == 0) {
|
||||
break;
|
||||
}
|
||||
add_row();
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
void read_column_by_columns(const std::string & column_name, std::string column_data) {
|
||||
// uph, let us try to work with columns
|
||||
if (column_data.size() >= 22) {
|
||||
std::string ss = column_data.substr(0, 8);
|
||||
std::string row_name = trim(ss);
|
||||
auto t = m_rows.find(row_name);
|
||||
|
||||
if (t == m_rows.end()) {
|
||||
*m_message_stream << "cannot find " << row_name << std::endl;
|
||||
goto fail;
|
||||
} else {
|
||||
row * row = t->second;
|
||||
row->m_row_columns[column_name] = numeric_traits<T>::from_string(column_data.substr(8));
|
||||
if (column_data.size() > 24) {
|
||||
column_data = column_data.substr(25);
|
||||
if (column_data.size() >= 22) {
|
||||
read_column_by_columns(column_name, column_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fail:
|
||||
set_m_ok_to_false();
|
||||
*m_message_stream << "cannot understand this line\n"
|
||||
"line = " << m_line << ", line number is " << m_line_number << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void read_column(const std::string & column_name, const std::string & column_data){
|
||||
auto tokens = split_and_trim(column_data);
|
||||
for (unsigned i = 0; i < tokens.size() - 1; i+= 2) {
|
||||
auto row_name = tokens[i];
|
||||
if (row_name == "'MARKER'") return; // it is the integrality marker, no real data here
|
||||
auto t = m_rows.find(row_name);
|
||||
if (t == m_rows.end()) {
|
||||
read_column_by_columns(column_name, column_data);
|
||||
return;
|
||||
}
|
||||
row *r = t->second;
|
||||
r->m_row_columns[column_name] = numeric_traits<T>::from_string(tokens[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
void read_columns(){
|
||||
std::string column_name;
|
||||
do {
|
||||
read_line();
|
||||
if (m_line.find("RHS") == 0) {
|
||||
break;
|
||||
}
|
||||
if (m_line.size() < 22) {
|
||||
(*m_message_stream) << "line is too short for a column" << std::endl;
|
||||
(*m_message_stream) << m_line << std::endl;
|
||||
(*m_message_stream) << "line number is " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
std::string column_name_tmp = trim(m_line.substr(4, 8));
|
||||
if (!column_name_tmp.empty()) {
|
||||
column_name = column_name_tmp;
|
||||
}
|
||||
auto col_it = m_columns.find(column_name);
|
||||
mps_reader::column * col;
|
||||
if (col_it == m_columns.end()) {
|
||||
col = new mps_reader::column(column_name, static_cast<unsigned>(m_columns.size()));
|
||||
m_columns[column_name] = col;
|
||||
// (*m_message_stream) << column_name << '[' << col->m_index << ']'<< std::endl;
|
||||
} else {
|
||||
col = col_it->second;
|
||||
}
|
||||
read_column(column_name, m_line.substr(14));
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
void read_rhs() {
|
||||
do {
|
||||
read_line();
|
||||
if (m_line.find("BOUNDS") == 0 || m_line.find("ENDATA") == 0 || m_line.find("RANGES") == 0) {
|
||||
break;
|
||||
}
|
||||
fill_rhs();
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
|
||||
void fill_rhs_by_columns(std::string rhsides) {
|
||||
// uph, let us try to work with columns
|
||||
if (rhsides.size() >= 22) {
|
||||
std::string ss = rhsides.substr(0, 8);
|
||||
std::string row_name = trim(ss);
|
||||
auto t = m_rows.find(row_name);
|
||||
|
||||
if (t == m_rows.end()) {
|
||||
(*m_message_stream) << "cannot find " << row_name << std::endl;
|
||||
goto fail;
|
||||
} else {
|
||||
row * row = t->second;
|
||||
row->m_right_side = numeric_traits<T>::from_string(rhsides.substr(8));
|
||||
if (rhsides.size() > 24) {
|
||||
rhsides = rhsides.substr(25);
|
||||
if (rhsides.size() >= 22) {
|
||||
fill_rhs_by_columns(rhsides);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fail:
|
||||
set_m_ok_to_false();
|
||||
(*m_message_stream) << "cannot understand this line" << std::endl;
|
||||
(*m_message_stream) << "line = " << m_line << ", line number is " << m_line_number << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void fill_rhs() {
|
||||
if (m_line.size() < 14) {
|
||||
(*m_message_stream) << "line is too short" << std::endl;
|
||||
(*m_message_stream) << m_line << std::endl;
|
||||
(*m_message_stream) << "line number is " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
std::string rhsides = m_line.substr(14);
|
||||
vector<std::string> splitted_line = split_and_trim(rhsides);
|
||||
|
||||
for (unsigned i = 0; i < splitted_line.size() - 1; i += 2) {
|
||||
auto t = m_rows.find(splitted_line[i]);
|
||||
if (t == m_rows.end()) {
|
||||
fill_rhs_by_columns(rhsides);
|
||||
return;
|
||||
}
|
||||
row * row = t->second;
|
||||
row->m_right_side = numeric_traits<T>::from_string(splitted_line[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
void read_bounds() {
|
||||
if (m_line.find("BOUNDS") != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
read_line();
|
||||
if (m_line[0] != ' ') {
|
||||
break;
|
||||
}
|
||||
create_or_update_bound();
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
void read_ranges() {
|
||||
if (m_line.find("RANGES") != 0) {
|
||||
return;
|
||||
}
|
||||
do {
|
||||
read_line();
|
||||
auto sl = split_and_trim(m_line);
|
||||
if (sl.size() < 2) {
|
||||
break;
|
||||
}
|
||||
read_range(sl);
|
||||
} while (m_is_OK);
|
||||
}
|
||||
|
||||
|
||||
void read_bound_by_columns(const std::string & colstr) {
|
||||
if (colstr.size() < 14) {
|
||||
(*m_message_stream) << "line is too short" << std::endl;
|
||||
(*m_message_stream) << m_line << std::endl;
|
||||
(*m_message_stream) << "line number is " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
// uph, let us try to work with columns
|
||||
if (colstr.size() >= 22) {
|
||||
std::string ss = colstr.substr(0, 8);
|
||||
std::string column_name = trim(ss);
|
||||
auto t = m_columns.find(column_name);
|
||||
|
||||
if (t == m_columns.end()) {
|
||||
(*m_message_stream) << "cannot find " << column_name << std::endl;
|
||||
goto fail;
|
||||
} else {
|
||||
vector<std::string> bound_string;
|
||||
bound_string.push_back(column_name);
|
||||
if (colstr.size() > 14) {
|
||||
bound_string.push_back(colstr.substr(14));
|
||||
}
|
||||
mps_reader::column * col = t->second;
|
||||
bound * b = col->m_bound;
|
||||
if (b == nullptr) {
|
||||
col->m_bound = b = new bound();
|
||||
}
|
||||
update_bound(b, bound_string);
|
||||
}
|
||||
} else {
|
||||
fail:
|
||||
set_m_ok_to_false();
|
||||
(*m_message_stream) << "cannot understand this line" << std::endl;
|
||||
(*m_message_stream) << "line = " << m_line << ", line number is " << m_line_number << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void update_bound(bound * b, vector<std::string> bound_string) {
|
||||
/*
|
||||
UP means an upper bound is applied to the variable. A bound of type LO means a lower bound is applied. A bound type of FX ("fixed") means that the variable has upper and lower bounds equal to a single value. A bound type of FR ("free") means the variable has neither lower nor upper bounds and so can take on negative values. A variation on that is MI for free negative, giving an upper bound of 0 but no lower bound. Bound type PL is for a free positive for zero to plus infinity, but as this is the normal default, it is seldom used. There are also bound types for use in MIP models - BV for binary, being 0 or 1. UI for upper integer and LI for lower integer. SC stands for semi-continuous and indicates that the variable may be zero, but if not must be equal to at least the value given.
|
||||
*/
|
||||
|
||||
std::string bound_type = get_string_from_position(1);
|
||||
if (bound_type == "BV") {
|
||||
b->m_upper_is_set = true;
|
||||
b->m_upper = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (bound_type == "UP" || bound_type == "UI" || bound_type == "LIMITMAX") {
|
||||
if (bound_string.size() <= 1){
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
b->m_upper_is_set = true;
|
||||
b->m_upper= numeric_traits<T>::from_string(bound_string[1]);
|
||||
} else if (bound_type == "LO" || bound_type == "LI") {
|
||||
if (bound_string.size() <= 1){
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
|
||||
b->m_low_is_set = true;
|
||||
b->m_low = numeric_traits<T>::from_string(bound_string[1]);
|
||||
} else if (bound_type == "FR") {
|
||||
b->m_free = true;
|
||||
} else if (bound_type == "FX") {
|
||||
if (bound_string.size() <= 1){
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
|
||||
b->m_value_is_fixed = true;
|
||||
b->m_fixed_value = numeric_traits<T>::from_string(bound_string[1]);
|
||||
} else if (bound_type == "PL") {
|
||||
b->m_low_is_set = true;
|
||||
b->m_low = 0;
|
||||
} else if (bound_type == "MI") {
|
||||
b->m_upper_is_set = true;
|
||||
b->m_upper = 0;
|
||||
} else {
|
||||
(*m_message_stream) << "unexpected bound type " << bound_type << " at line " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void create_or_update_bound() {
|
||||
const unsigned name_offset = 14;
|
||||
lp_assert(m_line.size() >= 14);
|
||||
vector<std::string> bound_string = split_and_trim(m_line.substr(name_offset, m_line.size()));
|
||||
|
||||
if (bound_string.empty()) {
|
||||
set_m_ok_to_false();
|
||||
(*m_message_stream) << "error at line " << m_line_number << std::endl;
|
||||
throw m_line;
|
||||
}
|
||||
|
||||
std::string name = bound_string[0];
|
||||
auto it = m_columns.find(name);
|
||||
if (it == m_columns.end()){
|
||||
read_bound_by_columns(m_line.substr(14));
|
||||
return;
|
||||
}
|
||||
mps_reader::column * col = it->second;
|
||||
bound * b = col->m_bound;
|
||||
if (b == nullptr) {
|
||||
col->m_bound = b = new bound();
|
||||
}
|
||||
update_bound(b, bound_string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void read_range_by_columns(std::string rhsides) {
|
||||
if (m_line.size() < 14) {
|
||||
(*m_message_stream) << "line is too short" << std::endl;
|
||||
(*m_message_stream) << m_line << std::endl;
|
||||
(*m_message_stream) << "line number is " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
// uph, let us try to work with columns
|
||||
if (rhsides.size() >= 22) {
|
||||
std::string ss = rhsides.substr(0, 8);
|
||||
std::string row_name = trim(ss);
|
||||
auto t = m_rows.find(row_name);
|
||||
|
||||
if (t == m_rows.end()) {
|
||||
(*m_message_stream) << "cannot find " << row_name << std::endl;
|
||||
goto fail;
|
||||
} else {
|
||||
row * row = t->second;
|
||||
row->m_range = numeric_traits<T>::from_string(rhsides.substr(8));
|
||||
maybe_modify_current_row_and_add_row_for_range(row);
|
||||
if (rhsides.size() > 24) {
|
||||
rhsides = rhsides.substr(25);
|
||||
if (rhsides.size() >= 22) {
|
||||
read_range_by_columns(rhsides);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fail:
|
||||
set_m_ok_to_false();
|
||||
(*m_message_stream) << "cannot understand this line" << std::endl;
|
||||
(*m_message_stream) << "line = " << m_line << ", line number is " << m_line_number << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void read_range(vector<std::string> & splitted_line){
|
||||
for (unsigned i = 1; i < splitted_line.size() - 1; i += 2) {
|
||||
auto it = m_rows.find(splitted_line[i]);
|
||||
if (it == m_rows.end()) {
|
||||
read_range_by_columns(m_line.substr(14));
|
||||
return;
|
||||
}
|
||||
row * row = it->second;
|
||||
row->m_range = numeric_traits<T>::from_string(splitted_line[i + 1]);
|
||||
maybe_modify_current_row_and_add_row_for_range(row);
|
||||
}
|
||||
}
|
||||
|
||||
void maybe_modify_current_row_and_add_row_for_range(row * row_with_range) {
|
||||
unsigned index= static_cast<unsigned>(m_rows.size() - m_cost_line_count);
|
||||
std::string row_name = row_with_range->m_name + "_range";
|
||||
row * other_bound_range_row;
|
||||
switch (row_with_range->m_type) {
|
||||
case row_type::Greater_or_equal:
|
||||
m_rows[row_name] = other_bound_range_row = new row(row_type::Less_or_equal, row_name, index);
|
||||
other_bound_range_row->m_right_side = row_with_range->m_right_side + abs(row_with_range->m_range);
|
||||
break;
|
||||
case row_type::Less_or_equal:
|
||||
m_rows[row_name] = other_bound_range_row = new row(row_type::Greater_or_equal, row_name, index);
|
||||
other_bound_range_row->m_right_side = row_with_range->m_right_side - abs(row_with_range->m_range);
|
||||
break;
|
||||
case row_type::Equal:
|
||||
if (row_with_range->m_range > 0) {
|
||||
row_with_range->m_type = row_type::Greater_or_equal; // the existing row type change
|
||||
m_rows[row_name] = other_bound_range_row = new row(row_type::Less_or_equal, row_name, index);
|
||||
} else { // row->m_range < 0;
|
||||
row_with_range->m_type = row_type::Less_or_equal; // the existing row type change
|
||||
m_rows[row_name] = other_bound_range_row = new row(row_type::Greater_or_equal, row_name, index);
|
||||
}
|
||||
other_bound_range_row->m_right_side = row_with_range->m_right_side + row_with_range->m_range;
|
||||
break;
|
||||
default:
|
||||
(*m_message_stream) << "unexpected bound type " << row_with_range->m_type << " at line " << m_line_number << std::endl;
|
||||
set_m_ok_to_false();
|
||||
throw;
|
||||
}
|
||||
|
||||
for (auto s : row_with_range->m_row_columns) {
|
||||
lp_assert(m_columns.find(s.first) != m_columns.end());
|
||||
other_bound_range_row->m_row_columns[s.first] = s.second;
|
||||
}
|
||||
}
|
||||
|
||||
void add_row() {
|
||||
if (m_line.length() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_line = trim(m_line);
|
||||
char c = m_line[0];
|
||||
m_line = m_line.substr(1);
|
||||
m_line = trim(m_line);
|
||||
add_row(c);
|
||||
}
|
||||
|
||||
void add_row(char c) {
|
||||
unsigned index= static_cast<unsigned>(m_rows.size() - m_cost_line_count);
|
||||
switch (c) {
|
||||
case 'E':
|
||||
m_rows[m_line] = new row(row_type::Equal, m_line, index);
|
||||
break;
|
||||
case 'L':
|
||||
m_rows[m_line] = new row(row_type::Less_or_equal, m_line, index);
|
||||
break;
|
||||
case 'G':
|
||||
m_rows[m_line] = new row(row_type::Greater_or_equal, m_line, index);
|
||||
break;
|
||||
case 'N':
|
||||
m_rows[m_line] = new row(row_type::Cost, m_line, index);
|
||||
m_cost_row_name = m_line;
|
||||
m_cost_line_count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
unsigned range_count() {
|
||||
unsigned ret = 0;
|
||||
for (auto s : m_rows) {
|
||||
if (s.second->m_range != 0) {
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
If rhs is a constraint's right-hand-side value and range is the constraint's range value, then the range interval is defined according to the following table:
|
||||
sense interval
|
||||
G [rhs, rhs + |range|]
|
||||
L [rhs - |range|, rhs]
|
||||
E [rhs, rhs + |range|] if range > 0,
|
||||
[rhs - |range|, rhs] if range < 0
|
||||
where |range| is range's absolute value.
|
||||
*/
|
||||
|
||||
lp_relation get_relation_from_row(row_type rt) {
|
||||
switch (rt) {
|
||||
case mps_reader::Less_or_equal: return lp_relation::Less_or_equal;
|
||||
case mps_reader::Greater_or_equal: return lp_relation::Greater_or_equal;
|
||||
case mps_reader::Equal: return lp_relation::Equal;
|
||||
default:
|
||||
(*m_message_stream) << "Unexpected rt " << rt << std::endl;
|
||||
set_m_ok_to_false();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned solver_row_count() {
|
||||
return m_rows.size() - m_cost_line_count + range_count();
|
||||
}
|
||||
|
||||
void fill_solver_on_row(row * row, lp_solver<T, X> *solver) {
|
||||
if (row->m_name != m_cost_row_name) {
|
||||
solver->add_constraint(get_relation_from_row(row->m_type), row->m_right_side, row->m_index);
|
||||
for (auto s : row->m_row_columns) {
|
||||
lp_assert(m_columns.find(s.first) != m_columns.end());
|
||||
solver->set_row_column_coefficient(row->m_index, m_columns[s.first]->m_index, s.second);
|
||||
}
|
||||
} else {
|
||||
set_solver_cost(row, solver);
|
||||
}
|
||||
}
|
||||
|
||||
T abs(T & t) { return t < numeric_traits<T>::zero() ? -t: t; }
|
||||
|
||||
void fill_solver_on_rows(lp_solver<T, X> * solver) {
|
||||
for (auto row_it : m_rows) {
|
||||
fill_solver_on_row(row_it.second, solver);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void fill_solver_on_columns(lp_solver<T, X> * solver){
|
||||
for (auto s : m_columns) {
|
||||
mps_reader::column * col = s.second;
|
||||
unsigned index = col->m_index;
|
||||
set_boundary_for_column(index, col->m_bound, solver);
|
||||
// optional call
|
||||
solver->give_symbolic_name_to_column(col->m_name, col->m_index);
|
||||
}
|
||||
}
|
||||
|
||||
void fill_solver(lp_solver<T, X> *solver) {
|
||||
fill_solver_on_rows(solver);
|
||||
fill_solver_on_columns(solver);
|
||||
}
|
||||
|
||||
void set_solver_cost(row * row, lp_solver<T, X> *solver) {
|
||||
for (auto s : row->m_row_columns) {
|
||||
std::string name = s.first;
|
||||
lp_assert(m_columns.find(name) != m_columns.end());
|
||||
mps_reader::column * col = m_columns[name];
|
||||
solver->set_cost_for_column(col->m_index, s.second);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void set_message_stream(std::ostream * o) {
|
||||
lp_assert(o != nullptr);
|
||||
m_message_stream = o;
|
||||
}
|
||||
vector<std::string> column_names() {
|
||||
vector<std::string> v;
|
||||
for (auto s : m_columns) {
|
||||
v.push_back(s.first);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
~mps_reader() {
|
||||
for (auto s : m_rows) {
|
||||
delete s.second;
|
||||
}
|
||||
for (auto s : m_columns) {
|
||||
auto col = s.second;
|
||||
delete col->m_bound;
|
||||
delete col;
|
||||
}
|
||||
}
|
||||
|
||||
mps_reader(const std::string & file_name):
|
||||
m_is_OK(true),
|
||||
m_file_name(file_name),
|
||||
m_file_stream(file_name),
|
||||
m_cost_line_count(0),
|
||||
m_line_number(0),
|
||||
m_message_stream(& std::cout) {}
|
||||
void read() {
|
||||
if (!m_file_stream.is_open()){
|
||||
set_m_ok_to_false();
|
||||
return;
|
||||
}
|
||||
|
||||
read_name();
|
||||
read_rows();
|
||||
read_columns();
|
||||
read_rhs();
|
||||
if (m_line.find("BOUNDS") == 0) {
|
||||
read_bounds();
|
||||
read_ranges();
|
||||
} else if (m_line.find("RANGES") == 0) {
|
||||
read_ranges();
|
||||
read_bounds();
|
||||
}
|
||||
}
|
||||
|
||||
bool is_ok() {
|
||||
return m_is_OK;
|
||||
}
|
||||
|
||||
lp_solver<T, X> * create_solver(bool dual) {
|
||||
lp_solver<T, X> * solver = dual? (lp_solver<T, X>*)new lp_dual_simplex<T, X>() : new lp_primal_simplex<T, X>();
|
||||
fill_solver(solver);
|
||||
return solver;
|
||||
}
|
||||
|
||||
lconstraint_kind get_lar_relation_from_row(row_type rt) {
|
||||
switch (rt) {
|
||||
case Less_or_equal: return LE;
|
||||
case Greater_or_equal: return GE;
|
||||
case Equal: return EQ;
|
||||
default:
|
||||
(*m_message_stream) << "Unexpected rt " << rt << std::endl;
|
||||
set_m_ok_to_false();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned get_var_index(std::string s) {
|
||||
auto it = m_names_to_var_index.find(s);
|
||||
if (it != m_names_to_var_index.end())
|
||||
return it->second;
|
||||
unsigned ret = static_cast<unsigned>(m_names_to_var_index.size());
|
||||
m_names_to_var_index[s] = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void fill_lar_solver_on_row(row * row, lar_solver *solver, int row_index) {
|
||||
if (row->m_name != m_cost_row_name) {
|
||||
auto kind = get_lar_relation_from_row(row->m_type);
|
||||
vector<std::pair<mpq, var_index>> ls;
|
||||
for (auto s : row->m_row_columns) {
|
||||
var_index i = solver->add_var(get_var_index(s.first), false);
|
||||
ls.push_back(std::make_pair(s.second, i));
|
||||
}
|
||||
unsigned j = solver->add_term(ls, row_index);
|
||||
solver->add_var_bound(j, kind, row->m_right_side);
|
||||
} else {
|
||||
// ignore the cost row
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void fill_lar_solver_on_rows(lar_solver * solver) {
|
||||
int row_index = 0;
|
||||
for (auto row_it : m_rows) {
|
||||
fill_lar_solver_on_row(row_it.second, solver, row_index++);
|
||||
}
|
||||
}
|
||||
|
||||
void create_low_constraint_for_var(column* col, bound * b, lar_solver *solver) {
|
||||
var_index i = solver->add_var(col->m_index, false);
|
||||
solver->add_var_bound(i, GE, b->m_low);
|
||||
}
|
||||
|
||||
void create_upper_constraint_for_var(column* col, bound * b, lar_solver *solver) {
|
||||
var_index i = solver->add_var(col->m_index, false);
|
||||
solver->add_var_bound(i, LE, b->m_upper);
|
||||
}
|
||||
|
||||
void create_equality_contraint_for_var(column* col, bound * b, lar_solver *solver) {
|
||||
var_index i = solver->add_var(col->m_index, false);
|
||||
solver->add_var_bound(i, LE, b->m_fixed_value);
|
||||
solver->add_var_bound(i, GE, b->m_fixed_value);
|
||||
}
|
||||
|
||||
void fill_lar_solver_on_columns(lar_solver * solver) {
|
||||
for (auto s : m_columns) {
|
||||
mps_reader::column * col = s.second;
|
||||
solver->add_var(col->m_index, false);
|
||||
auto b = col->m_bound;
|
||||
if (b == nullptr) return;
|
||||
|
||||
if (b->m_free) continue;
|
||||
|
||||
if (b->m_low_is_set) {
|
||||
create_low_constraint_for_var(col, b, solver);
|
||||
}
|
||||
if (b->m_upper_is_set) {
|
||||
create_upper_constraint_for_var(col, b, solver);
|
||||
}
|
||||
if (b->m_value_is_fixed) {
|
||||
create_equality_contraint_for_var(col, b, solver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void fill_lar_solver(lar_solver * solver) {
|
||||
fill_lar_solver_on_columns(solver);
|
||||
fill_lar_solver_on_rows(solver);
|
||||
}
|
||||
|
||||
lar_solver * create_lar_solver() {
|
||||
lar_solver * solver = new lar_solver();
|
||||
fill_lar_solver(solver);
|
||||
return solver;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -1745,6 +1745,13 @@ bool core::influences_nl_var(lpvar j) const {
|
|||
return false;
|
||||
}
|
||||
|
||||
void core::set_use_nra_model(bool m) {
|
||||
if (m != m_use_nra_model) {
|
||||
trail().push(value_trail(m_use_nra_model));
|
||||
m_use_nra_model = m;
|
||||
}
|
||||
}
|
||||
|
||||
void core::collect_statistics(::statistics & st) {
|
||||
st.update("arith-nla-explanations", m_stats.m_nla_explanations);
|
||||
st.update("arith-nla-lemmas", m_stats.m_nla_lemmas);
|
||||
|
|
|
|||
|
|
@ -418,7 +418,7 @@ public:
|
|||
bool var_is_big(lpvar) const;
|
||||
bool has_real(const factorization&) const;
|
||||
bool has_real(const monic& m) const;
|
||||
void set_use_nra_model(bool m) { m_use_nra_model = m; }
|
||||
void set_use_nra_model(bool m);
|
||||
bool use_nra_model() const { return m_use_nra_model; }
|
||||
void collect_statistics(::statistics&);
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -58,10 +58,11 @@ namespace nla {
|
|||
|
||||
auto monotonicity1 = [&](auto x1, auto& x1val, auto y1, auto& y1val, auto& q1, auto& q1val,
|
||||
auto x2, auto& x2val, auto y2, auto& y2val, auto& q2, auto& q2val) {
|
||||
if (y1val >= y2val && y2val > 0 && x1val <= x2val && q1val > q2val) {
|
||||
new_lemma lemma(c, "y1 >= y2 > 0 & x1 <= x2 => x1/y1 <= x2/y2");
|
||||
if (y1val >= y2val && y2val > 0 && 0 <= x1val && x1val <= x2val && q1val > q2val) {
|
||||
new_lemma lemma(c, "y1 >= y2 > 0 & 0 <= x1 <= x2 => x1/y1 <= x2/y2");
|
||||
lemma |= ineq(term(y1, rational(-1), y2), llc::LT, 0);
|
||||
lemma |= ineq(y2, llc::LE, 0);
|
||||
lemma |= ineq(x1, llc::LT, 0);
|
||||
lemma |= ineq(term(x1, rational(-1), x2), llc::GT, 0);
|
||||
lemma |= ineq(term(q1, rational(-1), q2), llc::LE, 0);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -79,8 +79,11 @@ am().set(rval, am_value(r));
|
|||
namespace nla {
|
||||
|
||||
lbool powers::check(lpvar r, lpvar x, lpvar y, vector<lemma>& lemmas) {
|
||||
TRACE("nla", tout << r << " == " << x << "^" << y << "\n");
|
||||
if (x == null_lpvar || y == null_lpvar || r == null_lpvar)
|
||||
return l_undef;
|
||||
if (lp::tv::is_term(x) || lp::tv::is_term(y) || lp::tv::is_term(r))
|
||||
return l_undef;
|
||||
|
||||
core& c = m_core;
|
||||
if (c.use_nra_model())
|
||||
|
|
@ -143,17 +146,24 @@ namespace nla {
|
|||
auto r2val = power(xval, yval.get_unsigned());
|
||||
if (rval == r2val)
|
||||
return l_true;
|
||||
if (xval > 0 && r2val < rval) {
|
||||
SASSERT(yval > 0);
|
||||
if (c.random() % 2 == 0) {
|
||||
new_lemma lemma(c, "x == x0, y == y0 => r = x0^y0");
|
||||
lemma |= ineq(x, llc::NE, xval);
|
||||
lemma |= ineq(y, llc::NE, yval);
|
||||
lemma |= ineq(r, llc::EQ, r2val);
|
||||
return l_false;
|
||||
}
|
||||
if (yval > 0 && r2val > rval) {
|
||||
new_lemma lemma(c, "x >= x0 > 0, y >= y0 > 0 => r >= x0^y0");
|
||||
lemma |= ineq(x, llc::LT, xval);
|
||||
lemma |= ineq(y, llc::LT, yval);
|
||||
lemma |= ineq(r, llc::GE, r2val);
|
||||
return l_false;
|
||||
}
|
||||
if (xval > 0 && r2val < rval) {
|
||||
new_lemma lemma(c, "x >= x0 > 0, y <= y0 => r <= x0^y0");
|
||||
lemma |= ineq(x, llc::LT, xval);
|
||||
if (r2val < rval) {
|
||||
new_lemma lemma(c, "0 < x <= x0, y <= y0 => r <= x0^y0");
|
||||
lemma |= ineq(x, llc::LE, rational::zero());
|
||||
lemma |= ineq(x, llc::GT, xval);
|
||||
lemma |= ineq(y, llc::GT, yval);
|
||||
lemma |= ineq(r, llc::LE, r2val);
|
||||
return l_false;
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@ struct solver::imp {
|
|||
lit = m_nlsat->mk_ineq_literal(nlsat::atom::kind::EQ, 1, ps, is_even);
|
||||
break;
|
||||
default:
|
||||
lp_assert(false); // unreachable
|
||||
UNREACHABLE(); // unreachable
|
||||
}
|
||||
m_nlsat->mk_clause(1, &lit, a);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ template <typename T> class numeric_traits {};
|
|||
|
||||
template <> class numeric_traits<unsigned> {
|
||||
public:
|
||||
static bool precise() { return true; }
|
||||
static unsigned zero() { return 0; }
|
||||
static unsigned one() { return 1; }
|
||||
static bool is_zero(unsigned v) { return v == 0; }
|
||||
|
|
@ -56,7 +55,6 @@ public:
|
|||
|
||||
template <> class numeric_traits<int> {
|
||||
public:
|
||||
static bool precise() { return true; }
|
||||
static int zero() { return 0; }
|
||||
static int one() { return 1; }
|
||||
static bool is_zero(int v) { return v == 0; }
|
||||
|
|
@ -71,7 +69,6 @@ public:
|
|||
|
||||
template <> class numeric_traits<double> {
|
||||
public:
|
||||
static bool precise() { return false; }
|
||||
static double g_zero;
|
||||
static double const &zero() { return g_zero; }
|
||||
static double g_one;
|
||||
|
|
@ -88,7 +85,6 @@ public:
|
|||
template<>
|
||||
class numeric_traits<rational> {
|
||||
public:
|
||||
static bool precise() { return true; }
|
||||
static rational const & zero() { return rational::zero(); }
|
||||
static rational const & one() { return rational::one(); }
|
||||
static bool is_zero(const rational & v) { return v.is_zero(); }
|
||||
|
|
@ -111,21 +107,8 @@ public:
|
|||
template <typename X, typename Y>
|
||||
struct convert_struct {
|
||||
static X convert(const Y & y){ return X(y);}
|
||||
static bool is_epsilon_small(const X & x, const double & y) { return std::abs(numeric_traits<X>::get_double(x)) < y; }
|
||||
static bool below_bound_numeric(const X &, const X &, const Y &) { /*lp_unreachable();*/ return false;}
|
||||
static bool above_bound_numeric(const X &, const X &, const Y &) { /*lp_unreachable();*/ return false; }
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct convert_struct<double, mpq> {
|
||||
static double convert(const mpq & q) {return q.get_double();}
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct convert_struct<mpq, unsigned> {
|
||||
static mpq convert(unsigned q) {return mpq(q);}
|
||||
static bool below_bound_numeric(const X &, const X &, const Y &) { /*UNREACHABLE();*/ return false;}
|
||||
static bool above_bound_numeric(const X &, const X &, const Y &) { /*UNREACHABLE();*/ return false; }
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -207,7 +190,7 @@ struct numeric_pair {
|
|||
}
|
||||
|
||||
numeric_pair operator/(const numeric_pair &) const {
|
||||
// lp_unreachable();
|
||||
// UNREACHABLE();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -216,7 +199,7 @@ struct numeric_pair {
|
|||
}
|
||||
|
||||
numeric_pair operator*(const numeric_pair & /*a*/) const {
|
||||
// lp_unreachable();
|
||||
// UNREACHABLE();
|
||||
}
|
||||
|
||||
numeric_pair& operator+=(const numeric_pair & a) {
|
||||
|
|
@ -251,8 +234,6 @@ struct numeric_pair {
|
|||
return numeric_pair(-x, -y);
|
||||
}
|
||||
|
||||
static bool precize() { return lp::numeric_traits<T>::precize();}
|
||||
|
||||
bool is_zero() const { return x.is_zero() && y.is_zero(); }
|
||||
|
||||
bool is_pos() const { return x.is_pos() || (x.is_zero() && y.is_pos());}
|
||||
|
|
@ -294,16 +275,14 @@ numeric_pair<T> operator/(const numeric_pair<T> & r, const X & a) {
|
|||
return numeric_pair<T>(r.x / a, r.y / a);
|
||||
}
|
||||
|
||||
// template <numeric_pair, typename T> bool precise() { return numeric_traits<T>::precise();}
|
||||
template <typename T> double get_double(const lp::numeric_pair<T> & ) { /* lp_unreachable(); */ return 0;}
|
||||
template <typename T> double get_double(const lp::numeric_pair<T> & ) { /* UNREACHABLE(); */ return 0;}
|
||||
template <typename T>
|
||||
class numeric_traits<lp::numeric_pair<T>> {
|
||||
public:
|
||||
static bool precise() { return numeric_traits<T>::precise();}
|
||||
static lp::numeric_pair<T> zero() { return lp::numeric_pair<T>(numeric_traits<T>::zero(), numeric_traits<T>::zero()); }
|
||||
static bool is_zero(const lp::numeric_pair<T> & v) { return numeric_traits<T>::is_zero(v.x) && numeric_traits<T>::is_zero(v.y); }
|
||||
static double get_double(const lp::numeric_pair<T> & v){ return numeric_traits<T>::get_double(v.x); } // just return the double of the first coordinate
|
||||
static double one() { /*lp_unreachable();*/ return 0;}
|
||||
static double one() { /*UNREACHABLE();*/ return 0;}
|
||||
static bool is_pos(const numeric_pair<T> &p) {
|
||||
return numeric_traits<T>::is_pos(p.x) ||
|
||||
(numeric_traits<T>::is_zero(p.x) && numeric_traits<T>::is_pos(p.y));
|
||||
|
|
@ -317,83 +296,8 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct convert_struct<double, numeric_pair<double>> {
|
||||
static double convert(const numeric_pair<double> & q) {return q.x;}
|
||||
};
|
||||
|
||||
typedef numeric_pair<mpq> impq;
|
||||
|
||||
template <typename X> bool is_epsilon_small(const X & v, const double& eps); // forward definition { return convert_struct<X, double>::is_epsilon_small(v, eps);}
|
||||
|
||||
template <typename T>
|
||||
struct convert_struct<numeric_pair<T>, double> {
|
||||
static numeric_pair<T> convert(const double & q) {
|
||||
return numeric_pair<T>(convert_struct<T, double>::convert(q), numeric_traits<T>::zero());
|
||||
}
|
||||
static bool is_epsilon_small(const numeric_pair<T> & p, const double & eps) {
|
||||
return convert_struct<T, double>::is_epsilon_small(p.x, eps) && convert_struct<T, double>::is_epsilon_small(p.y, eps);
|
||||
}
|
||||
static bool below_bound_numeric(const numeric_pair<T> &, const numeric_pair<T> &, const double &) {
|
||||
// lp_unreachable();
|
||||
return false;
|
||||
}
|
||||
static bool above_bound_numeric(const numeric_pair<T> &, const numeric_pair<T> &, const double &) {
|
||||
// lp_unreachable();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct convert_struct<numeric_pair<double>, double> {
|
||||
static numeric_pair<double> convert(const double & q) {
|
||||
return numeric_pair<double>(q, 0.0);
|
||||
}
|
||||
static bool is_epsilon_small(const numeric_pair<double> & p, const double & eps) {
|
||||
return std::abs(p.x) < eps && std::abs(p.y) < eps;
|
||||
}
|
||||
|
||||
static int compare_on_coord(const double & x, const double & bound, const double eps) {
|
||||
if (bound == 0) return (x < - eps)? -1: (x > eps? 1 : 0); // it is an important special case
|
||||
double relative = (bound > 0)? - eps: eps;
|
||||
return (x < bound * (1.0 + relative) - eps)? -1 : ((x > bound * (1.0 - relative) + eps)? 1 : 0);
|
||||
}
|
||||
|
||||
static bool below_bound_numeric(const numeric_pair<double> & x, const numeric_pair<double> & bound, const double & eps) {
|
||||
int r = compare_on_coord(x.x, bound.x, eps);
|
||||
if (r == 1) return false;
|
||||
if (r == -1) return true;
|
||||
// the first coordinates are almost the same
|
||||
return compare_on_coord(x.y, bound.y, eps) == -1;
|
||||
}
|
||||
|
||||
static bool above_bound_numeric(const numeric_pair<double> & x, const numeric_pair<double> & bound, const double & eps) {
|
||||
int r = compare_on_coord(x.x, bound.x, eps);
|
||||
if (r == -1) return false;
|
||||
if (r == 1) return true;
|
||||
// the first coordinates are almost the same
|
||||
return compare_on_coord(x.y, bound.y, eps) == 1;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct convert_struct<double, double> {
|
||||
static bool is_epsilon_small(const double& x, const double & eps) {
|
||||
return x < eps && x > -eps;
|
||||
}
|
||||
static double convert(const double & y){ return y;}
|
||||
static bool below_bound_numeric(const double & x, const double & bound, const double & eps) {
|
||||
if (bound == 0) return x < - eps;
|
||||
double relative = (bound > 0)? - eps: eps;
|
||||
return x < bound * (1.0 + relative) - eps;
|
||||
}
|
||||
static bool above_bound_numeric(const double & x, const double & bound, const double & eps) {
|
||||
if (bound == 0) return x > eps;
|
||||
double relative = (bound > 0)? eps: - eps;
|
||||
return x > bound * (1.0 + relative) + eps;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename X> bool is_epsilon_small(const X & v, const double &eps) { return convert_struct<X, double>::is_epsilon_small(v, eps);}
|
||||
template <typename X> bool below_bound_numeric(const X & x, const X & bound, const double& eps) { return convert_struct<X, double>::below_bound_numeric(x, bound, eps);}
|
||||
template <typename X> bool above_bound_numeric(const X & x, const X & bound, const double& eps) { return convert_struct<X, double>::above_bound_numeric(x, bound, eps);}
|
||||
template <typename T> T floor(const numeric_pair<T> & r) {
|
||||
|
|
|
|||
|
|
@ -21,50 +21,11 @@ Revision History:
|
|||
#include "util/vector.h"
|
||||
#include "math/lp/permutation_matrix_def.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
template void lp::permutation_matrix<double, double>::apply_from_right(vector<double>&);
|
||||
template void lp::permutation_matrix<double, double>::init(unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::init(unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq>>::init(unsigned int);
|
||||
template bool lp::permutation_matrix<double, double>::is_identity() const;
|
||||
template void lp::permutation_matrix<double, double>::multiply_by_permutation_from_left(lp::permutation_matrix<double, double>&);
|
||||
template void lp::permutation_matrix<double, double>::multiply_by_permutation_reverse_from_left(lp::permutation_matrix<double, double>&);
|
||||
template void lp::permutation_matrix<double, double>::multiply_by_reverse_from_right(lp::permutation_matrix<double, double>&);
|
||||
template lp::permutation_matrix<double, double>::permutation_matrix(unsigned int, vector<unsigned int> const&);
|
||||
template void lp::permutation_matrix<double, double>::transpose_from_left(unsigned int, unsigned int);
|
||||
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::apply_from_right(vector<lp::mpq>&);
|
||||
template bool lp::permutation_matrix<lp::mpq, lp::mpq>::is_identity() const;
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::multiply_by_permutation_from_left(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::multiply_by_permutation_from_right(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::multiply_by_permutation_reverse_from_left(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::multiply_by_reverse_from_right(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template lp::permutation_matrix<lp::mpq, lp::mpq>::permutation_matrix(unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::transpose_from_left(unsigned int, unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::transpose_from_right(unsigned int, unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_from_right(vector<lp::mpq>&);
|
||||
template bool lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::is_identity() const;
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::multiply_by_permutation_from_left(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::multiply_by_permutation_from_right(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::multiply_by_permutation_reverse_from_left(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::multiply_by_reverse_from_right(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::permutation_matrix(unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::transpose_from_left(unsigned int, unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::transpose_from_right(unsigned int, unsigned int);
|
||||
template void lp::permutation_matrix<double, double>::apply_reverse_from_left<double>(lp::indexed_vector<double>&);
|
||||
template void lp::permutation_matrix<double, double>::apply_reverse_from_left_to_T(vector<double>&);
|
||||
template void lp::permutation_matrix<double, double>::apply_reverse_from_right_to_T(vector<double>&);
|
||||
template void lp::permutation_matrix<double, double>::transpose_from_right(unsigned int, unsigned int);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::apply_reverse_from_left<lp::mpq>(lp::indexed_vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::apply_reverse_from_left_to_T(vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::apply_reverse_from_right_to_T(vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_reverse_from_left<lp::mpq>(lp::indexed_vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_reverse_from_left_to_T(vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_reverse_from_right_to_T(vector<lp::mpq >&);
|
||||
template void lp::permutation_matrix<double, double>::multiply_by_permutation_from_right(lp::permutation_matrix<double, double>&);
|
||||
template lp::permutation_matrix<double, double>::permutation_matrix(unsigned int);
|
||||
template void lp::permutation_matrix<double, double>::apply_reverse_from_left_to_X(vector<double> &);
|
||||
template void lp::permutation_matrix< lp::mpq, lp::mpq>::apply_reverse_from_left_to_X(vector<lp::mpq> &);
|
||||
template void lp::permutation_matrix< lp::mpq, lp::numeric_pair< lp::mpq> >::apply_reverse_from_left_to_X(vector<lp::numeric_pair< lp::mpq>> &);
|
||||
template void lp::permutation_matrix<double, double>::apply_reverse_from_right_to_T(lp::indexed_vector<double>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::mpq>::apply_reverse_from_right_to_T(lp::indexed_vector<lp::mpq>&);
|
||||
template void lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::apply_reverse_from_right_to_T(lp::indexed_vector<lp::mpq>&);
|
||||
|
|
|
|||
|
|
@ -22,24 +22,19 @@ Revision History:
|
|||
#include <algorithm>
|
||||
#include "util/debug.h"
|
||||
#include <string>
|
||||
#include "math/lp/sparse_vector.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include "math/lp/matrix.h"
|
||||
#include "math/lp/tail_matrix.h"
|
||||
namespace lp {
|
||||
#ifdef Z3DEBUG
|
||||
inline bool is_even(int k) { return (k/2)*2 == k; }
|
||||
#endif
|
||||
|
||||
template <typename T, typename X>
|
||||
class permutation_matrix : public tail_matrix<T, X> {
|
||||
template <typename T, typename X>
|
||||
class permutation_matrix
|
||||
#ifdef Z3DEBUG
|
||||
: public matrix<T, X>
|
||||
#endif
|
||||
{
|
||||
vector<unsigned> m_permutation;
|
||||
vector<unsigned> m_rev;
|
||||
vector<unsigned> m_work_array;
|
||||
vector<T> m_T_buffer;
|
||||
vector<X> m_X_buffer;
|
||||
|
||||
|
||||
class ref {
|
||||
permutation_matrix & m_p;
|
||||
|
|
@ -64,42 +59,15 @@ class permutation_matrix : public tail_matrix<T, X> {
|
|||
// create a unit permutation of the given length
|
||||
void init(unsigned length);
|
||||
unsigned get_rev(unsigned i) { return m_rev[i]; }
|
||||
bool is_dense() const override { return false; }
|
||||
#ifdef Z3DEBUG
|
||||
permutation_matrix get_inverse() const {
|
||||
return permutation_matrix(size(), m_rev);
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
void print(std::ostream & out) const;
|
||||
#endif
|
||||
|
||||
ref operator[](unsigned i) { return ref(*this, i); }
|
||||
|
||||
unsigned operator[](unsigned i) const { return m_permutation[i]; }
|
||||
|
||||
void apply_from_left(vector<X> & w, lp_settings &) override;
|
||||
|
||||
void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) override;
|
||||
|
||||
void apply_from_right(vector<T> & w) override;
|
||||
|
||||
void apply_from_right(indexed_vector<T> & w) override;
|
||||
|
||||
template <typename L>
|
||||
void copy_aside(vector<L> & t, vector<unsigned> & tmp_index, indexed_vector<L> & w);
|
||||
|
||||
template <typename L>
|
||||
void clear_data(indexed_vector<L> & w);
|
||||
|
||||
template <typename L>
|
||||
void apply_reverse_from_left(indexed_vector<L> & w);
|
||||
|
||||
void apply_reverse_from_left_to_T(vector<T> & w);
|
||||
void apply_reverse_from_left_to_X(vector<X> & w);
|
||||
|
||||
void apply_reverse_from_right_to_T(vector<T> & w);
|
||||
void apply_reverse_from_right_to_T(indexed_vector<T> & w);
|
||||
void apply_reverse_from_right_to_X(vector<X> & w);
|
||||
|
||||
void set_val(unsigned i, unsigned pi) {
|
||||
lp_assert(i < size() && pi < size()); m_permutation[i] = pi; m_rev[pi] = i; }
|
||||
|
||||
|
|
@ -117,18 +85,6 @@ class permutation_matrix : public tail_matrix<T, X> {
|
|||
void set_number_of_rows(unsigned /*m*/) override { }
|
||||
void set_number_of_columns(unsigned /*n*/) override { }
|
||||
#endif
|
||||
void multiply_by_permutation_from_left(permutation_matrix<T, X> & p);
|
||||
|
||||
// this is multiplication in the matrix sense
|
||||
void multiply_by_permutation_from_right(permutation_matrix<T, X> & p);
|
||||
|
||||
void multiply_by_reverse_from_right(permutation_matrix<T, X> & q);
|
||||
|
||||
void multiply_by_permutation_reverse_from_left(permutation_matrix<T, X> & r);
|
||||
|
||||
void shrink_by_one_identity();
|
||||
|
||||
bool is_identity() const;
|
||||
|
||||
unsigned size() const { return static_cast<unsigned>(m_rev.size()); }
|
||||
|
||||
|
|
@ -136,8 +92,6 @@ class permutation_matrix : public tail_matrix<T, X> {
|
|||
unsigned old_size = m_permutation.size();
|
||||
m_permutation.resize(size);
|
||||
m_rev.resize(size);
|
||||
m_T_buffer.resize(size);
|
||||
m_X_buffer.resize(size);
|
||||
for (unsigned i = old_size; i < size; i++) {
|
||||
m_permutation[i] = m_rev[i] = i;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@ Revision History:
|
|||
#include "util/vector.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X> permutation_matrix<T, X>::permutation_matrix(unsigned length): m_permutation(length), m_rev(length), m_T_buffer(length), m_X_buffer(length) {
|
||||
template <typename T, typename X> permutation_matrix<T, X>::permutation_matrix(unsigned length): m_permutation(length), m_rev(length) {
|
||||
for (unsigned i = 0; i < length; i++) { // do not change the direction of the loop because of the vectorization bug in clang3.3
|
||||
m_permutation[i] = m_rev[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> permutation_matrix<T, X>::permutation_matrix(unsigned length, vector<unsigned> const & values): m_permutation(length), m_rev(length) , m_T_buffer(length), m_X_buffer(length) {
|
||||
template <typename T, typename X> permutation_matrix<T, X>::permutation_matrix(unsigned length, vector<unsigned> const & values): m_permutation(length), m_rev(length) {
|
||||
for (unsigned i = 0; i < length; i++) {
|
||||
set_val(i, values[i]);
|
||||
}
|
||||
|
|
@ -37,8 +37,6 @@ template <typename T, typename X> permutation_matrix<T, X>::permutation_matrix(u
|
|||
template <typename T, typename X> void permutation_matrix<T, X>::init(unsigned length) {
|
||||
m_permutation.resize(length);
|
||||
m_rev.resize(length);
|
||||
m_T_buffer.resize(length);
|
||||
m_X_buffer.resize(length);
|
||||
for (unsigned i = 0; i < length; i++) {
|
||||
m_permutation[i] = m_rev[i] = i;
|
||||
}
|
||||
|
|
@ -59,213 +57,6 @@ template <typename T, typename X> void permutation_matrix<T, X>::print(std::ostr
|
|||
}
|
||||
#endif
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_from_left(vector<X> & w, lp_settings & ) {
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<L, X> deb(*this);
|
||||
// L * deb_w = clone_vector<L>(w, row_count());
|
||||
// deb.apply_from_left(deb_w);
|
||||
#endif
|
||||
lp_assert(m_X_buffer.size() == w.size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
m_X_buffer[i] = w[m_permutation[i]];
|
||||
}
|
||||
i = size();
|
||||
while (i-- > 0) {
|
||||
w[i] = m_X_buffer[i];
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<L>(deb_w, w, row_count()));
|
||||
// delete [] deb_w;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_from_left_to_T(indexed_vector<T> & w, lp_settings & ) {
|
||||
vector<T> t(w.m_index.size());
|
||||
vector<unsigned> tmp_index(w.m_index.size());
|
||||
copy_aside(t, tmp_index, w); // todo: is it too much copying
|
||||
clear_data(w);
|
||||
// set the new values
|
||||
for (unsigned i = static_cast<unsigned>(t.size()); i > 0;) {
|
||||
i--;
|
||||
unsigned j = m_rev[tmp_index[i]];
|
||||
w[j] = t[i];
|
||||
w.m_index[i] = j;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::apply_from_right(vector<T> & w) {
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T, X> deb(*this);
|
||||
// T * deb_w = clone_vector<T>(w, row_count());
|
||||
// deb.apply_from_right(deb_w);
|
||||
#endif
|
||||
lp_assert(m_T_buffer.size() == w.size());
|
||||
for (unsigned i = 0; i < size(); i++) {
|
||||
m_T_buffer[i] = w[m_rev[i]];
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < size(); i++) {
|
||||
w[i] = m_T_buffer[i];
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<T>(deb_w, w, row_count()));
|
||||
// delete [] deb_w;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::apply_from_right(indexed_vector<T> & w) {
|
||||
#ifdef Z3DEBUG
|
||||
vector<T> wcopy(w.m_data);
|
||||
apply_from_right(wcopy);
|
||||
#endif
|
||||
vector<T> buffer(w.m_index.size());
|
||||
vector<unsigned> index_copy(w.m_index);
|
||||
for (unsigned i = 0; i < w.m_index.size(); i++) {
|
||||
buffer[i] = w.m_data[w.m_index[i]];
|
||||
}
|
||||
w.clear();
|
||||
|
||||
for (unsigned i = 0; i < index_copy.size(); i++) {
|
||||
unsigned j = index_copy[i];
|
||||
unsigned pj = m_permutation[j];
|
||||
w.set_value(buffer[i], pj);
|
||||
}
|
||||
lp_assert(w.is_OK());
|
||||
#ifdef Z3DEBUG
|
||||
lp_assert(vectors_are_equal(wcopy, w.m_data));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> template <typename L>
|
||||
void permutation_matrix<T, X>::copy_aside(vector<L> & t, vector<unsigned> & tmp_index, indexed_vector<L> & w) {
|
||||
for (unsigned i = static_cast<unsigned>(t.size()); i > 0;) {
|
||||
i--;
|
||||
unsigned j = w.m_index[i];
|
||||
t[i] = w[j]; // copy aside all non-zeroes
|
||||
tmp_index[i] = j; // and the indices too
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> template <typename L>
|
||||
void permutation_matrix<T, X>::clear_data(indexed_vector<L> & w) {
|
||||
// clear old non-zeroes
|
||||
for (unsigned i = static_cast<unsigned>(w.m_index.size()); i > 0;) {
|
||||
i--;
|
||||
unsigned j = w.m_index[i];
|
||||
w[j] = zero_of_type<L>();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X>template <typename L>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_left(indexed_vector<L> & w) {
|
||||
// the result will be w = p(-1) * w
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<L, X> deb(get_reverse());
|
||||
// L * deb_w = clone_vector<L>(w.m_data, row_count());
|
||||
// deb.apply_from_left(deb_w);
|
||||
#endif
|
||||
vector<L> t(w.m_index.size());
|
||||
vector<unsigned> tmp_index(w.m_index.size());
|
||||
|
||||
copy_aside(t, tmp_index, w);
|
||||
clear_data(w);
|
||||
|
||||
// set the new values
|
||||
for (unsigned i = static_cast<unsigned>(t.size()); i > 0;) {
|
||||
i--;
|
||||
unsigned j = m_permutation[tmp_index[i]];
|
||||
w[j] = t[i];
|
||||
w.m_index[i] = j;
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<L>(deb_w, w.m_data, row_count()));
|
||||
// delete [] deb_w;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_left_to_T(vector<T> & w) {
|
||||
// the result will be w = p(-1) * w
|
||||
lp_assert(m_T_buffer.size() == w.size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
m_T_buffer[m_permutation[i]] = w[i];
|
||||
}
|
||||
i = size();
|
||||
while (i-- > 0) {
|
||||
w[i] = m_T_buffer[i];
|
||||
}
|
||||
}
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_left_to_X(vector<X> & w) {
|
||||
// the result will be w = p(-1) * w
|
||||
lp_assert(m_X_buffer.size() == w.size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
m_X_buffer[m_permutation[i]] = w[i];
|
||||
}
|
||||
i = size();
|
||||
while (i-- > 0) {
|
||||
w[i] = m_X_buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_right_to_T(vector<T> & w) {
|
||||
// the result will be w = w * p(-1)
|
||||
lp_assert(m_T_buffer.size() == w.size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
m_T_buffer[i] = w[m_permutation[i]];
|
||||
}
|
||||
i = size();
|
||||
while (i-- > 0) {
|
||||
w[i] = m_T_buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_right_to_T(indexed_vector<T> & w) {
|
||||
// the result will be w = w * p(-1)
|
||||
#ifdef Z3DEBUG
|
||||
// vector<T> wcopy(w.m_data);
|
||||
// apply_reverse_from_right_to_T(wcopy);
|
||||
#endif
|
||||
lp_assert(w.is_OK());
|
||||
vector<T> tmp;
|
||||
vector<unsigned> tmp_index(w.m_index);
|
||||
for (auto i : w.m_index) {
|
||||
tmp.push_back(w[i]);
|
||||
}
|
||||
w.clear();
|
||||
|
||||
for (unsigned k = 0; k < tmp_index.size(); k++) {
|
||||
unsigned j = tmp_index[k];
|
||||
w.set_value(tmp[k], m_rev[j]);
|
||||
}
|
||||
|
||||
// lp_assert(w.is_OK());
|
||||
// lp_assert(vectors_are_equal(w.m_data, wcopy));
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X>
|
||||
void permutation_matrix<T, X>::apply_reverse_from_right_to_X(vector<X> & w) {
|
||||
// the result will be w = w * p(-1)
|
||||
lp_assert(m_X_buffer.size() == w.size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
m_X_buffer[i] = w[m_permutation[i]];
|
||||
}
|
||||
i = size();
|
||||
while (i-- > 0) {
|
||||
w[i] = m_X_buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::transpose_from_left(unsigned i, unsigned j) {
|
||||
// the result will be this = (i,j)*this
|
||||
|
|
@ -285,55 +76,5 @@ template <typename T, typename X> void permutation_matrix<T, X>::transpose_from_
|
|||
set_val(j, pi);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::multiply_by_permutation_from_left(permutation_matrix<T, X> & p) {
|
||||
m_work_array = m_permutation;
|
||||
lp_assert(p.size() == size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
set_val(i, m_work_array[p[i]]); // we have m(P)*m(Q) = m(QP), where m is the matrix of the permutation
|
||||
}
|
||||
}
|
||||
|
||||
// this is multiplication in the matrix sense
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::multiply_by_permutation_from_right(permutation_matrix<T, X> & p) {
|
||||
m_work_array = m_permutation;
|
||||
lp_assert(p.size() == size());
|
||||
unsigned i = size();
|
||||
while (i-- > 0)
|
||||
set_val(i, p[m_work_array[i]]); // we have m(P)*m(Q) = m(QP), where m is the matrix of the permutation
|
||||
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::multiply_by_reverse_from_right(permutation_matrix<T, X> & q){ // todo : condensed permutations ?
|
||||
lp_assert(q.size() == size());
|
||||
m_work_array = m_permutation;
|
||||
// the result is this = this*q(-1)
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
set_val(i, q.m_rev[m_work_array[i]]); // we have m(P)*m(Q) = m(QP), where m is the matrix of the permutation
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void permutation_matrix<T, X>::multiply_by_permutation_reverse_from_left(permutation_matrix<T, X> & r){ // todo : condensed permutations?
|
||||
// the result is this = r(-1)*this
|
||||
m_work_array = m_permutation;
|
||||
// the result is this = this*q(-1)
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
set_val(i, m_work_array[r.m_rev[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename X> bool permutation_matrix<T, X>::is_identity() const {
|
||||
unsigned i = size();
|
||||
while (i-- > 0) {
|
||||
if (m_permutation[i] != i) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <memory>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/row_eta_matrix_def.h"
|
||||
#include "math/lp/lu.h"
|
||||
namespace lp {
|
||||
template void row_eta_matrix<double, double>::conjugate_by_permutation(permutation_matrix<double, double>&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::conjugate_by_permutation(permutation_matrix<mpq, numeric_pair<mpq> >&);
|
||||
template void row_eta_matrix<mpq, mpq>::conjugate_by_permutation(permutation_matrix<mpq, mpq>&);
|
||||
#ifdef Z3DEBUG
|
||||
template mpq row_eta_matrix<mpq, mpq>::get_elem(unsigned int, unsigned int) const;
|
||||
template mpq row_eta_matrix<mpq, numeric_pair<mpq> >::get_elem(unsigned int, unsigned int) const;
|
||||
template double row_eta_matrix<double, double>::get_elem(unsigned int, unsigned int) const;
|
||||
#endif
|
||||
template void row_eta_matrix<mpq, mpq>::apply_from_left(vector<mpq>&, lp_settings&);
|
||||
template void row_eta_matrix<mpq, mpq>::apply_from_right(vector<mpq>&);
|
||||
template void row_eta_matrix<mpq, mpq>::apply_from_right(indexed_vector<mpq>&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::apply_from_left(vector<numeric_pair<mpq>>&, lp_settings&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::apply_from_right(vector<mpq>&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::apply_from_right(indexed_vector<mpq>&);
|
||||
template void row_eta_matrix<double, double>::apply_from_left(vector<double>&, lp_settings&);
|
||||
template void row_eta_matrix<double, double>::apply_from_right(vector<double>&);
|
||||
template void row_eta_matrix<double, double>::apply_from_right(indexed_vector<double>&);
|
||||
template void row_eta_matrix<mpq, mpq>::apply_from_left_to_T(indexed_vector<mpq>&, lp_settings&);
|
||||
template void row_eta_matrix<mpq, mpq>::apply_from_left_local_to_T(indexed_vector<mpq>&, lp_settings&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::apply_from_left_to_T(indexed_vector<mpq>&, lp_settings&);
|
||||
template void row_eta_matrix<mpq, numeric_pair<mpq> >::apply_from_left_local_to_T(indexed_vector<mpq>&, lp_settings&);
|
||||
template void row_eta_matrix<double, double>::apply_from_left_to_T(indexed_vector<double>&, lp_settings&);
|
||||
template void row_eta_matrix<double, double>::apply_from_left_local_to_T(indexed_vector<double>&, lp_settings&);
|
||||
}
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "util/debug.h"
|
||||
#include <string>
|
||||
#include "math/lp/sparse_vector.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
namespace lp {
|
||||
// This is the sum of a unit matrix and a lower triangular matrix
|
||||
// with non-zero elements only in one row
|
||||
template <typename T, typename X>
|
||||
class row_eta_matrix
|
||||
: public tail_matrix<T, X> {
|
||||
#ifdef Z3DEBUG
|
||||
unsigned m_dimension;
|
||||
#endif
|
||||
unsigned m_row_start;
|
||||
unsigned m_row;
|
||||
sparse_vector<T> m_row_vector;
|
||||
public:
|
||||
#ifdef Z3DEBUG
|
||||
row_eta_matrix(unsigned row_start, unsigned row, unsigned dim):
|
||||
#else
|
||||
row_eta_matrix(unsigned row_start, unsigned row):
|
||||
#endif
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
m_dimension(dim),
|
||||
#endif
|
||||
m_row_start(row_start), m_row(row) {
|
||||
}
|
||||
|
||||
bool is_dense() const override { return false; }
|
||||
|
||||
void print(std::ostream & out) {
|
||||
print_matrix(*this, out);
|
||||
}
|
||||
|
||||
const T & get_diagonal_element() const {
|
||||
return m_row_vector.m_data[m_row];
|
||||
}
|
||||
|
||||
void apply_from_left(vector<X> & w, lp_settings &) override;
|
||||
|
||||
void apply_from_left_local_to_T(indexed_vector<T> & w, lp_settings & settings);
|
||||
void apply_from_left_local_to_X(indexed_vector<X> & w, lp_settings & settings);
|
||||
|
||||
void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) override {
|
||||
apply_from_left_local_to_T(w, settings);
|
||||
}
|
||||
|
||||
void push_back(unsigned row_index, T val ) {
|
||||
lp_assert(row_index != m_row);
|
||||
m_row_vector.push_back(row_index, val);
|
||||
}
|
||||
|
||||
void apply_from_right(vector<T> & w) override;
|
||||
void apply_from_right(indexed_vector<T> & w) override;
|
||||
|
||||
void conjugate_by_permutation(permutation_matrix<T, X> & p);
|
||||
#ifdef Z3DEBUG
|
||||
T get_elem(unsigned row, unsigned col) const override;
|
||||
unsigned row_count() const override { return m_dimension; }
|
||||
unsigned column_count() const override { return m_dimension; }
|
||||
void set_number_of_rows(unsigned m) override { m_dimension = m; }
|
||||
void set_number_of_columns(unsigned n) override { m_dimension = n; }
|
||||
#endif
|
||||
}; // end of row_eta_matrix
|
||||
}
|
||||
|
|
@ -1,188 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/row_eta_matrix.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::apply_from_left(vector<X> & w, lp_settings &) {
|
||||
// #ifdef Z3DEBUG
|
||||
// dense_matrix<T> deb(*this);
|
||||
// auto clone_w = clone_vector<T>(w, m_dimension);
|
||||
// deb.apply_from_left(clone_w, settings);
|
||||
// #endif
|
||||
|
||||
auto & w_at_row = w[m_row];
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
w_at_row += w[it.first] * it.second;
|
||||
}
|
||||
// w[m_row] = w_at_row;
|
||||
// #ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<T>(clone_w, w, m_dimension));
|
||||
// delete [] clone_w;
|
||||
// #endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::apply_from_left_local_to_T(indexed_vector<T> & w, lp_settings & settings) {
|
||||
auto w_at_row = w[m_row];
|
||||
bool was_zero_at_m_row = is_zero(w_at_row);
|
||||
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
w_at_row += w[it.first] * it.second;
|
||||
}
|
||||
|
||||
if (!settings.abs_val_is_smaller_than_drop_tolerance(w_at_row)){
|
||||
if (was_zero_at_m_row) {
|
||||
w.m_index.push_back(m_row);
|
||||
}
|
||||
w[m_row] = w_at_row;
|
||||
} else if (!was_zero_at_m_row){
|
||||
w[m_row] = zero_of_type<T>();
|
||||
auto it = std::find(w.m_index.begin(), w.m_index.end(), m_row);
|
||||
w.m_index.erase(it);
|
||||
}
|
||||
// TBD: lp_assert(check_vector_for_small_values(w, settings));
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::apply_from_left_local_to_X(indexed_vector<X> & w, lp_settings & settings) {
|
||||
auto w_at_row = w[m_row];
|
||||
bool was_zero_at_m_row = is_zero(w_at_row);
|
||||
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
w_at_row += w[it.first] * it.second;
|
||||
}
|
||||
|
||||
if (!settings.abs_val_is_smaller_than_drop_tolerance(w_at_row)){
|
||||
if (was_zero_at_m_row) {
|
||||
w.m_index.push_back(m_row);
|
||||
}
|
||||
w[m_row] = w_at_row;
|
||||
} else if (!was_zero_at_m_row){
|
||||
w[m_row] = zero_of_type<X>();
|
||||
auto it = std::find(w.m_index.begin(), w.m_index.end(), m_row);
|
||||
w.m_index.erase(it);
|
||||
}
|
||||
// TBD: does not compile lp_assert(check_vector_for_small_values(w, settings));
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::apply_from_right(vector<T> & w) {
|
||||
const T & w_row = w[m_row];
|
||||
if (numeric_traits<T>::is_zero(w_row)) return;
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T> deb(*this);
|
||||
// auto clone_w = clone_vector<T>(w, m_dimension);
|
||||
// deb.apply_from_right(clone_w);
|
||||
#endif
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
w[it.first] += w_row * it.second;
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal<T>(clone_w, w, m_dimension));
|
||||
// delete clone_w;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::apply_from_right(indexed_vector<T> & w) {
|
||||
lp_assert(w.is_OK());
|
||||
const T & w_row = w[m_row];
|
||||
if (numeric_traits<T>::is_zero(w_row)) return;
|
||||
#ifdef Z3DEBUG
|
||||
// vector<T> wcopy(w.m_data);
|
||||
// apply_from_right(wcopy);
|
||||
#endif
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
unsigned j = it.first;
|
||||
bool was_zero = numeric_traits<T>::is_zero(w[j]);
|
||||
const T & v = w[j] += w_row * it.second;
|
||||
|
||||
if (was_zero) {
|
||||
if (!numeric_traits<T>::is_zero(v))
|
||||
w.m_index.push_back(j);
|
||||
} else {
|
||||
if (numeric_traits<T>::is_zero(v))
|
||||
w.erase_from_index(j);
|
||||
}
|
||||
}
|
||||
} else { // the non precise version
|
||||
const double drop_eps = 1e-14;
|
||||
for (auto & it : m_row_vector.m_data) {
|
||||
unsigned j = it.first;
|
||||
bool was_zero = numeric_traits<T>::is_zero(w[j]);
|
||||
T & v = w[j] += w_row * it.second;
|
||||
|
||||
if (was_zero) {
|
||||
if (!lp_settings::is_eps_small_general(v, drop_eps))
|
||||
w.m_index.push_back(j);
|
||||
else
|
||||
v = zero_of_type<T>();
|
||||
} else {
|
||||
if (lp_settings::is_eps_small_general(v, drop_eps)) {
|
||||
w.erase_from_index(j);
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vectors_are_equal(wcopy, w.m_data));
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
void row_eta_matrix<T, X>::conjugate_by_permutation(permutation_matrix<T, X> & p) {
|
||||
// this = p * this * p(-1)
|
||||
#ifdef Z3DEBUG
|
||||
// auto rev = p.get_reverse();
|
||||
// auto deb = ((*this) * rev);
|
||||
// deb = p * deb;
|
||||
#endif
|
||||
m_row = p.apply_reverse(m_row);
|
||||
// copy aside the column indices
|
||||
vector<unsigned> columns;
|
||||
for (auto & it : m_row_vector.m_data)
|
||||
columns.push_back(it.first);
|
||||
for (unsigned i = static_cast<unsigned>(columns.size()); i-- > 0;)
|
||||
m_row_vector.m_data[i].first = p.get_rev(columns[i]);
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(deb == *this);
|
||||
#endif
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
template <typename T, typename X>
|
||||
T row_eta_matrix<T, X>::get_elem(unsigned row, unsigned col) const {
|
||||
if (row == m_row){
|
||||
if (col == row) {
|
||||
return numeric_traits<T>::one();
|
||||
}
|
||||
return m_row_vector[col];
|
||||
}
|
||||
|
||||
return col == row ? numeric_traits<T>::one() : numeric_traits<T>::zero();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include "math/lp/scaler_def.h"
|
||||
template bool lp::scaler<double, double>::scale();
|
||||
template bool lp::scaler<lp::mpq, lp::mpq>::scale();
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
#include <stdio.h> /* printf, fopen */
|
||||
#include <stdlib.h> /* exit, EXIT_FAILURE */
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/static_matrix.h"
|
||||
namespace lp {
|
||||
// for scaling an LP
|
||||
template <typename T, typename X>
|
||||
class scaler {
|
||||
vector<X> & m_b; // right side
|
||||
static_matrix<T, X> &m_A; // the constraint matrix
|
||||
const T & m_scaling_minimum;
|
||||
const T & m_scaling_maximum;
|
||||
vector<T>& m_column_scale;
|
||||
lp_settings & m_settings;
|
||||
public:
|
||||
// constructor
|
||||
scaler(vector<X> & b, static_matrix<T, X> &A, const T & scaling_minimum, const T & scaling_maximum, vector<T> & column_scale,
|
||||
lp_settings & settings):
|
||||
m_b(b),
|
||||
m_A(A),
|
||||
m_scaling_minimum(scaling_minimum),
|
||||
m_scaling_maximum(scaling_maximum),
|
||||
m_column_scale(column_scale),
|
||||
m_settings(settings) {
|
||||
lp_assert(m_column_scale.size() == 0);
|
||||
m_column_scale.resize(m_A.column_count(), numeric_traits<T>::one());
|
||||
}
|
||||
|
||||
T right_side_balance();
|
||||
|
||||
T get_balance() { return m_A.get_balance(); }
|
||||
|
||||
T A_min() const;
|
||||
|
||||
T A_max() const;
|
||||
|
||||
T get_A_ratio() const;
|
||||
|
||||
T get_max_ratio_on_rows() const;
|
||||
|
||||
T get_max_ratio_on_columns() const;
|
||||
|
||||
void scale_rows_with_geometric_mean();
|
||||
|
||||
void scale_columns_with_geometric_mean();
|
||||
|
||||
void scale_once_for_ratio();
|
||||
|
||||
bool scale_with_ratio();
|
||||
|
||||
void bring_row_maximums_to_one();
|
||||
|
||||
void bring_column_maximums_to_one();
|
||||
|
||||
void bring_rows_and_columns_maximums_to_one();
|
||||
|
||||
bool scale_with_log_balance();
|
||||
// Returns true if and only if the scaling was successful.
|
||||
// It is the caller responsibility to restore the matrix
|
||||
bool scale();
|
||||
|
||||
void scale_rows();
|
||||
|
||||
void scale_row(unsigned i);
|
||||
|
||||
void scale_column(unsigned i);
|
||||
|
||||
void scale_columns();
|
||||
};
|
||||
}
|
||||
|
|
@ -1,270 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include "math/lp/scaler.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
namespace lp {
|
||||
// for scaling an LP
|
||||
template <typename T, typename X> T scaler<T, X>::right_side_balance() {
|
||||
T ret = zero_of_type<T>();
|
||||
unsigned i = m_A.row_count();
|
||||
while (i--) {
|
||||
T rs = abs(convert_struct<T, X>::convert(m_b[i]));
|
||||
if (!is_zero<T>(rs)) {
|
||||
numeric_traits<T>::log(rs);
|
||||
ret += rs * rs;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T scaler<T, X>::A_min() const {
|
||||
T min = zero_of_type<T>();
|
||||
for (unsigned i = 0; i < m_A.row_count(); i++) {
|
||||
T t = m_A.get_min_abs_in_row(i);
|
||||
min = i == 0 ? t : std::min(t, min);
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T scaler<T, X>::A_max() const {
|
||||
T max = zero_of_type<T>();
|
||||
for (unsigned i = 0; i < m_A.row_count(); i++) {
|
||||
T t = m_A.get_max_abs_in_row(i);
|
||||
max = i == 0? t : std::max(t, max);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T scaler<T, X>::get_A_ratio() const {
|
||||
T min = A_min();
|
||||
T max = A_max();
|
||||
lp_assert(!m_settings.abs_val_is_smaller_than_zero_tolerance(min));
|
||||
T ratio = max / min;
|
||||
return ratio;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T scaler<T, X>::get_max_ratio_on_rows() const {
|
||||
T ret = T(1);
|
||||
unsigned i = m_A.row_count();
|
||||
while (i--) {
|
||||
T den = m_A.get_min_abs_in_row(i);
|
||||
lp_assert(!m_settings.abs_val_is_smaller_than_zero_tolerance(den));
|
||||
T t = m_A.get_max_abs_in_row(i)/ den;
|
||||
if (t > ret)
|
||||
ret = t;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> T scaler<T, X>::get_max_ratio_on_columns() const {
|
||||
T ret = T(1);
|
||||
unsigned i = m_A.column_count();
|
||||
while (i--) {
|
||||
T den = m_A.get_min_abs_in_column(i);
|
||||
if (m_settings.abs_val_is_smaller_than_zero_tolerance(den))
|
||||
continue; // got a zero column
|
||||
T t = m_A.get_max_abs_in_column(i)/den;
|
||||
if (t > ret)
|
||||
ret = t;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_rows_with_geometric_mean() {
|
||||
unsigned i = m_A.row_count();
|
||||
while (i--) {
|
||||
T max = m_A.get_max_abs_in_row(i);
|
||||
T min = m_A.get_min_abs_in_row(i);
|
||||
lp_assert(max > zero_of_type<T>() && min > zero_of_type<T>());
|
||||
if (is_zero(max) || is_zero(min))
|
||||
continue;
|
||||
T gm = T(sqrt(numeric_traits<T>::get_double(max*min)));
|
||||
if (m_settings.is_eps_small_general(gm, 0.01)) {
|
||||
continue;
|
||||
}
|
||||
m_A.multiply_row(i, one_of_type<T>() / gm);
|
||||
m_b[i] /= gm;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_columns_with_geometric_mean() {
|
||||
unsigned i = m_A.column_count();
|
||||
while (i--) {
|
||||
T max = m_A.get_max_abs_in_column(i);
|
||||
T min = m_A.get_min_abs_in_column(i);
|
||||
T den = T(sqrt(numeric_traits<T>::get_double(max*min)));
|
||||
if (m_settings.is_eps_small_general(den, 0.01))
|
||||
continue; // got a zero column
|
||||
T gm = T(1)/ den;
|
||||
T cs = m_column_scale[i] * gm;
|
||||
if (m_settings.is_eps_small_general(cs, 0.1))
|
||||
continue;
|
||||
m_A.multiply_column(i, gm);
|
||||
m_column_scale[i] = cs;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_once_for_ratio() {
|
||||
T max_ratio_on_rows = get_max_ratio_on_rows();
|
||||
T max_ratio_on_columns = get_max_ratio_on_columns();
|
||||
bool scale_rows_first = max_ratio_on_rows > max_ratio_on_columns;
|
||||
// if max_ratio_on_columns is the largest then the rows are in worse shape than columns
|
||||
if (scale_rows_first) {
|
||||
scale_rows_with_geometric_mean();
|
||||
scale_columns_with_geometric_mean();
|
||||
} else {
|
||||
scale_columns_with_geometric_mean();
|
||||
scale_rows_with_geometric_mean();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool scaler<T, X>::scale_with_ratio() {
|
||||
T ratio = get_A_ratio();
|
||||
// The ratio is greater than or equal to one. We would like to diminish it and bring it as close to 1 as possible
|
||||
unsigned reps = m_settings.reps_in_scaler;
|
||||
do {
|
||||
scale_once_for_ratio();
|
||||
T new_r = get_A_ratio();
|
||||
if (new_r >= T(0.9) * ratio)
|
||||
break;
|
||||
} while (reps--);
|
||||
|
||||
bring_rows_and_columns_maximums_to_one();
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::bring_row_maximums_to_one() {
|
||||
unsigned i = m_A.row_count();
|
||||
while (i--) {
|
||||
T t = m_A.get_max_abs_in_row(i);
|
||||
if (m_settings.abs_val_is_smaller_than_zero_tolerance(t)) continue;
|
||||
m_A.multiply_row(i, one_of_type<T>() / t);
|
||||
m_b[i] /= t;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::bring_column_maximums_to_one() {
|
||||
unsigned i = m_A.column_count();
|
||||
while (i--) {
|
||||
T max = m_A.get_max_abs_in_column(i);
|
||||
if (m_settings.abs_val_is_smaller_than_zero_tolerance(max)) continue;
|
||||
T t = T(1) / max;
|
||||
m_A.multiply_column(i, t);
|
||||
m_column_scale[i] *= t;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::bring_rows_and_columns_maximums_to_one() {
|
||||
if (get_max_ratio_on_rows() > get_max_ratio_on_columns()) {
|
||||
bring_row_maximums_to_one();
|
||||
bring_column_maximums_to_one();
|
||||
} else {
|
||||
bring_column_maximums_to_one();
|
||||
bring_row_maximums_to_one();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool scaler<T, X>::scale_with_log_balance() {
|
||||
T balance = get_balance();
|
||||
T balance_before_scaling = balance;
|
||||
// todo : analyze the scale order : rows-columns, or columns-rows. Iterate if needed
|
||||
for (int i = 0; i < 10; i++) {
|
||||
scale_rows();
|
||||
scale_columns();
|
||||
T nb = get_balance();
|
||||
if (nb < T(0.9) * balance) {
|
||||
balance = nb;
|
||||
} else {
|
||||
balance = nb;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return balance <= balance_before_scaling;
|
||||
}
|
||||
// Returns true if and only if the scaling was successful.
|
||||
// It is the caller responsibility to restore the matrix
|
||||
template <typename T, typename X> bool scaler<T, X>::scale() {
|
||||
if (numeric_traits<T>::precise()) return true;
|
||||
if (m_settings.scale_with_ratio)
|
||||
return scale_with_ratio();
|
||||
return scale_with_log_balance();
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_rows() {
|
||||
for (unsigned i = 0; i < m_A.row_count(); i++)
|
||||
scale_row(i);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_row(unsigned i) {
|
||||
T row_max = std::max(m_A.get_max_abs_in_row(i), abs(convert_struct<T, X>::convert(m_b[i])));
|
||||
T alpha = numeric_traits<T>::one();
|
||||
if (numeric_traits<T>::is_zero(row_max)) {
|
||||
return;
|
||||
}
|
||||
if (row_max < m_scaling_minimum) {
|
||||
do {
|
||||
alpha *= T(2);
|
||||
row_max *= T(2);
|
||||
} while (row_max < m_scaling_minimum);
|
||||
m_A.multiply_row(i, alpha);
|
||||
m_b[i] *= alpha;
|
||||
} else if (row_max > m_scaling_maximum) {
|
||||
do {
|
||||
alpha /= T(2);
|
||||
row_max /= T(2);
|
||||
} while (row_max > m_scaling_maximum);
|
||||
m_A.multiply_row(i, alpha);
|
||||
m_b[i] *= alpha;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_column(unsigned i) {
|
||||
T column_max = m_A.get_max_abs_in_column(i);
|
||||
T alpha = numeric_traits<T>::one();
|
||||
|
||||
if (numeric_traits<T>::is_zero(column_max)){
|
||||
return; // the column has zeros only
|
||||
}
|
||||
if (column_max < m_scaling_minimum) {
|
||||
do {
|
||||
alpha *= T(2);
|
||||
column_max *= T(2);
|
||||
} while (column_max < m_scaling_minimum);
|
||||
} else if (column_max > m_scaling_maximum) {
|
||||
do {
|
||||
alpha /= T(2);
|
||||
column_max /= T(2);
|
||||
} while (column_max > m_scaling_maximum);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
m_A.multiply_column(i, alpha);
|
||||
m_column_scale[i] = alpha;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void scaler<T, X>::scale_columns() {
|
||||
for (unsigned i = 0; i < m_A.column_count(); i++) {
|
||||
scale_column(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include <utility>
|
||||
#include "util/debug.h"
|
||||
#include "math/lp/lp_utils.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
namespace lp {
|
||||
|
||||
template <typename T>
|
||||
class sparse_vector {
|
||||
public:
|
||||
vector<std::pair<unsigned, T>> m_data;
|
||||
void push_back(unsigned index, T val) {
|
||||
m_data.push_back(std::make_pair(index, val));
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
T operator[] (unsigned i) const {
|
||||
for (auto &t : m_data) {
|
||||
if (t.first == i) return t.second;
|
||||
}
|
||||
return numeric_traits<T>::zero();
|
||||
}
|
||||
#endif
|
||||
void divide(T const & a) {
|
||||
lp_assert(!lp_settings::is_eps_small_general(a, 1e-12));
|
||||
for (auto & t : m_data) { t.second /= a; }
|
||||
}
|
||||
|
||||
unsigned size() const {
|
||||
return m_data.size();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <memory>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/square_dense_submatrix_def.h"
|
||||
template void lp::square_dense_submatrix<double, double>::init(lp::square_sparse_matrix<double, double>*, unsigned int);
|
||||
template lp::square_dense_submatrix<double, double>::square_dense_submatrix(lp::square_sparse_matrix<double, double>*, unsigned int);
|
||||
template void lp::square_dense_submatrix<double, double>::update_parent_matrix(lp::lp_settings&);
|
||||
template bool lp::square_dense_submatrix<double, double>::is_L_matrix() const;
|
||||
template void lp::square_dense_submatrix<double, double>::conjugate_by_permutation(lp::permutation_matrix<double, double>&);
|
||||
template int lp::square_dense_submatrix<double, double>::find_pivot_column_in_row(unsigned int) const;
|
||||
template void lp::square_dense_submatrix<double, double>::pivot(unsigned int, lp::lp_settings&);
|
||||
template lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::square_dense_submatrix(lp::square_sparse_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >*, unsigned int);
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::update_parent_matrix(lp::lp_settings&);
|
||||
template bool lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::is_L_matrix() const;
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::conjugate_by_permutation(lp::permutation_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >&);
|
||||
template int lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::find_pivot_column_in_row(unsigned int) const;
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::numeric_pair<lp::mpq> >::pivot(unsigned int, lp::lp_settings&);
|
||||
#ifdef Z3DEBUG
|
||||
template double lp::square_dense_submatrix<double, double>::get_elem(unsigned int, unsigned int) const;
|
||||
#endif
|
||||
template void lp::square_dense_submatrix<double, double>::apply_from_right(vector<double>&);
|
||||
|
||||
template void lp::square_dense_submatrix<double, double>::apply_from_left_local<double>(lp::indexed_vector<double>&, lp::lp_settings&);
|
||||
template void lp::square_dense_submatrix<double, double>::apply_from_left_to_vector<double>(vector<double>&);
|
||||
template lp::square_dense_submatrix<lp::mpq, lp::mpq>::square_dense_submatrix(lp::square_sparse_matrix<lp::mpq, lp::mpq>*, unsigned int);
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::mpq>::update_parent_matrix(lp::lp_settings&);
|
||||
template bool lp::square_dense_submatrix<lp::mpq, lp::mpq>::is_L_matrix() const;
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::mpq>::conjugate_by_permutation(lp::permutation_matrix<lp::mpq, lp::mpq>&);
|
||||
template int lp::square_dense_submatrix<lp::mpq, lp::mpq>::find_pivot_column_in_row(unsigned int) const;
|
||||
template void lp::square_dense_submatrix<lp::mpq, lp::mpq>::pivot(unsigned int, lp::lp_settings&);
|
||||
|
|
@ -1,225 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
#include <unordered_map>
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include "math/lp/indexed_value.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include <functional>
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include "math/lp/eta_matrix.h"
|
||||
#include "math/lp/binary_heap_upair_queue.h"
|
||||
#include "math/lp/square_sparse_matrix.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
class square_dense_submatrix : public tail_matrix<T, X> {
|
||||
// the submatrix uses the permutations of the parent matrix to access the elements
|
||||
struct ref {
|
||||
unsigned m_i_offset;
|
||||
square_dense_submatrix & m_s;
|
||||
ref(unsigned i, square_dense_submatrix & s) :
|
||||
m_i_offset((i - s.m_index_start) * s.m_dim), m_s(s){}
|
||||
T & operator[] (unsigned j) {
|
||||
lp_assert(j >= m_s.m_index_start);
|
||||
return m_s.m_v[m_i_offset + m_s.adjust_column(j) - m_s.m_index_start];
|
||||
}
|
||||
const T & operator[] (unsigned j) const {
|
||||
lp_assert(j >= m_s.m_index_start);
|
||||
return m_s.m_v[m_i_offset + m_s.adjust_column(j) - m_s.m_index_start];
|
||||
}
|
||||
};
|
||||
public:
|
||||
unsigned m_index_start;
|
||||
unsigned m_dim;
|
||||
vector<T> m_v;
|
||||
square_sparse_matrix<T, X> * m_parent;
|
||||
permutation_matrix<T, X> m_row_permutation;
|
||||
indexed_vector<T> m_work_vector;
|
||||
public:
|
||||
permutation_matrix<T, X> m_column_permutation;
|
||||
bool is_active() const { return m_parent != nullptr; }
|
||||
|
||||
square_dense_submatrix() {}
|
||||
|
||||
square_dense_submatrix (square_sparse_matrix<T, X> *parent_matrix, unsigned index_start);
|
||||
|
||||
void init(square_sparse_matrix<T, X> *parent_matrix, unsigned index_start);
|
||||
|
||||
bool is_dense() const override { return true; }
|
||||
|
||||
ref operator[] (unsigned i) {
|
||||
lp_assert(i >= m_index_start);
|
||||
lp_assert(i < m_parent->dimension());
|
||||
return ref(i, *this);
|
||||
}
|
||||
|
||||
int find_pivot_column_in_row(unsigned i) const;
|
||||
|
||||
void swap_columns(unsigned i, unsigned j) {
|
||||
if (i != j)
|
||||
m_column_permutation.transpose_from_left(i, j);
|
||||
}
|
||||
|
||||
unsigned adjust_column(unsigned col) const{
|
||||
if (col >= m_column_permutation.size())
|
||||
return col;
|
||||
return m_column_permutation.apply_reverse(col);
|
||||
}
|
||||
|
||||
unsigned adjust_column_inverse(unsigned col) const{
|
||||
if (col >= m_column_permutation.size())
|
||||
return col;
|
||||
return m_column_permutation[col];
|
||||
}
|
||||
unsigned adjust_row(unsigned row) const{
|
||||
if (row >= m_row_permutation.size())
|
||||
return row;
|
||||
return m_row_permutation[row];
|
||||
}
|
||||
|
||||
unsigned adjust_row_inverse(unsigned row) const{
|
||||
if (row >= m_row_permutation.size())
|
||||
return row;
|
||||
return m_row_permutation.apply_reverse(row);
|
||||
}
|
||||
|
||||
void pivot(unsigned i, lp_settings & settings);
|
||||
|
||||
void pivot_row_to_row(unsigned i, unsigned row, lp_settings & settings);;
|
||||
|
||||
void divide_row_by_pivot(unsigned i);
|
||||
|
||||
void update_parent_matrix(lp_settings & settings);
|
||||
|
||||
void update_existing_or_delete_in_parent_matrix_for_row(unsigned i, lp_settings & settings);
|
||||
|
||||
void push_new_elements_to_parent_matrix(lp_settings & settings);
|
||||
|
||||
template <typename L>
|
||||
L row_by_vector_product(unsigned i, const vector<L> & v);
|
||||
|
||||
template <typename L>
|
||||
L column_by_vector_product(unsigned j, const vector<L> & v);
|
||||
|
||||
template <typename L>
|
||||
L row_by_indexed_vector_product(unsigned i, const indexed_vector<L> & v);
|
||||
|
||||
template <typename L>
|
||||
void apply_from_left_local(indexed_vector<L> & w, lp_settings & settings);
|
||||
|
||||
template <typename L>
|
||||
void apply_from_left_to_vector(vector<L> & w);
|
||||
|
||||
bool is_L_matrix() const;
|
||||
|
||||
void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) override {
|
||||
apply_from_left_local(w, settings);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void apply_from_right(indexed_vector<T> & w) override {
|
||||
#if 1==0
|
||||
indexed_vector<T> wcopy = w;
|
||||
apply_from_right(wcopy.m_data);
|
||||
wcopy.m_index.clear();
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
if (!is_zero(wcopy.m_data[i]))
|
||||
wcopy.m_index.push_back(i);
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
T & v = wcopy.m_data[i];
|
||||
if (!lp_settings::is_eps_small_general(v, 1e-14)){
|
||||
wcopy.m_index.push_back(i);
|
||||
} else {
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
lp_assert(wcopy.is_OK());
|
||||
apply_from_right(w.m_data);
|
||||
w.m_index.clear();
|
||||
if (numeric_traits<T>::precise()) {
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
if (!is_zero(w.m_data[i]))
|
||||
w.m_index.push_back(i);
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
T & v = w.m_data[i];
|
||||
if (!lp_settings::is_eps_small_general(v, 1e-14)){
|
||||
w.m_index.push_back(i);
|
||||
} else {
|
||||
v = zero_of_type<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
lp_assert(w.is_OK());
|
||||
lp_assert(m_work_vector.is_OK());
|
||||
m_work_vector.resize(w.data_size());
|
||||
m_work_vector.clear();
|
||||
lp_assert(m_work_vector.is_OK());
|
||||
unsigned end = m_index_start + m_dim;
|
||||
for (unsigned k : w.m_index) {
|
||||
// find j such that k = adjust_row_inverse(j)
|
||||
unsigned j = adjust_row(k);
|
||||
if (j < m_index_start || j >= end) {
|
||||
m_work_vector.set_value(w[k], adjust_column_inverse(j));
|
||||
} else { // j >= m_index_start and j < end
|
||||
unsigned offset = (j - m_index_start) * m_dim; // this is the row start
|
||||
const T& wv = w[k];
|
||||
for (unsigned col = m_index_start; col < end; col++, offset ++) {
|
||||
unsigned adj_col = adjust_column_inverse(col);
|
||||
m_work_vector.add_value_at_index(adj_col, m_v[offset] * wv);
|
||||
}
|
||||
}
|
||||
}
|
||||
m_work_vector.clean_up();
|
||||
lp_assert(m_work_vector.is_OK());
|
||||
w = m_work_vector;
|
||||
#endif
|
||||
}
|
||||
void apply_from_left(vector<X> & w, lp_settings & /*settings*/) override {
|
||||
apply_from_left_to_vector(w);// , settings);
|
||||
}
|
||||
|
||||
void apply_from_right(vector<T> & w) override;
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
T get_elem (unsigned i, unsigned j) const override;
|
||||
unsigned row_count() const override { return m_parent->row_count();}
|
||||
unsigned column_count() const override { return row_count();}
|
||||
void set_number_of_rows(unsigned) override {}
|
||||
void set_number_of_columns(unsigned) override {}
|
||||
#endif
|
||||
void conjugate_by_permutation(permutation_matrix<T, X> & q);
|
||||
};
|
||||
}
|
||||
|
|
@ -1,370 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/square_dense_submatrix.h"
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
square_dense_submatrix<T, X>::square_dense_submatrix (square_sparse_matrix<T, X> *parent_matrix, unsigned index_start) :
|
||||
m_index_start(index_start),
|
||||
m_dim(parent_matrix->dimension() - index_start),
|
||||
m_v(m_dim * m_dim),
|
||||
m_parent(parent_matrix),
|
||||
m_row_permutation(m_parent->dimension()),
|
||||
m_column_permutation(m_parent->dimension()) {
|
||||
int row_offset = - static_cast<int>(m_index_start);
|
||||
for (unsigned i = index_start; i < parent_matrix->dimension(); i++) {
|
||||
unsigned row = parent_matrix->adjust_row(i);
|
||||
for (auto & iv : parent_matrix->get_row_values(row)) {
|
||||
unsigned j = parent_matrix->adjust_column_inverse(iv.m_index);
|
||||
lp_assert(j>= m_index_start);
|
||||
m_v[row_offset + j] = iv.m_value;
|
||||
}
|
||||
row_offset += m_dim;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::init(square_sparse_matrix<T, X> *parent_matrix, unsigned index_start) {
|
||||
m_index_start = index_start;
|
||||
m_dim = parent_matrix->dimension() - index_start;
|
||||
m_v.resize(m_dim * m_dim);
|
||||
m_parent = parent_matrix;
|
||||
m_column_permutation.init(m_parent->dimension());
|
||||
for (unsigned i = index_start; i < parent_matrix->dimension(); i++) {
|
||||
unsigned row = parent_matrix->adjust_row(i);
|
||||
for (auto & iv : parent_matrix->get_row_values(row)) {
|
||||
unsigned j = parent_matrix->adjust_column_inverse(iv.m_index);
|
||||
(*this)[i][j] = iv.m_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> int square_dense_submatrix<T, X>::find_pivot_column_in_row(unsigned i) const {
|
||||
int j = -1;
|
||||
T max = zero_of_type<T>();
|
||||
lp_assert(i >= m_index_start);
|
||||
unsigned row_start = (i - m_index_start) * m_dim;
|
||||
for (unsigned k = i; k < m_parent->dimension(); k++) {
|
||||
unsigned col = adjust_column(k); // this is where the column is in the row
|
||||
unsigned offs = row_start + col - m_index_start;
|
||||
T t = abs(m_v[offs]);
|
||||
if (t > max) {
|
||||
j = k;
|
||||
max = t;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::pivot(unsigned i, lp_settings & settings) {
|
||||
divide_row_by_pivot(i);
|
||||
for (unsigned k = i + 1; k < m_parent->dimension(); k++)
|
||||
pivot_row_to_row(i, k, settings);
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::pivot_row_to_row(unsigned i, unsigned row, lp_settings & settings) {
|
||||
lp_assert(i < row);
|
||||
unsigned pj = adjust_column(i); // the pivot column
|
||||
unsigned pjd = pj - m_index_start;
|
||||
unsigned pivot_row_offset = (i-m_index_start)*m_dim;
|
||||
T pivot = m_v[pivot_row_offset + pjd];
|
||||
unsigned row_offset= (row-m_index_start)*m_dim;
|
||||
T m = m_v[row_offset + pjd];
|
||||
lp_assert(!is_zero(pivot));
|
||||
m_v[row_offset + pjd] = -m * pivot; // creating L matrix
|
||||
for (unsigned j = m_index_start; j < m_parent->dimension(); j++) {
|
||||
if (j == pj) {
|
||||
pivot_row_offset++;
|
||||
row_offset++;
|
||||
continue;
|
||||
}
|
||||
auto t = m_v[row_offset] - m_v[pivot_row_offset] * m;
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(t)) {
|
||||
m_v[row_offset] = zero_of_type<T>();
|
||||
} else {
|
||||
m_v[row_offset] = t;
|
||||
}
|
||||
row_offset++; pivot_row_offset++;
|
||||
// at the same time we pivot the L too
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::divide_row_by_pivot(unsigned i) {
|
||||
unsigned pj = adjust_column(i); // the pivot column
|
||||
unsigned irow_offset = (i - m_index_start) * m_dim;
|
||||
T pivot = m_v[irow_offset + pj - m_index_start];
|
||||
lp_assert(!is_zero(pivot));
|
||||
for (unsigned k = m_index_start; k < m_parent->dimension(); k++) {
|
||||
if (k == pj){
|
||||
m_v[irow_offset++] = one_of_type<T>() / pivot; // creating the L matrix diagonal
|
||||
continue;
|
||||
}
|
||||
m_v[irow_offset++] /= pivot;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::update_parent_matrix(lp_settings & settings) {
|
||||
for (unsigned i = m_index_start; i < m_parent->dimension(); i++)
|
||||
update_existing_or_delete_in_parent_matrix_for_row(i, settings);
|
||||
push_new_elements_to_parent_matrix(settings);
|
||||
for (unsigned i = m_index_start; i < m_parent->dimension(); i++)
|
||||
m_parent->set_max_in_row(m_parent->adjust_row(i));
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::update_existing_or_delete_in_parent_matrix_for_row(unsigned i, lp_settings & settings) {
|
||||
bool diag_updated = false;
|
||||
unsigned ai = m_parent->adjust_row(i);
|
||||
auto & row_vals = m_parent->get_row_values(ai);
|
||||
for (unsigned k = 0; k < row_vals.size(); k++) {
|
||||
auto & iv = row_vals[k];
|
||||
unsigned j = m_parent->adjust_column_inverse(iv.m_index);
|
||||
if (j < i) {
|
||||
m_parent->remove_element(row_vals, iv);
|
||||
k--;
|
||||
} else if (i == j) {
|
||||
m_parent->m_columns[iv.m_index].m_values[iv.m_other].set_value(iv.m_value = one_of_type<T>());
|
||||
diag_updated = true;
|
||||
} else { // j > i
|
||||
T & v = (*this)[i][j];
|
||||
if (settings.abs_val_is_smaller_than_drop_tolerance(v)) {
|
||||
m_parent->remove_element(row_vals, iv);
|
||||
k--;
|
||||
} else {
|
||||
m_parent->m_columns[iv.m_index].m_values[iv.m_other].set_value(iv.m_value = v);
|
||||
v = zero_of_type<T>(); // only new elements are left above the diagonal
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!diag_updated) {
|
||||
unsigned aj = m_parent->adjust_column(i);
|
||||
m_parent->add_new_element(ai, aj, one_of_type<T>());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::push_new_elements_to_parent_matrix(lp_settings & settings) {
|
||||
for (unsigned i = m_index_start; i < m_parent->dimension() - 1; i++) {
|
||||
unsigned ai = m_parent->adjust_row(i);
|
||||
for (unsigned j = i + 1; j < m_parent->dimension(); j++) {
|
||||
T & v = (*this)[i][j];
|
||||
if (!settings.abs_val_is_smaller_than_drop_tolerance(v)) {
|
||||
unsigned aj = m_parent->adjust_column(j);
|
||||
m_parent->add_new_element(ai, aj, v);
|
||||
}
|
||||
v = zero_of_type<T>(); // leave only L elements now
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
L square_dense_submatrix<T, X>::row_by_vector_product(unsigned i, const vector<L> & v) {
|
||||
lp_assert(i >= m_index_start);
|
||||
|
||||
unsigned row_in_subm = i - m_index_start;
|
||||
unsigned row_offset = row_in_subm * m_dim;
|
||||
L r = zero_of_type<L>();
|
||||
for (unsigned j = 0; j < m_dim; j++)
|
||||
r += m_v[row_offset + j] * v[adjust_column_inverse(m_index_start + j)];
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
L square_dense_submatrix<T, X>::column_by_vector_product(unsigned j, const vector<L> & v) {
|
||||
lp_assert(j >= m_index_start);
|
||||
|
||||
unsigned offset = j - m_index_start;
|
||||
L r = zero_of_type<L>();
|
||||
for (unsigned i = 0; i < m_dim; i++, offset += m_dim)
|
||||
r += m_v[offset] * v[adjust_row_inverse(m_index_start + i)];
|
||||
return r;
|
||||
}
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
L square_dense_submatrix<T, X>::row_by_indexed_vector_product(unsigned i, const indexed_vector<L> & v) {
|
||||
lp_assert(i >= m_index_start);
|
||||
|
||||
unsigned row_in_subm = i - m_index_start;
|
||||
unsigned row_offset = row_in_subm * m_dim;
|
||||
L r = zero_of_type<L>();
|
||||
for (unsigned j = 0; j < m_dim; j++)
|
||||
r += m_v[row_offset + j] * v[adjust_column_inverse(m_index_start + j)];
|
||||
return r;
|
||||
}
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
void square_dense_submatrix<T, X>::apply_from_left_local(indexed_vector<L> & w, lp_settings & settings) {
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T, X> deb(*this);
|
||||
// vector<L> deb_w(w.m_data.size());
|
||||
// for (unsigned i = 0; i < w.m_data.size(); i++)
|
||||
// deb_w[i] = w[i];
|
||||
|
||||
// deb.apply_from_left(deb_w);
|
||||
#endif // use indexed vector here
|
||||
|
||||
#ifndef DO_NOT_USE_INDEX
|
||||
vector<L> t(m_parent->dimension(), zero_of_type<L>());
|
||||
for (auto k : w.m_index) {
|
||||
unsigned j = adjust_column(k); // k-th element will contribute only to column j
|
||||
if (j < m_index_start || j >= this->m_index_start + this->m_dim) { // it is a unit matrix outside
|
||||
t[adjust_row_inverse(j)] = w[k];
|
||||
} else {
|
||||
const L & v = w[k];
|
||||
for (unsigned i = 0; i < m_dim; i++) {
|
||||
unsigned row = adjust_row_inverse(m_index_start + i);
|
||||
unsigned offs = i * m_dim + j - m_index_start;
|
||||
t[row] += m_v[offs] * v;
|
||||
}
|
||||
}
|
||||
}
|
||||
w.m_index.clear();
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
const L & v = t[i];
|
||||
if (!settings.abs_val_is_smaller_than_drop_tolerance(v)){
|
||||
w.m_index.push_back(i);
|
||||
w.m_data[i] = v;
|
||||
} else {
|
||||
w.m_data[i] = zero_of_type<L>();
|
||||
}
|
||||
}
|
||||
#else
|
||||
vector<L> t(m_parent->dimension());
|
||||
for (unsigned i = 0; i < m_index_start; i++) {
|
||||
t[adjust_row_inverse(i)] = w[adjust_column_inverse(i)];
|
||||
}
|
||||
for (unsigned i = m_index_start; i < m_parent->dimension(); i++){
|
||||
t[adjust_row_inverse(i)] = row_by_indexed_vector_product(i, w);
|
||||
}
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
w.set_value(t[i], i);
|
||||
}
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
const L & v = t[i];
|
||||
if (!is_zero(v))
|
||||
w.m_index.push_back(i);
|
||||
w.m_data[i] = v;
|
||||
}
|
||||
#endif
|
||||
#ifdef Z3DEBUG
|
||||
// cout << "w final" << endl;
|
||||
// print_vector(w.m_data);
|
||||
// lp_assert(vectors_are_equal<T>(deb_w, w.m_data));
|
||||
// lp_assert(w.is_OK());
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X>
|
||||
template <typename L>
|
||||
void square_dense_submatrix<T, X>::apply_from_left_to_vector(vector<L> & w) {
|
||||
// lp_settings & settings) {
|
||||
// dense_matrix<T, L> deb(*this);
|
||||
// vector<L> deb_w(w);
|
||||
// deb.apply_from_left_to_X(deb_w, settings);
|
||||
// // cout << "deb" << endl;
|
||||
// // print_matrix(deb);
|
||||
// // cout << "w" << endl;
|
||||
// // print_vector(w.m_data);
|
||||
// // cout << "deb_w" << endl;
|
||||
// // print_vector(deb_w);
|
||||
vector<L> t(m_parent->dimension());
|
||||
for (unsigned i = 0; i < m_index_start; i++) {
|
||||
t[adjust_row_inverse(i)] = w[adjust_column_inverse(i)];
|
||||
}
|
||||
for (unsigned i = m_index_start; i < m_parent->dimension(); i++){
|
||||
t[adjust_row_inverse(i)] = row_by_vector_product(i, w);
|
||||
}
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
w[i] = t[i];
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
// cout << "w final" << endl;
|
||||
// print_vector(w.m_data);
|
||||
// lp_assert(vectors_are_equal<L>(deb_w, w));
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename X> bool square_dense_submatrix<T, X>::is_L_matrix() const {
|
||||
#ifdef Z3DEBUG
|
||||
lp_assert(m_row_permutation.is_identity());
|
||||
for (unsigned i = 0; i < m_parent->dimension(); i++) {
|
||||
if (i < m_index_start) {
|
||||
lp_assert(m_column_permutation[i] == i);
|
||||
continue;
|
||||
}
|
||||
unsigned row_offs = (i-m_index_start)*m_dim;
|
||||
for (unsigned k = 0; k < m_dim; k++) {
|
||||
unsigned j = m_index_start + k;
|
||||
unsigned jex = adjust_column_inverse(j);
|
||||
if (jex > i) {
|
||||
lp_assert(is_zero(m_v[row_offs + k]));
|
||||
} else if (jex == i) {
|
||||
lp_assert(!is_zero(m_v[row_offs + k]));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::apply_from_right(vector<T> & w) {
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T, X> deb(*this);
|
||||
// vector<T> deb_w(w);
|
||||
// deb.apply_from_right(deb_w);
|
||||
#endif
|
||||
vector<T> t(w.size());
|
||||
|
||||
for (unsigned j = 0; j < m_index_start; j++) {
|
||||
t[adjust_column_inverse(j)] = w[adjust_row_inverse(j)];
|
||||
}
|
||||
unsigned end = m_index_start + m_dim;
|
||||
for (unsigned j = end; j < m_parent->dimension(); j++) {
|
||||
t[adjust_column_inverse(j)] = w[adjust_row_inverse(j)];
|
||||
}
|
||||
for (unsigned j = m_index_start; j < end; j++) {
|
||||
t[adjust_column_inverse(j)] = column_by_vector_product(j, w);
|
||||
}
|
||||
w = t;
|
||||
#ifdef Z3DEBUG
|
||||
// lp_assert(vector_are_equal<T>(deb_w, w));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
|
||||
template <typename T, typename X> T square_dense_submatrix<T, X>::get_elem (unsigned i, unsigned j) const {
|
||||
i = adjust_row(i);
|
||||
j = adjust_column(j);
|
||||
if (i < m_index_start || j < m_index_start)
|
||||
return i == j? one_of_type<T>() : zero_of_type<T>();
|
||||
unsigned offs = (i - m_index_start)* m_dim + j - m_index_start;
|
||||
return m_v[offs];
|
||||
}
|
||||
|
||||
#endif
|
||||
template <typename T, typename X> void square_dense_submatrix<T, X>::conjugate_by_permutation(permutation_matrix<T, X> & q) {
|
||||
m_row_permutation.multiply_by_permutation_from_left(q);
|
||||
m_column_permutation.multiply_by_reverse_from_right(q);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,119 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
#include <memory>
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include "math/lp/lu.h"
|
||||
#include "math/lp/square_sparse_matrix_def.h"
|
||||
#include "math/lp/dense_matrix.h"
|
||||
namespace lp {
|
||||
template double square_sparse_matrix<double, double>::dot_product_with_row<double>(unsigned int, vector<double> const&) const;
|
||||
template void square_sparse_matrix<double, double>::add_new_element(unsigned int, unsigned int, const double&);
|
||||
template void square_sparse_matrix<double, double>::divide_row_by_constant(unsigned int, const double&, lp_settings&);
|
||||
template bool square_sparse_matrix<double, double>::fill_eta_matrix(unsigned int, eta_matrix<double, double>**);
|
||||
template const double & square_sparse_matrix<double, double>::get(unsigned int, unsigned int) const;
|
||||
template unsigned square_sparse_matrix<double, double>::get_number_of_nonzeroes() const;
|
||||
template bool square_sparse_matrix<double, double>::get_pivot_for_column(unsigned int&, unsigned int&, int, unsigned int);
|
||||
template unsigned square_sparse_matrix<double, double>::lowest_row_in_column(unsigned int);
|
||||
template bool square_sparse_matrix<double, double>::pivot_row_to_row(unsigned int, const double&, unsigned int, lp_settings&);
|
||||
template bool square_sparse_matrix<double, double>::pivot_with_eta(unsigned int, eta_matrix<double, double>*, lp_settings&);
|
||||
template void square_sparse_matrix<double, double>::prepare_for_factorization();
|
||||
template void square_sparse_matrix<double, double>::remove_element(vector<indexed_value<double> >&, indexed_value<double>&);
|
||||
template void square_sparse_matrix<double, double>::replace_column(unsigned int, indexed_vector<double>&, lp_settings&);
|
||||
template void square_sparse_matrix<double, double>::set(unsigned int, unsigned int, double);
|
||||
template void square_sparse_matrix<double, double>::set_max_in_row(vector<indexed_value<double> >&);
|
||||
template bool square_sparse_matrix<double, double>::set_row_from_work_vector_and_clean_work_vector_not_adjusted(unsigned int, indexed_vector<double>&, lp_settings&);
|
||||
template bool square_sparse_matrix<double, double>::shorten_active_matrix(unsigned int, eta_matrix<double, double>*);
|
||||
template void square_sparse_matrix<double, double>::solve_y_U(vector<double>&) const;
|
||||
template square_sparse_matrix<double, double>::square_sparse_matrix(unsigned int, unsigned);
|
||||
template void square_sparse_matrix<mpq, mpq>::add_new_element(unsigned int, unsigned int, const mpq&);
|
||||
template void square_sparse_matrix<mpq, mpq>::divide_row_by_constant(unsigned int, const mpq&, lp_settings&);
|
||||
template bool square_sparse_matrix<mpq, mpq>::fill_eta_matrix(unsigned int, eta_matrix<mpq, mpq>**);
|
||||
template mpq const & square_sparse_matrix<mpq, mpq>::get(unsigned int, unsigned int) const;
|
||||
template unsigned square_sparse_matrix<mpq, mpq>::get_number_of_nonzeroes() const;
|
||||
template bool square_sparse_matrix<mpq, mpq>::get_pivot_for_column(unsigned int&, unsigned int&, int, unsigned int);
|
||||
template unsigned square_sparse_matrix<mpq, mpq>::lowest_row_in_column(unsigned int);
|
||||
template bool square_sparse_matrix<mpq, mpq>::pivot_with_eta(unsigned int, eta_matrix<mpq, mpq>*, lp_settings&);
|
||||
template void square_sparse_matrix<mpq, mpq>::prepare_for_factorization();
|
||||
template void square_sparse_matrix<mpq, mpq>::remove_element(vector<indexed_value<mpq>> &, indexed_value<mpq>&);
|
||||
template void square_sparse_matrix<mpq, mpq>::replace_column(unsigned int, indexed_vector<mpq>&, lp_settings&);
|
||||
template void square_sparse_matrix<mpq, mpq>::set_max_in_row(vector<indexed_value<mpq>>&);
|
||||
template bool square_sparse_matrix<mpq, mpq>::set_row_from_work_vector_and_clean_work_vector_not_adjusted(unsigned int, indexed_vector<mpq>&, lp_settings&);
|
||||
template bool square_sparse_matrix<mpq, mpq>::shorten_active_matrix(unsigned int, eta_matrix<mpq, mpq>*);
|
||||
template void square_sparse_matrix<mpq, mpq>::solve_y_U(vector<mpq>&) const;
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::add_new_element(unsigned int, unsigned int, const mpq&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::divide_row_by_constant(unsigned int, const mpq&, lp_settings&);
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq>>::fill_eta_matrix(unsigned int, eta_matrix<mpq, numeric_pair<mpq> >**);
|
||||
template const mpq & square_sparse_matrix<mpq, numeric_pair<mpq>>::get(unsigned int, unsigned int) const;
|
||||
template unsigned square_sparse_matrix<mpq, numeric_pair<mpq>>::get_number_of_nonzeroes() const;
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq>>::get_pivot_for_column(unsigned int&, unsigned int&, int, unsigned int);
|
||||
template unsigned square_sparse_matrix<mpq, numeric_pair<mpq>>::lowest_row_in_column(unsigned int);
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq>>::pivot_with_eta(unsigned int, eta_matrix<mpq, numeric_pair<mpq> >*, lp_settings&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::prepare_for_factorization();
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::remove_element(vector<indexed_value<mpq>>&, indexed_value<mpq>&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::replace_column(unsigned int, indexed_vector<mpq>&, lp_settings&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::set_max_in_row(vector<indexed_value<mpq>>&);
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq>>::set_row_from_work_vector_and_clean_work_vector_not_adjusted(unsigned int, indexed_vector<mpq>&, lp_settings&);
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq>>::shorten_active_matrix(unsigned int, eta_matrix<mpq, numeric_pair<mpq> >*);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::solve_y_U(vector<mpq>&) const;
|
||||
template void square_sparse_matrix<double, double>::double_solve_U_y<double>(indexed_vector<double>&, const lp_settings &);
|
||||
template void square_sparse_matrix<mpq, mpq>::double_solve_U_y<mpq>(indexed_vector<mpq>&, const lp_settings&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq>>::double_solve_U_y<mpq>(indexed_vector<mpq>&, const lp_settings&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::double_solve_U_y<numeric_pair<mpq> >(indexed_vector<numeric_pair<mpq>>&, const lp_settings&);
|
||||
template void square_sparse_matrix<double, double>::solve_U_y_indexed_only<double>(indexed_vector<double>&, const lp_settings&, vector<unsigned> &);
|
||||
template void square_sparse_matrix<mpq, mpq>::solve_U_y_indexed_only<mpq>(indexed_vector<mpq>&, const lp_settings &, vector<unsigned> &);
|
||||
#ifdef Z3DEBUG
|
||||
template bool square_sparse_matrix<double, double>::is_upper_triangular_and_maximums_are_set_correctly_in_rows(lp_settings&) const;
|
||||
template bool square_sparse_matrix<mpq, mpq>::is_upper_triangular_and_maximums_are_set_correctly_in_rows(lp_settings&) const;
|
||||
template bool square_sparse_matrix<mpq, numeric_pair<mpq> >::is_upper_triangular_and_maximums_are_set_correctly_in_rows(lp_settings&) const;
|
||||
#endif
|
||||
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::solve_U_y_indexed_only<mpq>(indexed_vector<mpq>&, const lp_settings &, vector<unsigned> &);
|
||||
template void square_sparse_matrix<mpq, mpq>::solve_U_y<mpq>(vector<mpq>&);
|
||||
template void square_sparse_matrix<mpq, mpq>::double_solve_U_y<mpq>(vector<mpq >&);
|
||||
template void square_sparse_matrix<double, double>::solve_U_y<double>(vector<double>&);
|
||||
template void square_sparse_matrix<double, double>::double_solve_U_y<double>(vector<double>&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::solve_U_y<numeric_pair<mpq> >(vector<numeric_pair<mpq> >&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::double_solve_U_y<numeric_pair<mpq> >(vector<numeric_pair<mpq> >&);
|
||||
template void square_sparse_matrix<double, double>::find_error_in_solution_U_y_indexed<double>(indexed_vector<double>&, indexed_vector<double>&, const vector<unsigned> &);
|
||||
template double square_sparse_matrix<double, double>::dot_product_with_row<double>(unsigned int, indexed_vector<double> const&) const;
|
||||
template void square_sparse_matrix<mpq, mpq>::find_error_in_solution_U_y_indexed<mpq>(indexed_vector<mpq>&, indexed_vector<mpq>&, const vector<unsigned> &);
|
||||
template mpq square_sparse_matrix<mpq, mpq>::dot_product_with_row<mpq>(unsigned int, indexed_vector<mpq> const&) const;
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::find_error_in_solution_U_y_indexed<mpq>(indexed_vector<mpq>&, indexed_vector<mpq>&, const vector<unsigned> &);
|
||||
template mpq square_sparse_matrix<mpq, numeric_pair<mpq> >::dot_product_with_row<mpq>(unsigned int, indexed_vector<mpq> const&) const;
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::find_error_in_solution_U_y_indexed<numeric_pair<mpq> >(indexed_vector<numeric_pair<mpq> >&, indexed_vector<numeric_pair<mpq> >&, const vector<unsigned> &);
|
||||
template numeric_pair<mpq> square_sparse_matrix<mpq, numeric_pair<mpq> >::dot_product_with_row<numeric_pair<mpq> >(unsigned int, indexed_vector<numeric_pair<mpq> > const&) const;
|
||||
template void square_sparse_matrix<mpq, mpq>::extend_and_sort_active_rows(vector<unsigned int> const&, vector<unsigned int>&);
|
||||
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::extend_and_sort_active_rows(vector<unsigned int> const&, vector<unsigned int>&);
|
||||
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::solve_U_y<mpq>(vector<mpq >&);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::double_solve_U_y<mpq>(vector<mpq >&);
|
||||
template void square_sparse_matrix< mpq,numeric_pair< mpq> >::set(unsigned int,unsigned int, mpq);
|
||||
template void square_sparse_matrix<double, double>::solve_y_U_indexed(indexed_vector<double>&, const lp_settings & );
|
||||
template void square_sparse_matrix<mpq, mpq>::solve_y_U_indexed(indexed_vector<mpq>&, const lp_settings &);
|
||||
template void square_sparse_matrix<mpq, numeric_pair<mpq> >::solve_y_U_indexed(indexed_vector<mpq>&, const lp_settings &);
|
||||
|
||||
template square_sparse_matrix<double, double>::square_sparse_matrix(static_matrix<double, double> const&, vector<unsigned int, true, unsigned int>&);
|
||||
template square_sparse_matrix<mpq, mpq>::square_sparse_matrix (static_matrix<mpq, mpq> const&, vector<unsigned int, true, unsigned int>&);
|
||||
template square_sparse_matrix<mpq, numeric_pair<mpq> >::square_sparse_matrix(static_matrix<mpq, numeric_pair<mpq> > const&, vector<unsigned int, true, unsigned int>&);
|
||||
}
|
||||
template void lp::square_sparse_matrix<double, double>::copy_from_input_on_basis<lp::static_matrix<double, double> >(lp::static_matrix<double, double> const&, vector<unsigned int, true, unsigned int>&);
|
||||
template void lp::square_sparse_matrix<rational, rational>::copy_from_input_on_basis<lp::static_matrix<rational, rational> >(lp::static_matrix<rational, rational> const&, vector<unsigned int, true, unsigned int>&);
|
||||
|
|
@ -1,433 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
#include "math/lp/static_matrix.h"
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include "math/lp/indexed_value.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include <functional>
|
||||
#include "math/lp/lp_settings.h"
|
||||
#include "math/lp/eta_matrix.h"
|
||||
#include "math/lp/binary_heap_upair_queue.h"
|
||||
#include "math/lp/numeric_pair.h"
|
||||
#include "math/lp/u_set.h"
|
||||
namespace lp {
|
||||
// it is a square matrix
|
||||
template <typename T, typename X>
|
||||
class square_sparse_matrix
|
||||
: public matrix<T, X>
|
||||
{
|
||||
struct col_header {
|
||||
unsigned m_shortened_markovitz;
|
||||
vector<indexed_value<T>> m_values; // the actual column values
|
||||
|
||||
col_header(): m_shortened_markovitz(0) {}
|
||||
|
||||
void shorten_markovich_by_one() {
|
||||
m_shortened_markovitz++;
|
||||
}
|
||||
|
||||
void zero_shortened_markovitz() {
|
||||
m_shortened_markovitz = 0;
|
||||
}
|
||||
};
|
||||
|
||||
unsigned m_n_of_active_elems;
|
||||
binary_heap_upair_queue<unsigned> m_pivot_queue;
|
||||
public:
|
||||
vector<vector<indexed_value<T>>> m_rows;
|
||||
vector<col_header> m_columns;
|
||||
permutation_matrix<T, X> m_row_permutation;
|
||||
permutation_matrix<T, X> m_column_permutation;
|
||||
// m_work_pivot_vector[j] = offset of elementh of j-th column in the row we are pivoting to
|
||||
// if the column is not present then m_work_pivot_vector[j] is -1
|
||||
vector<int> m_work_pivot_vector;
|
||||
vector<bool> m_processed;
|
||||
unsigned get_n_of_active_elems() const { return m_n_of_active_elems; }
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
// dense_matrix<T> m_dense;
|
||||
#endif
|
||||
/*
|
||||
the rule is: row i is mapped to m_row_permutation[i] and
|
||||
column j is mapped to m_column_permutation.apply_reverse(j)
|
||||
*/
|
||||
|
||||
unsigned adjust_row(unsigned row) const{
|
||||
return m_row_permutation[row];
|
||||
}
|
||||
|
||||
unsigned adjust_column(unsigned col) const{
|
||||
return m_column_permutation.apply_reverse(col);
|
||||
}
|
||||
|
||||
unsigned adjust_row_inverse(unsigned row) const{
|
||||
return m_row_permutation.apply_reverse(row);
|
||||
}
|
||||
|
||||
unsigned adjust_column_inverse(unsigned col) const{
|
||||
return m_column_permutation[col];
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
void copy_column_from_input(unsigned input_column, const M& A, unsigned j);
|
||||
template <typename M>
|
||||
void copy_column_from_input_with_possible_zeros(const M& A, unsigned j);
|
||||
|
||||
template <typename M>
|
||||
void copy_from_input(const M& A);
|
||||
template <typename M>
|
||||
void copy_from_input_on_basis(const M& A, vector<unsigned> & basis);
|
||||
|
||||
public:
|
||||
|
||||
// constructors
|
||||
template <typename M>
|
||||
square_sparse_matrix(const M &A, vector<unsigned>& basis);
|
||||
|
||||
template <typename M>
|
||||
square_sparse_matrix(const M &A);
|
||||
|
||||
square_sparse_matrix(unsigned dim, unsigned); // the second parameter is needed to distinguish this
|
||||
// constructor from the one above
|
||||
|
||||
|
||||
|
||||
class ref_matrix_element {
|
||||
square_sparse_matrix & m_matrix;
|
||||
unsigned m_row;
|
||||
unsigned m_col;
|
||||
public:
|
||||
ref_matrix_element(square_sparse_matrix & m, unsigned row, unsigned col):m_matrix(m), m_row(row), m_col(col) {}
|
||||
ref_matrix_element & operator=(T const & v) { m_matrix.set( m_row, m_col, v); return *this; }
|
||||
ref_matrix_element & operator=(ref_matrix_element const & v) { m_matrix.set(m_row, m_col, v.m_matrix.get(v.m_row, v.m_col)); return *this; }
|
||||
operator T () const { return m_matrix.get(m_row, m_col); }
|
||||
};
|
||||
|
||||
class ref_row {
|
||||
square_sparse_matrix & m_matrix;
|
||||
unsigned m_row;
|
||||
public:
|
||||
ref_row(square_sparse_matrix & m, unsigned row) : m_matrix(m), m_row(row) {}
|
||||
ref_matrix_element operator[](unsigned col) const { return ref_matrix_element(m_matrix, m_row, col); }
|
||||
};
|
||||
|
||||
void set_with_no_adjusting_for_row(unsigned row, unsigned col, T val);
|
||||
void set_with_no_adjusting_for_col(unsigned row, unsigned col, T val);
|
||||
|
||||
void set_with_no_adjusting(unsigned row, unsigned col, T val);
|
||||
|
||||
void set(unsigned row, unsigned col, T val);
|
||||
|
||||
T const & get_not_adjusted(unsigned row, unsigned col) const;
|
||||
T const & get(unsigned row, unsigned col) const;
|
||||
|
||||
ref_row operator[](unsigned row) { return ref_row(*this, row); }
|
||||
|
||||
ref_matrix_element operator()(unsigned row, unsigned col) { return ref_matrix_element(*this, row, col); }
|
||||
|
||||
T operator() (unsigned row, unsigned col) const { return get(row, col); }
|
||||
|
||||
vector<indexed_value<T>> & get_row_values(unsigned row) {
|
||||
return m_rows[row];
|
||||
}
|
||||
|
||||
vector<indexed_value<T>> const & get_row_values(unsigned row) const {
|
||||
return m_rows[row];
|
||||
}
|
||||
|
||||
vector<indexed_value<T>> & get_column_values(unsigned col) {
|
||||
return m_columns[col].m_values;
|
||||
}
|
||||
|
||||
vector<indexed_value<T>> const & get_column_values(unsigned col) const {
|
||||
return m_columns[col].m_values;
|
||||
}
|
||||
|
||||
unsigned dimension() const {return static_cast<unsigned>(m_row_permutation.size());}
|
||||
|
||||
unsigned row_count() const override {return dimension();}
|
||||
unsigned column_count() const override {return dimension();}
|
||||
|
||||
void init_row_headers();
|
||||
|
||||
void init_column_headers();
|
||||
|
||||
unsigned lowest_row_in_column(unsigned j);
|
||||
|
||||
indexed_value<T> & column_iv_other(indexed_value<T> & iv) {
|
||||
return m_rows[iv.m_index][iv.m_other];
|
||||
}
|
||||
|
||||
indexed_value<T> & row_iv_other(indexed_value<T> & iv) {
|
||||
return m_columns[iv.m_index].m_values[iv.m_other];
|
||||
}
|
||||
|
||||
void remove_element(vector<indexed_value<T>> & row_vals, unsigned row_offset, vector<indexed_value<T>> & column_vals, unsigned column_offset);
|
||||
|
||||
void remove_element(vector<indexed_value<T>> & row_chunk, indexed_value<T> & row_el_iv);
|
||||
|
||||
void put_max_index_to_0(vector<indexed_value<T>> & row_vals, unsigned max_index);
|
||||
|
||||
void set_max_in_row(unsigned row) {
|
||||
set_max_in_row(m_rows[row]);
|
||||
}
|
||||
|
||||
|
||||
void set_max_in_row(vector<indexed_value<T>> & row_vals);
|
||||
|
||||
bool pivot_with_eta(unsigned i, eta_matrix<T, X> *eta_matrix, lp_settings & settings);
|
||||
|
||||
void scan_row_to_work_vector_and_remove_pivot_column(unsigned row, unsigned pivot_column);
|
||||
|
||||
// This method pivots row i to row i0 by muliplying row i by
|
||||
// alpha and adding it to row i0.
|
||||
// After pivoting the row i0 has a max abs value set correctly at the beginning of m_start,
|
||||
// Returns false if the resulting row is all zeroes, and true otherwise
|
||||
bool pivot_row_to_row(unsigned i, const T& alpha, unsigned i0, lp_settings & settings );
|
||||
|
||||
// set the max val as well
|
||||
// returns false if the resulting row is all zeroes, and true otherwise
|
||||
bool set_row_from_work_vector_and_clean_work_vector_not_adjusted(unsigned i0, indexed_vector<T> & work_vec,
|
||||
lp_settings & settings);
|
||||
|
||||
|
||||
// set the max val as well
|
||||
// returns false if the resulting row is all zeroes, and true otherwise
|
||||
bool set_row_from_work_vector_and_clean_work_vector(unsigned i0);
|
||||
|
||||
void remove_zero_elements_and_set_data_on_existing_elements(unsigned row);
|
||||
|
||||
// work_vec here has not adjusted column indices
|
||||
void remove_zero_elements_and_set_data_on_existing_elements_not_adjusted(unsigned row, indexed_vector<T> & work_vec, lp_settings & settings);
|
||||
|
||||
void multiply_from_right(permutation_matrix<T, X>& p) {
|
||||
// m_dense = m_dense * p;
|
||||
m_column_permutation.multiply_by_permutation_from_right(p);
|
||||
// lp_assert(*this == m_dense);
|
||||
}
|
||||
|
||||
void multiply_from_left(permutation_matrix<T, X>& p) {
|
||||
// m_dense = p * m_dense;
|
||||
m_row_permutation.multiply_by_permutation_from_left(p);
|
||||
// lp_assert(*this == m_dense);
|
||||
}
|
||||
|
||||
void multiply_from_left_with_reverse(permutation_matrix<T, X>& p) {
|
||||
// m_dense = p * m_dense;
|
||||
m_row_permutation.multiply_by_permutation_reverse_from_left(p);
|
||||
// lp_assert(*this == m_dense);
|
||||
}
|
||||
|
||||
// adding delta columns at the end of the matrix
|
||||
void add_columns_at_the_end(unsigned delta);
|
||||
|
||||
void delete_column(int i);
|
||||
|
||||
void swap_columns(unsigned a, unsigned b) {
|
||||
m_column_permutation.transpose_from_left(a, b);
|
||||
}
|
||||
|
||||
void swap_rows(unsigned a, unsigned b) {
|
||||
m_row_permutation.transpose_from_right(a, b);
|
||||
// m_dense.swap_rows(a, b);
|
||||
// lp_assert(*this == m_dense);
|
||||
}
|
||||
|
||||
void divide_row_by_constant(unsigned i, const T & t, lp_settings & settings);
|
||||
|
||||
bool close(T a, T b) {
|
||||
return // (numeric_traits<T>::precise() && numeric_traits<T>::is_zero(a - b))
|
||||
// ||
|
||||
fabs(numeric_traits<T>::get_double(a - b)) < 0.0000001;
|
||||
}
|
||||
|
||||
// solving x * this = y, and putting the answer into y
|
||||
// the matrix here has to be upper triangular
|
||||
void solve_y_U(vector<T> & y) const;
|
||||
|
||||
// solving x * this = y, and putting the answer into y
|
||||
// the matrix here has to be upper triangular
|
||||
void solve_y_U_indexed(indexed_vector<T> & y, const lp_settings &);
|
||||
|
||||
// fills the indices for such that y[i] can be not a zero
|
||||
// sort them so the smaller indices come first
|
||||
void fill_reachable_indices(std::set<unsigned> & rset, T *y);
|
||||
|
||||
template <typename L>
|
||||
void find_error_in_solution_U_y(vector<L>& y_orig, vector<L> & y);
|
||||
|
||||
template <typename L>
|
||||
void find_error_in_solution_U_y_indexed(indexed_vector<L>& y_orig, indexed_vector<L> & y, const vector<unsigned>& sorted_active_rows);
|
||||
|
||||
template <typename L>
|
||||
void add_delta_to_solution(const vector<L>& del, vector<L> & y);
|
||||
|
||||
template <typename L>
|
||||
void add_delta_to_solution(const indexed_vector<L>& del, indexed_vector<L> & y);
|
||||
|
||||
template <typename L>
|
||||
void double_solve_U_y(indexed_vector<L>& y, const lp_settings & settings);
|
||||
|
||||
template <typename L>
|
||||
void double_solve_U_y(vector<L>& y);
|
||||
// solving this * x = y, and putting the answer into y
|
||||
// the matrix here has to be upper triangular
|
||||
template <typename L>
|
||||
void solve_U_y(vector<L> & y);
|
||||
// solving this * x = y, and putting the answer into y
|
||||
// the matrix here has to be upper triangular
|
||||
template <typename L>
|
||||
void solve_U_y_indexed_only(indexed_vector<L> & y, const lp_settings&, vector<unsigned> & sorted_active_rows );
|
||||
|
||||
T get_elem(unsigned i, unsigned j) const override { return get(i, j); }
|
||||
unsigned get_number_of_rows() const { return dimension(); }
|
||||
unsigned get_number_of_columns() const { return dimension(); }
|
||||
void set_number_of_rows(unsigned /*m*/) override { }
|
||||
void set_number_of_columns(unsigned /*n*/) override { }
|
||||
template <typename L>
|
||||
L dot_product_with_row (unsigned row, const vector<L> & y) const;
|
||||
|
||||
template <typename L>
|
||||
L dot_product_with_row (unsigned row, const indexed_vector<L> & y) const;
|
||||
|
||||
unsigned get_number_of_nonzeroes() const;
|
||||
|
||||
bool get_non_zero_column_in_row(unsigned i, unsigned *j) const;
|
||||
|
||||
void remove_element_that_is_not_in_w(vector<indexed_value<T>> & column_vals, indexed_value<T> & col_el_iv);
|
||||
|
||||
|
||||
// w contains the new column
|
||||
// the old column inside of the matrix has not been changed yet
|
||||
void remove_elements_that_are_not_in_w_and_update_common_elements(unsigned column_to_replace, indexed_vector<T> & w);
|
||||
|
||||
void add_new_element(unsigned row, unsigned col, const T& val);
|
||||
|
||||
// w contains the "rest" of the new column; all common elements of w and the old column has been zeroed
|
||||
// the old column inside of the matrix has not been changed yet
|
||||
void add_new_elements_of_w_and_clear_w(unsigned column_to_replace, indexed_vector<T> & w, lp_settings & settings);
|
||||
|
||||
void replace_column(unsigned column_to_replace, indexed_vector<T> & w, lp_settings &settings);
|
||||
|
||||
unsigned pivot_score(unsigned i, unsigned j);
|
||||
|
||||
void enqueue_domain_into_pivot_queue();
|
||||
|
||||
void set_max_in_rows();
|
||||
|
||||
void zero_shortened_markovitz_numbers();
|
||||
|
||||
void prepare_for_factorization();
|
||||
|
||||
void recover_pivot_queue(vector<upair> & rejected_pivots);
|
||||
|
||||
int elem_is_too_small(unsigned i, unsigned j, int c_partial_pivoting);
|
||||
|
||||
bool remove_row_from_active_pivots_and_shorten_columns(unsigned row);
|
||||
|
||||
void remove_pivot_column(unsigned row);
|
||||
|
||||
void update_active_pivots(unsigned row);
|
||||
|
||||
bool shorten_active_matrix(unsigned row, eta_matrix<T, X> *eta_matrix);
|
||||
|
||||
unsigned pivot_score_without_shortened_counters(unsigned i, unsigned j, unsigned k);
|
||||
#ifdef Z3DEBUG
|
||||
bool can_improve_score_for_row(unsigned row, unsigned score, T const & c_partial_pivoting, unsigned k);
|
||||
bool really_best_pivot(unsigned i, unsigned j, T const & c_partial_pivoting, unsigned k);
|
||||
void print_active_matrix(unsigned k, std::ostream & out);
|
||||
#endif
|
||||
bool pivot_queue_is_correct_for_row(unsigned i, unsigned k);
|
||||
|
||||
bool pivot_queue_is_correct_after_pivoting(int k);
|
||||
|
||||
bool get_pivot_for_column(unsigned &i, unsigned &j, int c_partial_pivoting, unsigned k);
|
||||
|
||||
bool elem_is_too_small(vector<indexed_value<T>> & row_chunk, indexed_value<T> & iv, int c_partial_pivoting);
|
||||
|
||||
unsigned number_of_non_zeroes_in_row(unsigned row) const {
|
||||
return static_cast<unsigned>(m_rows[row].size());
|
||||
}
|
||||
|
||||
unsigned number_of_non_zeroes_in_column(unsigned col) const {
|
||||
return m_columns[col].m_values.size();
|
||||
}
|
||||
|
||||
bool shorten_columns_by_pivot_row(unsigned i, unsigned pivot_column);
|
||||
|
||||
bool col_is_active(unsigned j, unsigned pivot) {
|
||||
return adjust_column_inverse(j) > pivot;
|
||||
}
|
||||
|
||||
bool row_is_active(unsigned i, unsigned pivot) {
|
||||
return adjust_row_inverse(i) > pivot;
|
||||
}
|
||||
|
||||
bool fill_eta_matrix(unsigned j, eta_matrix<T, X> ** eta);
|
||||
#ifdef Z3DEBUG
|
||||
bool is_upper_triangular_and_maximums_are_set_correctly_in_rows(lp_settings & settings) const;
|
||||
|
||||
bool is_upper_triangular_until(unsigned k) const;
|
||||
void check_column_vs_rows(unsigned col);
|
||||
|
||||
void check_row_vs_columns(unsigned row);
|
||||
|
||||
void check_rows_vs_columns();
|
||||
|
||||
void check_columns_vs_rows();
|
||||
|
||||
void check_matrix();
|
||||
#endif
|
||||
void create_graph_G(const vector<unsigned> & active_rows, vector<unsigned> & sorted_active_rows);
|
||||
void process_column_recursively(unsigned i, vector<unsigned> & sorted_rows);
|
||||
void extend_and_sort_active_rows(const vector<unsigned> & active_rows, vector<unsigned> & sorted_active_rows);
|
||||
void process_index_recursively_for_y_U(unsigned j, vector<unsigned> & sorted_rows);
|
||||
void resize(unsigned new_dim) {
|
||||
unsigned old_dim = dimension();
|
||||
lp_assert(new_dim >= old_dim);
|
||||
for (unsigned j = old_dim; j < new_dim; j++) {
|
||||
m_rows.push_back(vector<indexed_value<T>>());
|
||||
m_columns.push_back(col_header());
|
||||
}
|
||||
m_pivot_queue.resize(new_dim);
|
||||
m_row_permutation.resize(new_dim);
|
||||
m_column_permutation.resize(new_dim);
|
||||
m_work_pivot_vector.resize(new_dim);
|
||||
m_processed.resize(new_dim);
|
||||
for (unsigned j = old_dim; j < new_dim; j++) {
|
||||
add_new_element(j, j, numeric_traits<T>::one());
|
||||
}
|
||||
}
|
||||
#ifdef Z3DEBUG
|
||||
vector<T> get_full_row(unsigned i) const;
|
||||
#endif
|
||||
unsigned pivot_queue_size() const { return m_pivot_queue.size(); }
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -23,42 +23,19 @@ Revision History:
|
|||
#include <utility>
|
||||
#include "math/lp/static_matrix_def.h"
|
||||
#include "math/lp/lp_core_solver_base.h"
|
||||
#include "math/lp/lp_dual_core_solver.h"
|
||||
#include "math/lp/lp_dual_simplex.h"
|
||||
#include "math/lp/lp_primal_core_solver.h"
|
||||
#include "math/lp/scaler.h"
|
||||
#include "math/lp/lar_solver.h"
|
||||
namespace lp {
|
||||
template void static_matrix<double, double>::add_columns_at_the_end(unsigned int);
|
||||
template void static_matrix<double, double>::clear();
|
||||
#ifdef Z3DEBUG
|
||||
template bool static_matrix<double, double>::is_correct() const;
|
||||
#endif
|
||||
template void static_matrix<double, double>::copy_column_to_indexed_vector(unsigned int, indexed_vector<double>&) const;
|
||||
|
||||
template double static_matrix<double, double>::get_balance() const;
|
||||
template std::set<std::pair<unsigned, unsigned>> static_matrix<double, double>::get_domain();
|
||||
template std::set<std::pair<unsigned, unsigned>> lp::static_matrix<lp::mpq, lp::mpq>::get_domain();
|
||||
template std::set<std::pair<unsigned, unsigned>> lp::static_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::get_domain();
|
||||
template double static_matrix<double, double>::get_elem(unsigned int, unsigned int) const;
|
||||
template double static_matrix<double, double>::get_max_abs_in_column(unsigned int) const;
|
||||
template double static_matrix<double, double>::get_min_abs_in_column(unsigned int) const;
|
||||
template double static_matrix<double, double>::get_min_abs_in_row(unsigned int) const;
|
||||
template void static_matrix<double, double>::init_empty_matrix(unsigned int, unsigned int);
|
||||
template void static_matrix<double, double>::init_row_columns(unsigned int, unsigned int);
|
||||
template static_matrix<double, double>::ref & static_matrix<double, double>::ref::operator=(double const&);
|
||||
template void static_matrix<double, double>::set(unsigned int, unsigned int, double const&);
|
||||
template static_matrix<double, double>::static_matrix(unsigned int, unsigned int);
|
||||
template void static_matrix<mpq, mpq>::add_column_to_vector(mpq const&, unsigned int, mpq*) const;
|
||||
template void static_matrix<mpq, mpq>::add_columns_at_the_end(unsigned int);
|
||||
template bool static_matrix<mpq, mpq>::is_correct() const;
|
||||
template void static_matrix<mpq, mpq>::copy_column_to_indexed_vector(unsigned int, indexed_vector<mpq>&) const;
|
||||
|
||||
template mpq static_matrix<mpq, mpq>::get_balance() const;
|
||||
template mpq static_matrix<mpq, mpq>::get_elem(unsigned int, unsigned int) const;
|
||||
template mpq static_matrix<mpq, mpq>::get_max_abs_in_column(unsigned int) const;
|
||||
template mpq static_matrix<mpq, mpq>::get_max_abs_in_row(unsigned int) const;
|
||||
template double static_matrix<double, double>::get_max_abs_in_row(unsigned int) const;
|
||||
template mpq static_matrix<mpq, mpq>::get_min_abs_in_column(unsigned int) const;
|
||||
template mpq static_matrix<mpq, mpq>::get_min_abs_in_row(unsigned int) const;
|
||||
template void static_matrix<mpq, mpq>::init_row_columns(unsigned int, unsigned int);
|
||||
|
|
@ -69,13 +46,11 @@ template static_matrix<mpq, mpq>::static_matrix(unsigned int, unsigned int);
|
|||
#ifdef Z3DEBUG
|
||||
template bool static_matrix<mpq, numeric_pair<mpq> >::is_correct() const;
|
||||
#endif
|
||||
template void static_matrix<mpq, numeric_pair<mpq> >::copy_column_to_indexed_vector(unsigned int, indexed_vector<mpq>&) const;
|
||||
template mpq static_matrix<mpq, numeric_pair<mpq> >::get_elem(unsigned int, unsigned int) const;
|
||||
template void static_matrix<mpq, numeric_pair<mpq> >::init_empty_matrix(unsigned int, unsigned int);
|
||||
template void static_matrix<mpq, numeric_pair<mpq> >::set(unsigned int, unsigned int, mpq const&);
|
||||
|
||||
|
||||
template bool lp::static_matrix<double, double>::pivot_row_to_row_given_cell(unsigned int, column_cell &, unsigned int);
|
||||
template bool lp::static_matrix<lp::mpq, lp::mpq>::pivot_row_to_row_given_cell(unsigned int, column_cell& , unsigned int);
|
||||
template bool lp::static_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::pivot_row_to_row_given_cell(unsigned int, column_cell&, unsigned int);
|
||||
template void lp::static_matrix<lp::mpq, lp::numeric_pair<lp::mpq> >::remove_element(vector<lp::row_cell<lp::mpq>, true, unsigned int>&, lp::row_cell<lp::mpq>&);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ Author:
|
|||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include "math/lp/sparse_vector.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include "math/lp/permutation_matrix.h"
|
||||
#include <stack>
|
||||
|
|
@ -169,8 +168,6 @@ public:
|
|||
|
||||
std::set<std::pair<unsigned, unsigned>> get_domain();
|
||||
|
||||
void copy_column_to_indexed_vector(unsigned j, indexed_vector<T> & v) const;
|
||||
|
||||
T get_max_abs_in_row(unsigned row) const;
|
||||
void add_column_to_vector (const T & a, unsigned j, T * v) const {
|
||||
for (const auto & it : m_columns[j]) {
|
||||
|
|
@ -223,7 +220,7 @@ public:
|
|||
virtual void set_number_of_columns(unsigned /*n*/) { }
|
||||
#endif
|
||||
|
||||
T get_max_val_in_row(unsigned /* i */) const { lp_unreachable(); }
|
||||
T get_max_val_in_row(unsigned /* i */) const { UNREACHABLE(); }
|
||||
|
||||
T get_balance() const;
|
||||
|
||||
|
|
@ -344,7 +341,6 @@ public:
|
|||
void fill_last_row_with_pivoting(const term& row,
|
||||
unsigned bj, // the index of the basis column
|
||||
const vector<int> & basis_heading) {
|
||||
lp_assert(numeric_traits<T>::precise());
|
||||
lp_assert(row_count() > 0);
|
||||
m_work_vector.resize(column_count());
|
||||
T a;
|
||||
|
|
@ -360,7 +356,6 @@ public:
|
|||
for (auto p : row) {
|
||||
fill_last_row_with_pivoting_loop_block(p.column().index(), basis_heading);
|
||||
}
|
||||
lp_assert(m_work_vector.is_OK());
|
||||
unsigned last_row = row_count() - 1;
|
||||
|
||||
for (unsigned j : m_work_vector.m_index) {
|
||||
|
|
|
|||
|
|
@ -174,14 +174,6 @@ std::set<std::pair<unsigned, unsigned>> static_matrix<T, X>::get_domain() {
|
|||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename X> void static_matrix<T, X>::copy_column_to_indexed_vector (unsigned j, indexed_vector<T> & v) const {
|
||||
lp_assert(j < m_columns.size());
|
||||
for (auto & it : m_columns[j]) {
|
||||
const T& val = get_val(it);
|
||||
if (!is_zero(val))
|
||||
v.set_value(val, it.var());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename X> T static_matrix<T, X>::get_max_abs_in_row(unsigned row) const {
|
||||
T ret = numeric_traits<T>::zero();
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2017 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
<name>
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Lev Nachmanson (levnach)
|
||||
|
||||
Revision History:
|
||||
|
||||
|
||||
--*/
|
||||
|
||||
#pragma once
|
||||
#include "util/vector.h"
|
||||
#include "math/lp/indexed_vector.h"
|
||||
#include "math/lp/matrix.h"
|
||||
#include "math/lp/lp_settings.h"
|
||||
// These matrices appear at the end of the list
|
||||
|
||||
namespace lp {
|
||||
template <typename T, typename X>
|
||||
class tail_matrix
|
||||
#ifdef Z3DEBUG
|
||||
: public matrix<T, X>
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
virtual void apply_from_left_to_T(indexed_vector<T> & w, lp_settings & settings) = 0;
|
||||
virtual void apply_from_left(vector<X> & w, lp_settings & settings) = 0;
|
||||
virtual void apply_from_right(vector<T> & w) = 0;
|
||||
virtual void apply_from_right(indexed_vector<T> & w) = 0;
|
||||
virtual ~tail_matrix() = default;
|
||||
virtual bool is_dense() const = 0;
|
||||
struct ref_row {
|
||||
const tail_matrix & m_A;
|
||||
unsigned m_row;
|
||||
ref_row(const tail_matrix& m, unsigned row): m_A(m), m_row(row) {}
|
||||
T operator[](unsigned j) const { return m_A.get_elem(m_row, j);}
|
||||
};
|
||||
ref_row operator[](unsigned i) const { return ref_row(*this, i);}
|
||||
};
|
||||
}
|
||||
|
|
@ -311,7 +311,6 @@ struct expr2subpaving::imp {
|
|||
case OP_REM:
|
||||
case OP_IRRATIONAL_ALGEBRAIC_NUM:
|
||||
case OP_DIV0:
|
||||
case OP_REM0:
|
||||
case OP_MOD0:
|
||||
case OP_IDIV0:
|
||||
throw default_exception("you must apply arithmetic purifier before internalizing expressions into the subpaving module.");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue