]> git.donarmstrong.com Git - lilypond.git/blobdiff - src/qlp.cc
release: 0.0.30
[lilypond.git] / src / qlp.cc
index 3dd8e4227e036810b539c717d699c3a83ce3fa1d..1e3e2f67d495cf59dade64ff5ee47ae4beda1571 100644 (file)
@@ -12,20 +12,21 @@ Mixed_qp::add_equality_cons(Vector , double )
 void
 Mixed_qp::add_fixed_var(int i, Real r)
 {
-    eq_cons.add(i);
-    eq_consrhs.add(r);
+    eq_cons.push(i);
+    eq_consrhs.push(r);
 }
 
 void
 Ineq_constrained_qp::add_inequality_cons(Vector c, double r)
 {
-    cons.add(c);
-    consrhs.add(r);
+    cons.push(c);
+    consrhs.push(r);
 }
 
 Ineq_constrained_qp::Ineq_constrained_qp(int novars):
     quad(novars),
-    lin(novars)
+    lin(novars),
+    const_term (0.0)
 {
 }
 
@@ -33,7 +34,7 @@ void
 Ineq_constrained_qp::OK() const
 {
 #ifndef NDEBUG    
-    assert(cons.sz() == consrhs.sz());
+    assert(cons.size() == consrhs.size());
     Matrix Qdif= quad - quad.transposed();
     assert(Qdif.norm()/quad.norm() < EPS);
 #endif    
@@ -45,25 +46,18 @@ Ineq_constrained_qp::eval (Vector v)
 {
     return v * quad * v + lin * v + const_term;
 }
-/*
-    eliminate appropriate variables, until we have a Ineq_constrained_qp
-    then solve that.
-
-    PRE
-    cons should be ascending
-    */
 Vector
 Mixed_qp::solve(Vector start) const 
 {
     print();
     Ineq_constrained_qp pure(*this);
     
-    for  (int i= eq_cons.sz()-1; i>=0; i--) {
+    for  (int i= eq_cons.size()-1; i>=0; i--) {
        pure.eliminate_var(eq_cons[i], eq_consrhs[i]);
        start.del(eq_cons[i]);
     }
     Vector sol = pure.solve(start);
-    for (int i= 0; i < eq_cons.sz(); i++) {
+    for (int i= 0; i < eq_cons.size(); i++) {
        sol.insert( eq_consrhs[i],eq_cons[i]);
     }
     return sol;
@@ -86,7 +80,7 @@ Ineq_constrained_qp::eliminate_var(int idx, Real value)
     row.del(idx);
     lin +=row ;
 
-   for (int i=0; i < cons.sz(); i++) {
+   for (int i=0; i < cons.size(); i++) {
       consrhs[i] -= cons[i](idx) *value;
       cons[i].del(idx);
    }
@@ -94,54 +88,67 @@ Ineq_constrained_qp::eliminate_var(int idx, Real value)
 
 
 
-
-Mixed_qp::Mixed_qp(int n)
-    : Ineq_constrained_qp(n)
-{
-}
-
 void
-Mixed_qp::OK() const
+Ineq_constrained_qp::assert_solution(Vector sol) const
 {
-#ifndef NDEBUG
-    Ineq_constrained_qp::OK();
-    assert(eq_consrhs.sz() == eq_cons.sz());
-#endif    
+    Array<int> binding;
+    for (int i=0; i < cons.size(); i++) {
+       Real R=cons[i] * sol- consrhs[i];
+       assert(R> -EPS);
+       if (R < EPS)
+           binding.push(i);
+    }
+    // KKT check...
+    // todo
 }
+
 void
 Ineq_constrained_qp::print() const
 {
 #ifndef NPRINT
     mtor << "Quad " << quad;
-    mtor << "lin " << lin <<"\n";
-    for (int i=0; i < cons.sz(); i++) {
+    mtor << "lin " << lin <<"\n"
+       << "const " << const_term<<"\n";
+    for (int i=0; i < cons.size(); i++) {
        mtor << "constraint["<<i<<"]: " << cons[i] << " >= " << consrhs[i];
        mtor << "\n";
     }
 #endif
 }
+
+/* *************** */
+
+/*
+    eliminate appropriate variables, until we have a Ineq_constrained_qp
+    then solve that.
+
+    PRE
+    cons should be ascending
+    */
+
+
+Mixed_qp::Mixed_qp(int n)
+    : Ineq_constrained_qp(n)
+{
+}
+
+void
+Mixed_qp::OK() const
+{
+#ifndef NDEBUG
+    Ineq_constrained_qp::OK();
+    assert(eq_consrhs.size() == eq_cons.size());
+#endif    
+}
+
 void
 Mixed_qp::print() const
 {
 #ifndef NPRINT
     Ineq_constrained_qp::print();
-    for (int i=0; i < eq_cons.sz(); i++) {
+    for (int i=0; i < eq_cons.size(); i++) {
        mtor << "eq cons "<<i<<": x["<<eq_cons[i]<<"] == " << eq_consrhs[i]<<"\n";
     }
 #endif
 }
 
-
-void
-Ineq_constrained_qp::assert_solution(Vector sol) const
-{
-    svec<int> binding;
-    for (int i=0; i < cons.sz(); i++) {
-       Real R=cons[i] * sol- consrhs[i];
-       assert(R> -EPS);
-       if (R < EPS)
-           binding.add(i);
-    }
-    // KKT check...
-    // todo
-}