forked from p-col/Z3Ann
/
z3ann.cpp
116 lines (99 loc) · 3.66 KB
/
z3ann.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <vector>
#include "BackendC.hpp"
#include "z3ann.hpp"
const float Z3Equation::E = 2.71828182846f;
z3::expr ite(z3::expr const & c, z3::expr const & t, z3::expr const & e)
{
check_context(c, t); check_context(c, e);
assert(c.is_bool());
Z3_ast r = Z3_mk_ite(c.ctx(), c, t, e);
c.check_error();
return z3::expr(c.ctx(), r);
}
// it's supposed to be no error in this function
float &z3ann::getRealWeightFromName(const std::string &str)
{
int y = atoi(&(str[1]));
int x = 0;
size_t j;
j = str.find_first_of('_') + 1;
x = atoi(&str[j]);
return _realWeights[y][x];
}
bool Z3Equation::init(const trainingData &data)
{
_trainingData = data;
return true;
}
bool z3ann::loadTrainingData(const trainingData &data)
{
Z3Equation *newEquation;
newEquation = new Z3Equation(_inputSize, _hiddenSize, _outputSize,
++_equationId, _context, _min, _max,
_precision);
if (newEquation->init(data))
{
_equations.push_back(newEquation);
return true;
}
delete newEquation;
return false;
}
bool z3ann::loadTrainingDataSet(const std::vector<trainingData> &data)
{
std::vector<trainingData>::const_iterator it = data.begin();
std::vector<trainingData>::const_iterator end = data.end();
// _equations.resize(data.size());
for (; it != end; ++it)
if (!loadTrainingData(*it))
return false;
return true;
}
bool z3ann::setLink(int from, int to, bool enabled)
{
return _linkTable.setLink(from, to, enabled);
}
void z3ann::setAllLink(bool enabled)
{
_linkTable.setAll(enabled);
}
bool z3ann::solve(void)
{
std::vector<Z3Equation*>::iterator it;
bool retVal;
for (it = _equations.begin(); it != _equations.end(); ++it)
{
(*it)->generateEquations(_weights, _linkTable);// get equation
(*it)->assignInpoutOutputValues();
(*it)->addConstraintsToSolver(_solver);
}
if ((retVal = (_solver.check() == z3::sat)) == true)
{
std::cout << "=====================================" << std::endl;
std::cout << "============== SOLUTION =============" << std::endl;
std::cout << "=========== (on std error) ==========" << std::endl;
z3::model m = _solver.get_model();
for (unsigned i = 0; i < m.size(); i++) {
z3::func_decl v = m[i];
// this problem contains only constants
assert(v.arity() == 0);
// std::cout << v.name().str() << "=" << m.get_const_interp(v) << std::endl;
if (v.name().str()[0] == 'w')
{
getRealWeightFromName(v.name().str()) = Z3SolutionConverter::getFloatFromZ3Expr(m.get_const_interp(v));
std::cerr << v.name() << " = " <<
m.get_const_interp(v) << " = \033[1;32m" <<
Z3SolutionConverter::getFloatFromZ3Expr(m.get_const_interp(v)) << "\033[0m" << std::endl;
}
}
// std::cout << _solver.statistics() << std::endl;
_isSolved = true;
}
else
{
_isSolved = false;
std::cout << "Model unsat =(" << std::endl;
}
return retVal;
// std::cout << _solver.get_model() << std::endl;
}