-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathincrease.cpp
221 lines (198 loc) · 6.36 KB
/
increase.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "increase.h"
/*
Increase functions
*/
Increase_Type increase_function = Max_Linear_incr;
/*
Generic functions
*/
void increase(const double& K_0, const double& K_1, const double& alpha, double& new_K_0, double& new_K_1){
switch (increase_function)
{
case Linear_incr:
/* code */
linear_increase(K_0, K_1, alpha, new_K_0, new_K_1);
break;
case Max_Linear_incr:
max_linear_increase(K_0, K_1, alpha, new_K_0, new_K_1);
break;
default:
break;
}
}
void increase(const double &K_0, const double &K_1, const double &K_2, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2){
switch (increase_function)
{
case Linear_incr:
/* code */
linear_increase(K_0, K_1, K_2, alpha, new_K_0, new_K_1, new_K_2);
break;
case Max_Linear_incr:
max_linear_increase(K_0, K_1, K_2, alpha, new_K_0, new_K_1, new_K_2);
break;
default:
break;
}
}
void increase(const double &K_0, const double &K_1, const double &K_2, const double &K_3, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2, double &new_K_3){
switch (increase_function)
{
case Linear_incr:
/* code */
linear_increase(K_0, K_1, K_2, K_3, alpha, new_K_0, new_K_1, new_K_2, new_K_3);
break;
case Max_Linear_incr:
max_linear_increase(K_0, K_1, K_2, K_3, alpha, new_K_0, new_K_1, new_K_2, new_K_3);
break;
default:
break;
}
}
/*
Linear increase functions
*/
void linear_increase(const double& K_0, const double& K_1, const double& alpha, double& new_K_0, double& new_K_1){
new_K_0 = alpha*K_0;
new_K_1 = alpha*K_1;
}
void linear_increase(const double &K_0, const double &K_1, const double &K_2, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2){
new_K_0 = alpha*K_0;
new_K_1 = alpha*K_1;
new_K_2 = alpha*K_2;
}
void linear_increase(const double &K_0, const double &K_1, const double &K_2, const double &K_3, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2, double &new_K_3){
new_K_0 = alpha*K_0;
new_K_1 = alpha*K_1;
new_K_2 = alpha*K_2;
new_K_3 = alpha*K_3;
}
/*
Max-Linear increase functions
*/
void max_linear_increase_ordered(const std::vector<std::pair<double, int>> &K, const double &alpha, std::vector<std::pair<double, int>> &new_K)
{
// Suppose that the curvatures K are increasingly ordered by absolute value.
double alpha_left = alpha;
new_K.clear();
for (auto it = K.begin(); it != K.end(); it++)
{
new_K.push_back(std::pair<double, int>((*it).first, (*it).second));
}
bool stop = false;
for (int i = 0; i < new_K.size() - 1; i++)
{
if (!stop)
{
double big_K = abs(new_K[i + 1].first);
double small_K = abs(new_K[i].first);
double alpha_max = big_K / small_K;
if (alpha_max > alpha_left)
{ // we won't be able to continue
stop = true;
double K_limit = small_K * alpha_left;
for (int j = 0; j <= i; j++)
{
new_K[i].first = sign(new_K[i].first) * K_limit;
}
}
else
{
alpha_left = alpha_left / alpha_max;
for (int j = 0; j <= i; j++)
{
new_K[i].first = sign(new_K[i].first) * big_K;
}
}
}
}
if (!stop)
{
double K_limit = abs(new_K[0].first) * alpha_left;
for (int i = 0; i < new_K.size(); i++)
{
new_K[i].first = sign(new_K[i].first) * K_limit;
}
}
}
bool personal_compare(std::pair<double, int> K1, std::pair<double, int> K2)
{
return abs(K1.first) < abs(K2.first);
}
void max_linear_increase(const double &K_0, const double &K_1, const double &alpha, double &new_K_0, double &new_K_1)
{
// increases linearly until it reaches the maximum of both curvature
std::vector<std::pair<double, int>> K, new_K;
K.push_back(std::pair<double, int>(K_0, 0));
K.push_back(std::pair<double, int>(K_1, 1));
sort(K.begin(), K.end(), personal_compare);
max_linear_increase_ordered(K, alpha, new_K);
for (int i = 0; i < new_K.size(); i++)
{
auto n_K = new_K[i];
if (n_K.second == 0)
{
new_K_0 = n_K.first;
}
else if (n_K.second == 1)
{
new_K_1 = n_K.first;
}
}
}
void max_linear_increase(const double &K_0, const double &K_1, const double &K_2, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2)
{
// increases linearly until it reaches the maximum of both curvature
std::vector<std::pair<double, int>> K, new_K;
K.push_back(std::pair<double, int>(K_0, 0));
K.push_back(std::pair<double, int>(K_1, 1));
K.push_back(std::pair<double, int>(K_2, 2));
sort(K.begin(), K.end(), personal_compare);
max_linear_increase_ordered(K, alpha, new_K);
for (int i = 0; i < new_K.size(); i++)
{
auto n_K = new_K[i];
if (n_K.second == 0)
{
new_K_0 = n_K.first;
}
else if (n_K.second == 1)
{
new_K_1 = n_K.first;
}
else if (n_K.second == 2)
{
new_K_2 = n_K.first;
}
}
}
void max_linear_increase(const double &K_0, const double &K_1, const double &K_2, const double &K_3, const double &alpha, double &new_K_0, double &new_K_1, double &new_K_2, double &new_K_3)
{
// increases linearly until it reaches the maximum of both curvature
std::vector<std::pair<double, int>> K, new_K;
K.push_back(std::pair<double, int>(K_0, 0));
K.push_back(std::pair<double, int>(K_1, 1));
K.push_back(std::pair<double, int>(K_2, 2));
K.push_back(std::pair<double, int>(K_3, 3));
sort(K.begin(), K.end(), personal_compare);
max_linear_increase_ordered(K, alpha, new_K);
for (int i = 0; i < new_K.size(); i++)
{
auto n_K = new_K[i];
if (n_K.second == 0)
{
new_K_0 = n_K.first;
}
else if (n_K.second == 1)
{
new_K_1 = n_K.first;
}
else if (n_K.second == 2)
{
new_K_2 = n_K.first;
}
else if (n_K.second == 3)
{
new_K_3 = n_K.first;
}
}
}