-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
143 lines (130 loc) · 2.35 KB
/
solution.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Ten plik nie powinien byæ edytowany
#include"solution.h"
int solution::f_calls = 0;
int solution::g_calls = 0;
int solution::H_calls = 0;
void solution::clear_calls()
{
f_calls = 0;
g_calls = 0;
H_calls = 0;
}
solution::solution(double L)
{
x = L;
g = NAN;
H = NAN;
y = NAN;
ud = NAN;
flag = -1;
}
solution::solution(const matrix& A)
{
x = A;
g = NAN;
H = NAN;
y = NAN;
ud = NAN;
flag = -1;
}
solution::solution(int n, double* A)
{
try
{
x = matrix(n, A);
g = NAN;
H = NAN;
y = NAN;
ud = NAN;
flag = -1;
}
catch (string ex_info)
{
throw ("solution::solution(int,double*):\n" + ex_info);
}
}
solution::solution(const solution& A)
{
x = A.x;
g = A.g;
H = A.H;
y = A.y;
if (!isnan(A.ud(0, 0)))
ud = A.ud;
flag = A.flag;
}
solution& solution::operator=(const solution& A)
{
if (&A == this)
return *this;
x = A.x;
g = A.g;
H = A.H;
y = A.y;
if (!isnan(A.ud(0, 0)))
ud = A.ud;
flag = A.flag;
return *this;
}
matrix solution::fit_fun(matrix(*ff)(matrix, matrix, matrix), matrix ud1, matrix ud2) //oblicza wartosc funckji celu w danym punkcjie i zwiksza liczbe wywo³añ o 1
{
try
{
++f_calls;
y = ff(x, ud1, ud2);
return y;
}
catch (string ex_info)
{
throw ("matrix solution::fit_fun(...):\n" + ex_info);
}
}
matrix solution::grad(matrix(*gf)(matrix, matrix, matrix), matrix ud1, matrix ud2)
{
try
{
++g_calls;
g = gf(x, ud1, ud2);
return g;
}
catch (string ex_info)
{
throw ("matrix solution::grad(...):\n" + ex_info);
}
}
matrix solution::hess(matrix(*Hf)(matrix, matrix, matrix), matrix ud1, matrix ud2)
{
try
{
++H_calls;
H = Hf(x, ud1, ud2);
return H;
}
catch (string ex_info)
{
throw ("matrix solution::hess(...):\n" + ex_info);
}
}
ostream& operator<<(ostream& S, const solution& A)
{
S << "x = " << A.x << endl;
S << "y = " << A.y << endl;
S << "f_calls = " << solution::f_calls << endl;
if (solution::g_calls > 0)
S << "g_calls = " << solution::g_calls << endl;
if (solution::H_calls > 0)
S << "H_calls = " << solution::H_calls << endl;
S << "Exit flag: " << A.flag << endl;
return S;
}
int get_dim(const solution& A)
{
try
{
return get_len(A.x);
}
catch (string ex_info)
{
throw ("int get_dim(const solution&):\n" + ex_info);
}
}