-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.cpp
More file actions
195 lines (174 loc) · 4.61 KB
/
data.cpp
File metadata and controls
195 lines (174 loc) · 4.61 KB
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
#include "data.h"
#define N 10
using std::string;
inline bool isNumber(char ch) {
if (ch <= '9' && ch >= '0') return true;
return false;
}
//gets coefficients from string
void Data::getCoefficients(string func, vector<float>& coeff)
{
char ch = 0;
float curr = 0;
unsigned length = func.length();
bool isNeg = false;
for (unsigned i = 0; i < length; i++) {
if (func[i] == 'x' && !isNumber(func[i + 1]))continue;
if (func[i] == '-') isNeg = true;
if (isNumber(func[i]) || func[i] == 'x') {
string coef;
if (func[i] == 'x') coef = "1";
else {
coef += func[i];
while (func[++i] != 'x') {
coef += func[i];
}
}
if (isNeg) {
coef = "-" + coef;
isNeg = false;
}
string num;
num += func[++i];
while (isNumber(func[++i])) {
num += func[i];
}
int curVar = stoi(num);
while (coeff.size() + 1 < curVar) {
coeff.push_back(0.0);
}
curr = stof(coef);
maxCoef = (curr > maxCoef) ? curr : maxCoef;
coeff.push_back(curr);
}
}
while (coefficients.size() > coeff.size()) coeff.push_back(0.0);
}
//reads information from followed stream
// information must be structured in such form:
// function f(x) = ax1 + bx2 + cx3 + ... , where a, b, c... - function's coefficients
// mode (max or min)
// system of restrictions a11x1 + a12x2 + ... + a1nxn <= b1
// a21x1 + a22x2 + ... + a2nxn <= b1
// .................................
// am1x1 + am2x2 + ... + amnxn <= bm
//
// END
// instead of "<=" can be used ">=" or "="
void Data::readUserData(std::istream& in){
//
string function;
getline(in, function);
getCoefficients(function, coefficients);
string mode;
getline(in, mode);
if (mode == "min") isMaximization = false;
else if (mode == "max") isMaximization = true;
string st = "";
getline(in, st);
//reading system of restrictions
while (!(in.eof() || st == "END")) {
string num;
int i = st.length();
//the free member separation
while (isNumber(st[--i]) || st[i] == '.');
num = st.substr(i + 1, st.length());
float n = stof(num);
while (st[i] != '=') {
if (st[i] == '-') n = -n;
i--;
}
freeMembers.push_back(n);
//processing sign
if (st[i - 1] == '<') signOfRestrictions.push_back(1);
else if (st[i - 1] == '>') signOfRestrictions.push_back(-1);
else signOfRestrictions.push_back(0);
vector<float> vect;
getCoefficients(st.substr(0, i), vect);
system.push_back(vect);
getline(in, st);
}
maxCoef *= 2;
convertation();
}
//converts information after reading into standart form
void Data::convertation(){
// standart mode - minimization
if (isMaximization) {
isMaximization = false;
int s = coefficients.size();
for (int i = 0; i < s; i++) {
coefficients[i] = -coefficients[i];
}
}
//standart sign - " <="
//also here an artificial base is created
int size = signOfRestrictions.size();
for (int i = 0; i < size; i++) {
if (signOfRestrictions[i] == 0) {
coefficients.push_back(maxCoef);
base.push_back(i);
int s = system.size();
for (int j = 0; j < s; j++) {
if (j != i)system[j].push_back(0);
else {
system[j].push_back(1);
base.push_back(coefficients.size());
}
}
}
else if (signOfRestrictions[i] > 0) {
coefficients.push_back(0);
coefficients.push_back(maxCoef);
int s = system.size();
for (int j = 0; j < s; j++) {
if (j != i) {
system[j].push_back(0);
system[j].push_back(0);
}
else {
system[j].push_back(1);
system[j].push_back(1);
base.push_back(coefficients.size());
}
}
}
else {
coefficients.push_back(0);
coefficients.push_back(maxCoef);
int s = system.size();
for (int j = 0; j < s; j++) {
if (j != i) {
system[j].push_back(0);
system[j].push_back(0);
}
else {
system[j].push_back(-1);
system[j].push_back(1);
base.push_back(coefficients.size());
}
}
}
}
//we need the sum of elements in each column
int s = coefficients.size();
float free = 0; //the sum of free members
for (int i = 0; i < freeMembers.size(); i++)
free += freeMembers[i];
float* sum = new float[s];
for (int i = 0; i < s; i++)
sum[i] = 0;
for (int i = 0; i < system.size(); i++) {
for (int j = 0; j < s; j++) {
sum[j] += system[i][j];
}
}
// after creating an artificial base we need to multiply coefficients
for (int i = 0; i < s; i++)
sum[i] *= maxCoef;
free *= maxCoef;
for (int i = 0; i < s; i++)
coefficients[i] -= sum[i];
max = -free; // the function value in this particular point
MFT.assign(system.size(), 0.0); // vector of simplex coefficients
}