]> git.donarmstrong.com Git - lilypond.git/blob - qlpsolve.cc
release: 0.0.1
[lilypond.git] / qlpsolve.cc
1 #include "qlpsolve.hh"
2 #include "debug.hh"
3 #include "choleski.hh"
4
5 const Real TOL=1e-2;            // roughly 1/10 mm
6
7 String
8 Active_constraints::status() const
9 {
10     String s("Active|Inactive [");
11     for (int i=0; i< active.sz(); i++) {
12         s += String(active[i]) + " ";
13     }
14
15     s+="|";
16     for (int i=0; i< inactive.sz(); i++) {
17         s += String(inactive[i]) + " ";
18     }
19     s+="]";
20
21     return s;
22 }
23
24 void
25 Active_constraints::OK() {
26     H.OK();
27     A.OK();
28     assert(active.sz() +inactive.sz() == opt->cons.sz());
29     assert(H.dim() == opt->dim());
30     assert(active.sz() == A.rows());
31     svec<int> allcons;
32
33     for (int i=0; i < opt->cons.sz(); i++)
34         allcons.add(0);
35     for (int i=0; i < active.sz(); i++) {
36         int j = active[i];
37         allcons[j]++;
38     }
39     for (int i=0; i < inactive.sz(); i++) {
40         int j = inactive[i];
41         allcons[j]++;
42     }
43     for (int i=0; i < allcons.sz(); i++)
44         assert(allcons[i] == 1);
45 }
46
47 Vector
48 Active_constraints::get_lagrange(Vector gradient)
49 {
50     Vector l(A*gradient);
51
52     return l;
53 }
54
55 void
56 Active_constraints::add(int k)
57 {
58     // add indices
59     int cidx=inactive[k];
60     active.add(cidx);
61
62     inactive.swap(k,inactive.sz()-1);
63     inactive.pop();
64
65     Vector a( opt->cons[cidx] );
66     // update of matrices
67     Vector Ha = H*a;
68     Real aHa = a*Ha;
69
70     H -= Matrix(Ha , Ha)/(aHa);
71
72     Vector addrow(Ha/(aHa));
73     A -= Matrix(A*a, addrow);
74     A.insert_row(addrow,A.rows());        
75 }
76
77 void
78 Active_constraints::drop(int k)
79 {
80     int q=active.sz()-1;
81
82         // drop indices
83     inactive.add(active[k]);
84     active.swap(k,q);
85     A.swap_rows(k,q);
86     active.pop();
87
88     Vector a(A.row(q));
89     H += Matrix(a,a)/(a*opt->quad*a);
90     A -= A*opt->quad*Matrix(a,a)/(a*opt->quad*a);
91
92     Vector rem_row(A.row(q));
93     assert(rem_row.norm() < EPS);    
94     A.delete_row(q);
95 }
96
97
98 Active_constraints::Active_constraints(Ineq_constrained_qp const *op)
99     :       A(0,op->dim()),
100             H(op->dim()),
101             opt(op)
102 {
103     for (int i=0; i < op->cons.sz(); i++)
104         inactive.add(i);
105     Choleski_decomposition chol(op->quad);
106     H=chol.inverse();
107 }
108
109 /* Find the optimum which is in the planes generated by the active
110     constraints.        
111     */
112 Vector
113 Active_constraints::find_active_optimum(Vector g)
114 {
115     return H*g;
116 }
117
118 /****************************************************************/
119
120 int
121 min_elt_index(Vector v)
122 {
123     Real m=INFTY; int idx=-1;
124     for (int i = 0; i < v.dim(); i++)
125         if (v(i) < m) {
126             idx = i;
127             m = v(i);
128         }
129     return idx;
130 }
131
132 ///the numerical solving
133 Vector
134 Ineq_constrained_qp::solve(Vector start) const 
135 {    
136     Active_constraints act(this);
137
138
139     act.OK();    
140
141     
142     Vector x(start);
143     Vector gradient=quad*x+lin;
144
145
146     Vector last_gradient(gradient);
147     int iterations=0;
148     
149     while (iterations++ < MAXITER) {
150         Vector direction= - act.find_active_optimum(gradient);
151         
152         //      mtor << "gradient "<< gradient<< "\ndirection " << direction<<"\n";
153         
154         if (direction.norm() > EPS) {
155             //  mtor << act.status() << '\n';
156             
157             Real minalf = INFTY;
158
159             Inactive_iter minidx(act);
160
161
162             /*
163     we know the optimum on this "hyperplane". Check if we
164     bump into the edges of the simplex
165     */
166     
167             for (Inactive_iter ia(act); ia.ok(); ia++) {
168
169                 if (ia.vec() * direction >= 0)
170                     continue;
171                 Real alfa= - (ia.vec()*x - ia.rhs())/
172                     (ia.vec()*direction);
173                 
174                 if (minalf > alfa) {
175                     minidx = ia;
176                     minalf = alfa;
177                 }
178             }
179             Real unbounded_alfa = 1.0;
180             Real optimal_step = MIN(minalf, unbounded_alfa);
181
182             Vector deltax=direction * optimal_step;
183             x += deltax;            
184             gradient += optimal_step * (quad * deltax);
185             
186             //mtor << "step = " << optimal_step<< " (|dx| = " <<
187             //deltax.norm() << ")\n";       
188            
189             if (minalf < unbounded_alfa) {
190                 /* bumped into an edge. try again, in smaller space. */
191                 act.add(minidx.idx());
192                 continue;
193             }
194             /*ASSERT: we are at optimal solution for this "plane"*/
195     
196     
197         }
198         
199         Vector lagrange_mult=act.get_lagrange(gradient);        
200         int m= min_elt_index(lagrange_mult);
201         
202         if (m>=0 && lagrange_mult(m) > 0) {
203             break;              // optimal sol.
204         } else if (m<0 && gradient.norm() < EPS) {
205             break;
206         }
207         
208         mtor << "dropping cons " << m<<'\n';
209         act.drop(m);
210     }
211     if (iterations >= MAXITER)
212         WARN<<"didn't converge!\n";
213     
214     //    mtor <<  ": found " << x<<" in " << iterations <<" iterations\n";
215     assert_solution(x);
216     return x;
217
218
219 /** Mordecai Avriel, Nonlinear Programming: analysis and methods (1976)
220     Prentice Hall.
221
222     Section 13.3
223
224     This is a "projected gradient" algorithm. Starting
225     from a point x the next point is found in a direction determined by
226     projecting the gradient onto the active constraints.  */
227     
228 /*
229     thoroughly hacked to barely living tiny pieces by HW
230     */