]> git.donarmstrong.com Git - lilypond.git/blob - lily/qlp.cc
release: 0.1.1
[lilypond.git] / lily / qlp.cc
1 /*
2   qlp.cc -- implement Mixed_qp
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "debug.hh"
10 #include "qlp.hh"
11
12 void
13 Mixed_qp::add_equality_cons(Vector , double )
14 {
15     assert(false);
16 }
17
18 void
19 Mixed_qp::add_fixed_var(int i, Real r)
20 {
21     eq_cons.push(i);
22     eq_consrhs.push(r);
23 }
24
25
26 /**
27     eliminate appropriate variables, until we have a Ineq_constrained_qp
28     then solve that.
29
30     PRE
31     cons should be ascending
32     */
33 Vector
34 Mixed_qp::solve(Vector start) const 
35 {
36     if (!dim())
37         return Vector(0);
38     
39     print();
40     Ineq_constrained_qp pure(*this);
41     
42     for  (int i= eq_cons.size()-1; i>=0; i--) {
43         pure.eliminate_var(eq_cons[i], eq_consrhs[i]);
44         start.del(eq_cons[i]);
45     }
46     Vector sol = pure.solve(start);
47     for (int i= 0; i < eq_cons.size(); i++) {
48         sol.insert( eq_consrhs[i],eq_cons[i]);
49     }
50     return sol;
51 }
52
53
54 void
55 Ineq_constrained_qp::assert_solution(Vector sol) const
56 {
57     Array<int> binding;
58     for (int i=0; i < cons.size(); i++) {
59         Real R=cons[i] * sol- consrhs[i];
60         assert(R> -EPS);
61         if (R < EPS)
62             binding.push(i);
63     }
64     // KKT check...
65     // todo
66 }
67
68 void
69 Ineq_constrained_qp::print() const
70 {
71 #ifndef NPRINT
72     mtor << "Quad " << quad;
73     mtor << "lin " << lin <<"\n"
74         << "const " << const_term<<"\n";
75     for (int i=0; i < cons.size(); i++) {
76         mtor << "constraint["<<i<<"]: " << cons[i] << " >= " << consrhs[i];
77         mtor << "\n";
78     }
79 #endif
80 }
81
82 Mixed_qp::Mixed_qp(int n)
83     : Ineq_constrained_qp(n)
84 {
85 }
86
87 void
88 Mixed_qp::OK() const
89 {
90 #ifndef NDEBUG
91     Ineq_constrained_qp::OK();
92     assert(eq_consrhs.size() == eq_cons.size());
93 #endif    
94 }
95
96 void
97 Mixed_qp::print() const
98 {
99 #ifndef NPRINT
100     Ineq_constrained_qp::print();
101     for (int i=0; i < eq_cons.size(); i++) {
102         mtor << "eq cons "<<i<<": x["<<eq_cons[i]<<"] == " << eq_consrhs[i]<<"\n";
103     }
104 #endif
105 }
106