Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
satisfy_inc.cpp
Go to the documentation of this file.
1/*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libvpsc - A solver for the problem of Variable Placement with
5 * Separation Constraints.
6 *
7 * Copyright (C) 2005-2008 Monash University
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library in the file LICENSE; if not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307 USA
23 *
24*/
25
26#include <libvpsc/rectangle.h>
27#include <libvpsc/variable.h>
28#include <libvpsc/constraint.h>
29#include <libvpsc/solve_VPSC.h>
30#include <algorithm>
31#include <cstdio>
32#include <ctime>
33#include <cmath>
34#include <cassert>
35
36using namespace std;
37using namespace vpsc;
38
39static inline double getRand(const int range) {
40 return (double)range*rand()/(RAND_MAX+1.0);
41}
42inline bool approxEquals(const double a, const double b) {
43 return fabs((double)a-b)<0.01;
44}
45typedef vector<Constraint*> CS;
46
47bool checkResult(unsigned n, Variable *a[], unsigned m, Constraint *c[], double expected[]=nullptr) {
48 std::vector<Variable*> aa(a,a+n);
49 std::vector<Constraint*> cc(c,c+m);
50 IncSolver vpsc(aa,cc);
51 vpsc.solve();
52#ifdef MOSEK_AVAILABLE
53 //printf("Checking with mosek...");
54 MosekEnv* menv = mosek_init_sep_ls(n,c,m);
55 float *b=new float[n];
56 float *x=new float[n];
57 for(unsigned i=0;i<n;i++) {
58 b[i]=a[i]->desiredPosition;
59 }
60 mosek_quad_solve_sep(menv,b,x);
61 mosek_delete(menv);
62#endif
63 for(unsigned i=0;i<n;i++) {
64 char s=',';
65 if(i==n-1) s='\n';
66#ifdef MOSEK_AVAILABLE
67 //printf("%f(%f)%c",a[i]->finalPosition,x[i],s);
68 if(!(approxEquals(a[i]->finalPosition,x[i]))) {
69 return false;
70 }
71 assert(approxEquals(a[i]->finalPosition,x[i]));
72#endif
73 if(expected) assert(approxEquals(a[i]->finalPosition,expected[i]));
74 }
75#ifdef MOSEK_AVAILABLE
76 delete [] b;
77 delete [] x;
78#endif
79 return true;
80}
81void dumpMatlabProblem(unsigned n, Variable *a[], unsigned m, Constraint *c[]){
82 printf("H=2*eye(%d);\n",n);
83 printf("f=-2*[ ");
84 for(unsigned i=0;i<n;i++) {
85 printf("%f ",a[i]->desiredPosition);
86 }
87 printf("];\n");
88 printf("s=[ ");
89 for(unsigned i=0;i<n;i++) {
90 printf("%f ",a[i]->scale);
91 }
92 printf("];\n");
93 printf("C=zeros(%d,%d);\n",m,n);
94 for(unsigned i=0;i<m;i++) {
95 printf("C(%d,[%d %d])=[1 -1];\n",i+1,c[i]->left->id+1,c[i]->right->id+1);
96 }
97 printf("A=C*diag(s);\n");
98 printf("b=[ ");
99 for(unsigned i=0;i<m;i++) {
100 printf("%f ",-1.*c[i]->gap);
101 }
102 printf("];\n");
103 printf("quadprog(H,f,A,b)\n");
104}
105void test1() {
106 cout << "Test 1..." << endl;
107 Variable *a[] = {
108 new Variable(0,2,1,1.),
109 new Variable(1,9,1,1),
110 new Variable(2,9,1,1),
111 new Variable(3,9,1,1),
112 new Variable(4,2,1,1)};
113 Constraint *c[] = {
114 new Constraint(a[0],a[4],3),
115 new Constraint(a[0],a[1],3),
116 new Constraint(a[1],a[2],3),
117 new Constraint(a[2],a[4],3),
118 new Constraint(a[3],a[4],3)};
119 unsigned int n = sizeof(a)/sizeof(Variable*);
120 unsigned int m = sizeof(c)/sizeof(Constraint*);
121 double expected[]={1.4,4.4,7.4,7.4,10.4};
122 checkResult(n,a,m,c,expected);
123 cout << "Test 1... done." << endl;
124}
125void test1a() {
126 cout << "Test 1a..." << endl;
127 /* matlab:
128 H=2*eye(2)
129 f=[0 0]
130 A=[ 2 -1 ]
131 b=[ -2 ]
132 quadprog(H,f,A,b)
133 */
134 Variable *a[] = {
135 new Variable(0,0,1,2.),
136 new Variable(1,0,1,1)};
137 Constraint *c[] = {
138 new Constraint(a[0],a[1],2)};
139 unsigned int n = sizeof(a)/sizeof(Variable*);
140 unsigned int m = sizeof(c)/sizeof(Constraint*);
141 double expected[]={-0.8,0.4};
142 checkResult(n,a,m,c,expected);
143 cout << "Test 1a... done." << endl;
144}
145void test1b() {
146 cout << "Test 1b..." << endl;
147 /* matlab:
148 H=2*eye(2)
149 f=[0 0]
150 A=[ 1 -2 ]
151 b=[ -2 ]
152 quadprog(H,f,A,b)
153 */
154 Variable *a[] = {
155 new Variable(0,0,1,1.),
156 new Variable(1,0,1,2)};
157 Constraint *c[] = {
158 new Constraint(a[0],a[1],2)};
159 unsigned int n = sizeof(a)/sizeof(Variable*);
160 unsigned int m = sizeof(c)/sizeof(Constraint*);
161 double expected[]={-0.4,0.8};
162 checkResult(n,a,m,c,expected);
163 cout << "Test 1b... done." << endl;
164}
165void test1c() {
166 cout << "Test 1c..." << endl;
167 /* matlab:
168 H=2*eye(3)
169 f=-2*[ 1 1 1 ]
170 s=[ 3 2 4 ]
171 C=zeros(2,3)
172 C(1,1:2)=[1 -1]
173 C(2,2:3)=[1 -1]
174 A=C*diag(s)
175 b=[-2 -2 ]
176 quadprog(H,f,A,b)
177 */
178 Variable *a[] = {
179 new Variable(0,1,1,3),
180 new Variable(1,1,1,2),
181 new Variable(2,1,1,4)};
182 Constraint *c[] = {
183 new Constraint(a[0],a[1],2),
184 new Constraint(a[1],a[2],2)};
185 unsigned int n = sizeof(a)/sizeof(Variable*);
186 unsigned int m = sizeof(c)/sizeof(Constraint*);
187 double expected[]={0.2623, 1.3934, 1.1967};
188 checkResult(n,a,m,c,expected);
189 cout << "Test 1c... done." << endl;
190}
191
192// no splits required
193void test2() {
194 cout << "Test 2..." << endl;
195 Variable *a[] = {
196 new Variable(0,4,1),
197 new Variable(1,6,1),
198 new Variable(2,9,1),
199 new Variable(3,2,1),
200 new Variable(4,5,1)};
201 Constraint *c[] = {
202 new Constraint(a[0],a[2],3),
203 new Constraint(a[0],a[3],3),
204 new Constraint(a[1],a[4],3),
205 new Constraint(a[2],a[4],3),
206 new Constraint(a[2],a[3],3),
207 new Constraint(a[3],a[4],3)};
208 double expected[]={0.5,6,3.5,6.5,9.5};
209 unsigned int n = sizeof(a)/sizeof(Variable*);
210 unsigned int m = sizeof(c)/sizeof(Constraint*);
211 checkResult(n,a,m,c,expected);
212 cout << "Test 2... done." << endl;
213}
214
215// split required
216void test3() {
217 /* matlab:
218 H=2*eye(5)
219 f=-2*[ 5 6 7 4 3 ]
220 s=[ 1 1 1 1 1 ]
221 C=zeros(5,5)
222 C(1,[1 5])=[1 -1]
223 C(2,[2 3])=[1 -1]
224 C(3,[3 4])=[1 -1]
225 C(4,[3 5])=[1 -1]
226 C(5,[4 5])=[1 -1]
227 A=C*diag(s)
228 b=-3*ones(5,1)
229 quadprog(H,f,A,b)
230 */
231 cout << "Test 3..." << endl;
232 Variable *a[] = {
233 new Variable(0,5,1),
234 new Variable(1,6,1),
235 new Variable(2,7,1),
236 new Variable(3,4,1),
237 new Variable(4,3,1)};
238 Constraint *c[] = {
239 new Constraint(a[0],a[4],3),
240 new Constraint(a[1],a[2],3),
241 new Constraint(a[2],a[3],3),
242 new Constraint(a[2],a[4],3),
243 new Constraint(a[3],a[4],3)};
244 double expected[]={5,0.5,3.5,6.5,9.5};
245 unsigned int n = sizeof(a)/sizeof(Variable*);
246 unsigned int m = sizeof(c)/sizeof(Constraint*);
247 checkResult(n,a,m,c,expected);
248 cout << "Test 3... done." << endl;
249}
250// split required
251void test4() {
252 /* matlab:
253 H=2*eye(5)
254 f=-2*[ 7 1 6 0 2 ]
255 s=[ 5 8 3 1 7 ]
256 C=zeros(6,5)
257 C(1,[1 4])=[1 -1]
258 C(2,[1 2])=[1 -1]
259 C(3,[2 5])=[1 -1]
260 C(4,[3 5])=[1 -1]
261 C(5,[3 4])=[1 -1]
262 C(6,[4 5])=[1 -1]
263 A=C*diag(s)
264 b=-3*ones(6,1)
265 quadprog(H,f,A,b)
266 */
267 cout << "Test 4..." << endl;
268 Variable *a[] = {
269 new Variable(0,7,1,1),
270 new Variable(1,1,1,1),
271 new Variable(2,6,1,1),
272 new Variable(3,0,1,1),
273 new Variable(4,2,1,1)};
274 Constraint *c[] = {
275 new Constraint(a[0],a[3],3),
276 new Constraint(a[0],a[1],3),
277 new Constraint(a[1],a[4],3),
278 new Constraint(a[2],a[4],3),
279 new Constraint(a[2],a[3],3),
280 new Constraint(a[3],a[4],3)};
281 double expected[]={0.8,3.8,0.8,3.8,6.8};
282 unsigned int n = sizeof(a)/sizeof(Variable*);
283 unsigned int m = sizeof(c)/sizeof(Constraint*);
284 checkResult(n,a,m,c,expected);
285 cout << "Test 4... done." << endl;
286}
287void test5() {
288 cout << "Test 5..." << endl;
289 Variable *a[] = {
290 new Variable(0,0,1), new Variable(1,9,1), new
291 Variable(2,1,1), new Variable(3,9,1), new
292 Variable(4,5,1), new Variable(5,1,1), new
293 Variable(6,2,1), new Variable(7,1,1), new
294 Variable(8,6,1), new Variable(9,3,1)};
295 Constraint *c[] = {
296 new Constraint(a[0],a[3],3), new Constraint(a[1],a[8],3),
297 new Constraint(a[1],a[6],3), new Constraint(a[2],a[6],3),
298 new Constraint(a[3],a[5],3), new Constraint(a[3],a[6],3),
299 new Constraint(a[3],a[7],3), new Constraint(a[4],a[8],3),
300 new Constraint(a[4],a[7],3), new Constraint(a[5],a[8],3),
301 new Constraint(a[5],a[7],3), new Constraint(a[5],a[8],3),
302 new Constraint(a[6],a[9],3), new Constraint(a[7],a[8],3),
303 new Constraint(a[7],a[9],3), new Constraint(a[8],a[9],3)
304 };
305 double expected[]={-3.71429,4,1,-0.714286,2.28571,2.28571,7,5.28571,8.28571,11.2857};
306 unsigned int n = sizeof(a)/sizeof(Variable*);
307 unsigned int m = sizeof(c)/sizeof(Constraint*);
308 checkResult(n,a,m,c,expected);
309 cout << "Test 5... done." << endl;
310}
311void test6() {
312 cout << "Test 6..." << endl;
313 Variable *a[] = {
314 new Variable(0,7,1),
315 new Variable(1,0,1),
316 new Variable(2,3,1),
317 new Variable(3,1,1),
318 new Variable(4,4,1)
319 };
320 Constraint *c[] = {
321 new Constraint(a[0],a[3],3),
322 new Constraint(a[0],a[2],3),
323 new Constraint(a[1],a[4],3),
324 new Constraint(a[1],a[4],3),
325 new Constraint(a[2],a[3],3),
326 new Constraint(a[3],a[4],3)
327 };
328 double expected[]={-0.75,0,2.25,5.25,8.25};
329 unsigned int n = sizeof(a)/sizeof(Variable*);
330 unsigned int m = sizeof(c)/sizeof(Constraint*);
331 checkResult(n,a,m,c,expected);
332 cout << "Test 6... done." << endl;
333}
334void test7() {
335 cout << "Test 7..." << endl;
336 Variable *a[] = {
337 new Variable(0,4,1),
338 new Variable(1,2,1),
339 new Variable(2,3,1),
340 new Variable(3,1,1),
341 new Variable(4,8,1)
342 };
343 Constraint *c[] = {
344 new Constraint(a[0],a[4],3),
345 new Constraint(a[0],a[2],3),
346 new Constraint(a[1],a[3],3),
347 new Constraint(a[2],a[3],3),
348 new Constraint(a[2],a[4],3),
349 new Constraint(a[3],a[4],3)
350 };
351 double expected[]={-0.5,2,2.5,5.5,8.5};
352 unsigned int n = sizeof(a)/sizeof(Variable*);
353 unsigned int m = sizeof(c)/sizeof(Constraint*);
354 checkResult(n,a,m,c,expected);
355 cout << "Test 7... done." << endl;
356}
357void test8() {
358 /* matlab:
359 H=2*eye(5)
360 f=-2*[ 3 4 0 5 6 ]
361 s=[ 1 1 1 1 1 ]
362 C=zeros(6,5)
363 C(1,[1 2])=[1 -1]
364 C(2,[1 3])=[1 -1]
365 C(3,[2 3])=[1 -1]
366 C(4,[2 5])=[1 -1]
367 C(5,[3 4])=[1 -1]
368 C(6,[4 5])=[1 -1]
369 A=C*diag(s)
370 b=-3*ones(6,1)
371 quadprog(H,f,A,b)
372 */
373 // This cycles when using the heuristic of merging on the least
374 // violated, violated constraint first.
375 cout << "Test 8..." << endl;
376 Variable *a[] = {
377 new Variable(0,3,1),
378 new Variable(1,4,1),
379 new Variable(2,0,1),
380 new Variable(3,5,1),
381 new Variable(4,6,1)
382 };
383 Constraint *c[] = {
384 new Constraint(a[0],a[1],3),
385 new Constraint(a[0],a[2],3),
386 new Constraint(a[1],a[2],3),
387 new Constraint(a[1],a[4],3),
388 new Constraint(a[2],a[3],3),
389 new Constraint(a[2],a[3],3),
390 new Constraint(a[3],a[4],3),
391 new Constraint(a[3],a[4],3)
392 };
393 double expected[]={-2.4,0.6,3.6,6.6,9.6};
394 unsigned int n = sizeof(a)/sizeof(Variable*);
395 unsigned int m = sizeof(c)/sizeof(Constraint*);
396 checkResult(n,a,m,c,expected);
397 cout << "Test 8... done." << endl;
398}
399void test9() {
400 /* matlab:
401 H=2*eye(5)
402 f=-2*[ 8 2 6 5 3 ]
403 s=[ 1 1 1 1 1 ]
404 C=zeros(7,5)
405 C(1,[1 5])=[1 -1]
406 C(2,[1 4])=[1 -1]
407 C(3,[2 3])=[1 -1]
408 C(4,[2 5])=[1 -1]
409 C(5,[3 4])=[1 -1]
410 C(6,[3 5])=[1 -1]
411 C(7,[4 5])=[1 -1]
412 A=C*diag(s)
413 b=-3*ones(7,1)
414 quadprog(H,f,A,b)
415 */
416 cout << "Test 9..." << endl;
417 Variable *a[] = {
418 new Variable(0,8,1),
419 new Variable(1,2,1),
420 new Variable(2,6,1),
421 new Variable(3,5,1),
422 new Variable(4,3,1)};
423 Constraint *c[] = {
424 new Constraint(a[0],a[4],3),
425 new Constraint(a[0],a[3],3),
426 new Constraint(a[1],a[2],3),
427 new Constraint(a[1],a[4],3),
428 new Constraint(a[2],a[3],3),
429 new Constraint(a[2],a[4],3),
430 new Constraint(a[3],a[4],3)};
431 double expected[]={3.6,0.6,3.6,6.6,9.6};
432 unsigned int n = sizeof(a)/sizeof(Variable*);
433 unsigned int m = sizeof(c)/sizeof(Constraint*);
434 checkResult(n,a,m,c,expected);
435 cout << "Test 9... done." << endl;
436}
437
438void test10() {
439 cout << "Test 10..." << endl;
440 Variable *a[] = {
441 new Variable(0,8.56215,1,4.99888),
442new Variable(1,1.27641,1,8.06009),
443new Variable(2,6.28523,1,1.06585),
444new Variable(3,4.09743,1,0.924166),
445new Variable(4,0.369025,1,6.12702)};
446
447 Constraint *c[] = {
448 new Constraint(a[0],a[2],3),
449new Constraint(a[0],a[1],3),
450new Constraint(a[0],a[1],3),
451new Constraint(a[1],a[3],3),
452new Constraint(a[1],a[3],3),
453new Constraint(a[1],a[2],3),
454new Constraint(a[2],a[4],3),
455new Constraint(a[2],a[4],3),
456new Constraint(a[3],a[4],3),
457new Constraint(a[3],a[4],3)};
458
459 //double expected[]={-1,2,5,5,8};
460 unsigned int n = sizeof(a)/sizeof(Variable*);
461 unsigned int m = sizeof(c)/sizeof(Constraint*);
462 std::vector<Variable*> aa(a,a+n);
463 std::vector<Constraint*> cc(c,c+m);
464 IncSolver vpsc(aa,cc);
465 vpsc.solve();
466 assert(checkResult(n,a,m,c,nullptr));
467 cout << "Test 10... done." << endl;
468}
469void test11() {
470 cout << "Test 11..." << endl;
471 Variable *a[] = {
472 new Variable(0,1.31591,1,9.02545),
473new Variable(1,1.48155,1,3.68918),
474new Variable(2,3.5091,1,2.07033),
475new Variable(3,3.47131,1,8.75145),
476new Variable(4,0.77374,1,0.967941)};
477
478 Constraint *c[] = {
479new Constraint(a[0],a[3],3),
480new Constraint(a[0],a[1],3),
481new Constraint(a[1],a[4],3),
482new Constraint(a[1],a[2],3),
483new Constraint(a[2],a[4],3),
484new Constraint(a[2],a[4],3),
485new Constraint(a[3],a[4],3),
486new Constraint(a[3],a[4],3)};
487
488 //double expected[]={-1,2,5,5,8};
489 unsigned int n = sizeof(a)/sizeof(Variable*);
490 unsigned int m = sizeof(c)/sizeof(Constraint*);
491 //dumpMatlabProblem(n,a,m,c);
492 std::vector<Variable*> aa(a,a+n);
493 std::vector<Constraint*> cc(c,c+m);
494 IncSolver vpsc(aa,cc);
495 vpsc.solve();
496 assert(checkResult(n,a,m,c,nullptr));
497 cout << "Test 11... done." << endl;
498}
499void test12() {
500 cout << "Test 12..." << endl;
501 Variable *a[] = {
502new Variable(0,2.83063,1,6.67901),
503new Variable(1,6.81696,1,7.28642),
504new Variable(2,9.27616,1,0.918345),
505new Variable(3,3.4094,1,3.39673),
506new Variable(4,2.92492,1,2.36269)};
507
508 Constraint *c[] = {
509new Constraint(a[0],a[3],3),
510new Constraint(a[0],a[2],3),
511new Constraint(a[0],a[1],3),
512new Constraint(a[1],a[4],3),
513new Constraint(a[1],a[4],3),
514new Constraint(a[1],a[4],3),
515new Constraint(a[2],a[4],3),
516new Constraint(a[2],a[4],3),
517new Constraint(a[3],a[4],3),
518new Constraint(a[3],a[4],3),
519new Constraint(a[3],a[4],3),
520new Constraint(a[3],a[4],3)};
521
522 //double expected[]={-1,2,5,5,8};
523 unsigned int n = sizeof(a)/sizeof(Variable*);
524 unsigned int m = sizeof(c)/sizeof(Constraint*);
525 //dumpMatlabProblem(n,a,m,c);
526 assert(checkResult(n,a,m,c,nullptr));
527 cout << "Test 12... done." << endl;
528}
529void test13() {
530 cout << "Test 13..." << endl;
531 Variable *a[] = {
532new Variable(0,0.485024,1,1),
533new Variable(1,3.52714,1,1),
534new Variable(2,4.01263,1,1),
535new Variable(3,4.58524,1,1),
536new Variable(4,5.40796,1,1)};
537
538 Constraint *c[] = {
539new Constraint(a[0],a[4],3),
540new Constraint(a[0],a[4],3),
541new Constraint(a[0],a[4],3),
542new Constraint(a[0],a[2],3),
543new Constraint(a[1],a[3],3),
544new Constraint(a[1],a[3],3),
545new Constraint(a[1],a[2],3),
546new Constraint(a[2],a[4],3),
547new Constraint(a[2],a[4],3),
548new Constraint(a[2],a[4],3),
549new Constraint(a[2],a[3],3),
550new Constraint(a[3],a[4],3),
551new Constraint(a[3],a[4],3),
552new Constraint(a[3],a[4],3)};
553
554 //double expected[]={-1,2,5,5,8};
555 unsigned int n = sizeof(a)/sizeof(Variable*);
556 unsigned int m = sizeof(c)/sizeof(Constraint*);
557 //dumpMatlabProblem(n,a,m,c);
558 assert(checkResult(n,a,m,c,nullptr));
559 cout << "Test 13... done." << endl;
560}
561
562// n=number vars
563// m=max constraints per var
564void rand_test(unsigned n, unsigned m) {
565 Variable **a=new Variable*[n];
566 CS cs;
567 for(unsigned i=0;i<n;i++) {
568 a[i]=new Variable(i,getRand(10),1,1);//getRand(10));
569 }
570 for(unsigned i=0;i<n-1;i++) {
571 for(int j=0;j<getRand(m)+1;j++) {
572 int e = static_cast<int>(i + getRand(n-1-i)+1);
573 cs.push_back(new Constraint(a[i],a[e],3));
574 }
575 }
576 Constraint **acs=new Constraint*[cs.size()];
577 for(unsigned i=0;i<cs.size();i++) {
578 acs[i]=cs[i];
579 }
580 try {
581 if(!checkResult(n,a,cs.size(),acs)) {
582 throw "Check failed!";
583 }
584 } catch (char const *msg) {
585 cout << msg << endl;
586 cout<<"digraph g {"<<endl;
587 for(CS::iterator i(cs.begin());i!=cs.end();i++) {
588 Constraint *c=*i;
589 cout << c->left->id << "->" << c->right->id << ";" << endl;
590 }
591 cout<<"}"<<endl;
592 for(unsigned i=0;i<n;i++) {
593 if(i!=0) cout << "," << endl;
594 cout << "new Variable("<<i<<","<<a[i]->desiredPosition<< ",1,"
595 << a[i]->scale <<")";
596 //cout << "a[i]->Pos="<<a[i]->position() << endl;
597 }
598 cout << "};" << endl;
599 for(CS::iterator i(cs.begin());i!=cs.end();i++) {
600 if(i!=cs.begin()) cout << "," << endl;
601 Constraint *c=*i;
602 //cout << c->left->id << "->" << c->right->id << ";" << endl;
603 cout << "new Constraint(a[" << c->left->id << "],a[" << c->right->id << "],3)";
604
605 }
606 cout << "};" << endl;
607 throw "test failed!";
608 }
609 /*
610 for(unsigned i=0;i<n;i++) {
611 a[i]->desiredPosition=getRand(10);
612 }
613 vpsc.solve();
614 try {
615 if(!checkResult(n,a,m,acs)) {
616 throw "2nd Check failed!";
617 }
618 } catch (char const *msg) {
619 cout << msg << endl;
620 for(unsigned i=0;i<n;i++) {
621 if(i!=0) cout << "," << endl;
622 cout << "new Variable("<<i<<","<<a[i]->desiredPosition<< ",1)";
623 //cout << "a[i]->Pos="<<a[i]->position() << endl;
624 }
625 cout << "};" << endl;
626 for(CS::iterator i(cs.begin());i!=cs.end();i++) {
627 if(i!=cs.begin()) cout << "," << endl;
628 Constraint *c=*i;
629 //cout << c->left->id << "->" << c->right->id << ";" << endl;
630 cout << "new Constraint(a[" << c->left->id << "],a[" << c->right->id << "],3)";
631
632 }
633 cout << "};" << endl;
634 throw "test failed!";
635 }
636 */
637 for_each(a,a+n,delete_object());
638 delete [] a;
639 for_each(cs.begin(),cs.end(),delete_object());
640 delete [] acs;
641}
642int main() {
643 srand(time(nullptr));
644 test10();
645 test2();
646 test3();
647 test4();
648 test5();
649 test6();
650 test7();
651 test8();
652 test9();
653 test10();
654 test11();
655 test12();
656 test13();
657 for(int i=0;i<1000;i++) {
658 if(i%100==0) cout << "i=" << i << endl;
659 rand_test(100,3);
660 }
661 for(int i=0;i<10000;i++) {
662 if(i%100==0) cout << "i=" << i << endl;
663 rand_test(5,3);
664 }
665 return 0;
666}
double scale
Definition aa.cpp:228
A constraint determines a minimum or exact spacing required between two Variable objects.
Definition constraint.h:45
Variable * left
The left Variable.
Definition constraint.h:102
Incremental solver for Variable Placement with Separation Constraints problem instance.
Definition solve_VPSC.h:105
A variable is comprised of an ideal position, final position and a weight.
Definition variable.h:45
double scale
Definition variable.h:56
double desiredPosition
Definition variable.h:52
Glib::ustring msg
double c[8][4]
STL namespace.
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
void test2()
bool approxEquals(const double a, const double b)
void test1()
void test8()
void test4()
vector< Constraint * > CS
void test9()
void dumpMatlabProblem(unsigned n, Variable *a[], unsigned m, Constraint *c[])
void test3()
void test1a()
void test5()
void test1c()
static double getRand(const int range)
void test7()
void test6()
bool checkResult(unsigned n, Variable *a[], unsigned m, Constraint *c[], double expected[]=nullptr)
void rand_test(unsigned n, unsigned m)
void test10()
void test12()
void test1b()
void test13()
void test11()
int main()