-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminsnap.cpp
More file actions
176 lines (163 loc) · 4.82 KB
/
minsnap.cpp
File metadata and controls
176 lines (163 loc) · 4.82 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
#include <iostream>
#include <CGAL/basic.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <list>
#include <math.h>
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
typedef CGAL::Gmpz ET;
#else
#include <CGAL/MP_Float.h>
typedef CGAL::MP_Float ET;
#endif
// program and solution types
typedef CGAL::Quadratic_program_from_iterators
<int**, // for A
int*, // for b
CGAL::Const_oneset_iterator<CGAL::Comparison_result>, // for r
bool*, // for fl
int*, // for l
bool*, // for fu
int*, // for u
int**, // for D
int*> // for c
Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
const int m = 6;
const int t = 10;
void print(float matrix[(m+1)*(t-1)][(m+1)*(t-1)])
{
int i, j;
for (i = 0; i < (m+1)*(t-1); ++i)
{
for (j = 0; j < (m+1)*(t-1); ++j)
printf("%.0f ", matrix[i][j]);
printf("\n");
}
}
float** create2DArray(unsigned height, unsigned width, float array[][m+1][m+1], int length)
{
float** array2D = 0;
array2D = new float*[height];
for (int i = 0; i < length; i++)
{
for (int h = 0; h < m+1; h++)
{
array2D[i*(m+1)+h] = new float[width];
for (int w = 0; w < m+1; w++)
{
array2D[i*(m+1)+h][i*(m+1)+w] = array[i][h][w];
//printf("%1f ",array[i][h][w]);
}
}
}
return array2D;
}
// int * concat(int list1[], int list2[]){
// static int list[sizeof(list1)/sizeof(*list1) + sizeof(list2)/sizeof(*list2)];
// for (int counter = 0; counter < (sizeof(list)/sizeof(*list)); counter++)
// {
// if (counter < sizeof(list1)/sizeof(*list1))
// {
// list[counter] = list1[counter];
// }
// else
// {
// list[counter] = list2[counter-(sizeof(list1)/sizeof(*list1))];
// }
// }
// return list;
// }
int main() {
int C[m+1];
float pH[t-1][m+1][m+1];
float nH[t-1][m+1][m+1];
float H[t-1][m+1][m+1];
float T[t];
float Q[(m+1)*(t-1)][(m+1)*(t-1)];
for (int i=0; i < t; i++)
{
T[i] = (float)i/(float)t;
}
for (int q=0; q < t-1; q++)
{
for (int i=0; i <= m ; i++)
{
C[i] = i*(i-1)*(i-2)*(i-3);
}
for (int i=0; i <= m ; i++)
{
for (int z=0; z <= m; z++)
{
if (z > 3 && i > 3)
{
pH[q][i][z] = (1./(z-3+i-4))*(float)C[i]*(float)C[z]*pow(T[q+1],(z-3+i-4));
nH[q][i][z] = (1./(z-3+i-4))*(float)C[i]*(float)C[z]*pow(T[q],(z-3+i-4));
}
else
{
pH[q][i][z] = 0;
nH[q][i][z] = 0;
}
H[q][i][z] = pH[q][i][z] - nH[q][i][z];
}
}
}
float** my2DArray = create2DArray((m+1)*(t-1),(m+1)*(t-1),H,t-1);
//print(my2DArray);
for (int h = 0; h < (m+1)*(t-1); h++)
{
for (int w = 0; w < (m+1)*(t-1); w++)
{
Q[h][w] = 2*my2DArray[h][w];
}
}
print(Q);
// int *p = concat(list1,list2);
// int list[sizeof(list1)/sizeof(*list1) + sizeof(list2)/sizeof(*list2)];
// for (int counter = 0; counter < (sizeof(list)/sizeof(*list)); counter++)
// {
// list[counter] = *(p+counter);
// //std::cout << *(p+counter) <<std::endl;
// }
int* A[] = {}; // A comes columnwise
int b[] = {}; // right-hand side
CGAL::Const_oneset_iterator<CGAL::Comparison_result>
r( CGAL::SMALLER); // constraints are "<="
bool fl[(m+1)*(t-1)]; // both x, y are lower-bounded
for (int h = 0; h < (m+1)*(t-1); h++)
{
fl[h] = true;
}
int l[(m+1)*(t-1)];
for (int h = 0; h < (m+1)*(t-1); h++)
{
l[h] = 0;
}
bool fu[(m+1)*(t-1)]; // only y is upper-bounded
for (int h = 0; h < (m+1)*(t-1); h++)
{
fu[h] = true;
}
int u[(m+1)*(t-1)]; // x's u-entry is ignored
for (int h = 0; h < (m+1)*(t-1); h++)
{
u[h] = 1000000;
}
int* D[(m+1)*(t-1)]; // D-entries on/below diagonal
for (int h = 0; h < (m+1)*(t-1); h++)
{
D[h] = (int*)Q[h];
}
int c[] = {};
int c0 = 0; // constant term
// now construct the quadratic program; the first two parameters are
// the number of variables and the number of constraints (rows of A)
Program qp ((m+1)*(t-1), 0, A, b, r, fl, l, fu, u, D, c, c0);
// solve the program, using ET as the exact type
Solution s = CGAL::solve_quadratic_program(qp, ET());
// output solution
std::cout << s;
return 0;
}