39static inline double getRand(
const int range) {
40 return (
double)range*rand()/(RAND_MAX+1.0);
43 return fabs((
double)a-b)<0.01;
45typedef vector<Constraint*>
CS;
48 std::vector<Variable*> aa(a,a+n);
49 std::vector<Constraint*> cc(
c,
c+m);
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++) {
60 mosek_quad_solve_sep(menv,b,x);
63 for(
unsigned i=0;i<n;i++) {
73 if(expected) assert(
approxEquals(a[i]->finalPosition,expected[i]));
82 printf(
"H=2*eye(%d);\n",n);
84 for(
unsigned i=0;i<n;i++) {
85 printf(
"%f ",a[i]->desiredPosition);
89 for(
unsigned i=0;i<n;i++) {
90 printf(
"%f ",a[i]->
scale);
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);
97 printf(
"A=C*diag(s);\n");
99 for(
unsigned i=0;i<m;i++) {
100 printf(
"%f ",-1.*
c[i]->gap);
103 printf(
"quadprog(H,f,A,b)\n");
106 cout <<
"Test 1..." << endl;
119 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
121 double expected[]={1.4,4.4,7.4,7.4,10.4};
123 cout <<
"Test 1... done." << endl;
126 cout <<
"Test 1a..." << endl;
139 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
141 double expected[]={-0.8,0.4};
143 cout <<
"Test 1a... done." << endl;
146 cout <<
"Test 1b..." << endl;
159 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
161 double expected[]={-0.4,0.8};
163 cout <<
"Test 1b... done." << endl;
166 cout <<
"Test 1c..." << endl;
185 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
187 double expected[]={0.2623, 1.3934, 1.1967};
189 cout <<
"Test 1c... done." << endl;
194 cout <<
"Test 2..." << endl;
208 double expected[]={0.5,6,3.5,6.5,9.5};
209 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
212 cout <<
"Test 2... done." << endl;
231 cout <<
"Test 3..." << endl;
244 double expected[]={5,0.5,3.5,6.5,9.5};
245 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
248 cout <<
"Test 3... done." << endl;
267 cout <<
"Test 4..." << endl;
281 double expected[]={0.8,3.8,0.8,3.8,6.8};
282 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
285 cout <<
"Test 4... done." << endl;
288 cout <<
"Test 5..." << endl;
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*);
309 cout <<
"Test 5... done." << endl;
312 cout <<
"Test 6..." << endl;
328 double expected[]={-0.75,0,2.25,5.25,8.25};
329 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
332 cout <<
"Test 6... done." << endl;
335 cout <<
"Test 7..." << endl;
351 double expected[]={-0.5,2,2.5,5.5,8.5};
352 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
355 cout <<
"Test 7... done." << endl;
375 cout <<
"Test 8..." << endl;
393 double expected[]={-2.4,0.6,3.6,6.6,9.6};
394 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
397 cout <<
"Test 8... done." << endl;
416 cout <<
"Test 9..." << endl;
431 double expected[]={3.6,0.6,3.6,6.6,9.6};
432 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
435 cout <<
"Test 9... done." << endl;
439 cout <<
"Test 10..." << endl;
460 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
462 std::vector<Variable*> aa(a,a+n);
463 std::vector<Constraint*> cc(
c,
c+m);
467 cout <<
"Test 10... done." << endl;
470 cout <<
"Test 11..." << endl;
489 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
492 std::vector<Variable*> aa(a,a+n);
493 std::vector<Constraint*> cc(
c,
c+m);
497 cout <<
"Test 11... done." << endl;
500 cout <<
"Test 12..." << endl;
523 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
527 cout <<
"Test 12... done." << endl;
530 cout <<
"Test 13..." << endl;
555 unsigned int n =
sizeof(a)/
sizeof(
Variable*);
559 cout <<
"Test 13... done." << endl;
567 for(
unsigned i=0;i<n;i++) {
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);
577 for(
unsigned i=0;i<cs.size();i++) {
582 throw "Check failed!";
584 }
catch (
char const *
msg) {
586 cout<<
"digraph g {"<<endl;
587 for(CS::iterator i(cs.begin());i!=cs.end();i++) {
589 cout <<
c->
left->
id <<
"->" <<
c->right->id <<
";" << endl;
592 for(
unsigned i=0;i<n;i++) {
593 if(i!=0) cout <<
"," << endl;
595 << a[i]->
scale <<
")";
598 cout <<
"};" << endl;
599 for(CS::iterator i(cs.begin());i!=cs.end();i++) {
600 if(i!=cs.begin()) cout <<
"," << endl;
603 cout <<
"new Constraint(a[" <<
c->
left->
id <<
"],a[" <<
c->right->id <<
"],3)";
606 cout <<
"};" << endl;
607 throw "test failed!";
643 srand(time(
nullptr));
657 for(
int i=0;i<1000;i++) {
658 if(i%100==0) cout <<
"i=" << i << endl;
661 for(
int i=0;i<10000;i++) {
662 if(i%100==0) cout <<
"i=" << i << endl;
A constraint determines a minimum or exact spacing required between two Variable objects.
Variable * left
The left Variable.
Incremental solver for Variable Placement with Separation Constraints problem instance.
A variable is comprised of an ideal position, final position and a weight.
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
bool approxEquals(const double a, const double b)
vector< Constraint * > CS
void dumpMatlabProblem(unsigned n, Variable *a[], unsigned m, Constraint *c[])
static double getRand(const int range)
bool checkResult(unsigned n, Variable *a[], unsigned m, Constraint *c[], double expected[]=nullptr)
void rand_test(unsigned n, unsigned m)