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