-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (148 loc) · 4.57 KB
/
main.cpp
File metadata and controls
182 lines (148 loc) · 4.57 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
//
// Created by Anurag Bhattacharyya on 10/23/24.
//
//
// Created by Anurag Bhattacharyya on 10/23/24.
//
#include "src/gcmma.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
double Squared(double x) { return x*x; }
struct Problem {
int n, m;
std::vector<double> x0, xmin, xmax;
Problem()
: n(3)
, m(2)
, x0({4, 3, 2})
, xmin(n, 0.0)
, xmax(n, 5.0)
{ }
void Obj(const double *x, double *f0x, double *fx) {
f0x[0] = 0;
for (int i = 0; i < n; ++i) {
f0x[0] += x[i]*x[i];
}
fx[0] = Squared(x[0] - 5) + Squared(x[1] - 2) + Squared(x[2] - 1) - 9;
fx[1] = Squared(x[0] - 3) + Squared(x[1] - 4) + Squared(x[2] - 3) - 9;
}
void ObjSens(const double *x, double *f0x, double *fx, double *df0dx, double *dfdx) {
Obj(x, f0x, fx);
for (int i = 0; i < n; ++i) {
df0dx[i] = 2*x[i];
}
int k = 0;
dfdx[k++] = 2 * (x[0] - 5); dfdx[k++] = 2 * (x[0] - 3);
dfdx[k++] = 2 * (x[1] - 2); dfdx[k++] = 2 * (x[1] - 4);
dfdx[k++] = 2 * (x[2] - 1); dfdx[k++] = 2 * (x[2] - 3);
}
};
void Print(double *x, int n, const std::string &name = "x") {
std::cout << name << ":";
for (int i=0;i<n;i++) {
std::cout << " " << x[i];
}
std::cout << std::endl;
}
double VarChange() {
Problem toy;
double movlim = 0.2;
double f, fnew;
std::vector<double> df(toy.n);
std::vector<double> g(toy.m), gnew(toy.m);
std::vector<double> dg(toy.n * toy.m);
std::vector<double> x = toy.x0;
std::vector<double> xold = x;
std::vector<double> xnew(toy.n);
GCMMASolver gcmma(toy.n, toy.m, 0, 1000, 1);
double ch = 1.0;
int maxoutit = 8;
for (int iter = 0; ch > 0.0002 && iter < maxoutit; ++iter) {
toy.ObjSens(x.data(), &f, g.data(), df.data(), dg.data());
// Call the update method
// GCMMA version
gcmma.OuterUpdate(xnew.data(), x.data(), f, df.data(),
g.data(), dg.data(), toy.xmin.data(), toy.xmax.data());
// Check conservativity
toy.Obj(xnew.data(), &fnew, gnew.data());
bool conserv = gcmma.ConCheck(fnew, gnew.data());
//std::cout << conserv << std::endl;
for (int inneriter = 0; !conserv && inneriter < 15; ++inneriter) {
// Inner iteration update
gcmma.InnerUpdate(xnew.data(), fnew, gnew.data(), x.data(), f,
df.data(), g.data(), dg.data(), toy.xmin.data(), toy.xmax.data());
// Check conservativity
toy.Obj(xnew.data(), &fnew, gnew.data());
conserv = gcmma.ConCheck(fnew, gnew.data());
//std::cout << conserv << std::endl;
}
x = xnew;
// Compute infnorm on design change
ch = 0.0;
for (int i=0; i < toy.n; ++i) {
ch = std::max(ch, std::abs(x[i] - xold[i]));
xold[i] = x[i];
}
}
return ch;
}
int main(int argc, char *argv[]) {
std::cout << "///////////////////////////////////////////////////" << std::endl;
std::cout << "// Test the GCMMA algorithm" << std::endl;
std::cout << "///////////////////////////////////////////////////" << std::endl;
Problem toy;
double movlim = 0.2;
double f, fnew;
std::vector<double> df(toy.n);
std::vector<double> g(toy.m), gnew(toy.m);
std::vector<double> dg(toy.n * toy.m);
std::vector<double> x = toy.x0;
std::vector<double> xold = x;
std::vector<double> xnew(toy.n);
// Print initial values
toy.Obj(x.data(), &f, g.data());
std::cout << "f: " << f << std::endl;
Print(g.data(), toy.m, "g");
// Initialize GCMMA
GCMMASolver gcmma(toy.n, toy.m, 0, 1000, 1);
double ch = 1.0;
int maxoutit = 8;
for (int iter = 0; ch > 0.0002 && iter < maxoutit; ++iter) {
toy.ObjSens(x.data(), &f, g.data(), df.data(), dg.data());
// Call the update method
// GCMMA version
gcmma.OuterUpdate(xnew.data(), x.data(), f, df.data(),
g.data(), dg.data(), toy.xmin.data(), toy.xmax.data());
// Check conservativity
toy.Obj(xnew.data(), &fnew, gnew.data());
bool conserv = gcmma.ConCheck(fnew, gnew.data());
//std::cout << conserv << std::endl;
for (int inneriter = 0; !conserv && inneriter < 15; ++inneriter) {
// Inner iteration update
gcmma.InnerUpdate(xnew.data(), fnew, gnew.data(), x.data(), f,
df.data(), g.data(), dg.data(), toy.xmin.data(), toy.xmax.data());
// Check conservativity
toy.Obj(xnew.data(), &fnew, gnew.data());
conserv = gcmma.ConCheck(fnew, gnew.data());
//std::cout << conserv << std::endl;
}
x = xnew;
// Compute infnorm on design change
ch = 0.0;
for (int i=0; i < toy.n; ++i) {
ch = std::max(ch, std::abs(x[i] - xold[i]));
xold[i] = x[i];
}
// Print to screen
printf("it.: %d, obj.: %f, ch.: %f \n", iter, f, ch);
Print(x.data(), toy.n);
toy.Obj(x.data(), &f, g.data());
std::cout << "f: " << f << std::endl;
Print(g.data(), toy.m, "g");
std::cout << std::endl;
}
return 0;
}