#define DSTREAM_HH
#include "string.hh"
+#include "assoc.hh"
const char eol= '\n';
/// debug stream
-class dstream
+class Dstream
{
ostream *os;
int indentlvl;
-
+ Assoc<String, bool> silent;
+ bool local_silence;
+ String classname;
/// indent of each level
const INDTAB = 3;
+
public:
- dstream(ostream &r){
- os = &r;
- indentlvl = 0;
- }
- dstream &operator << (String s);
+ Dstream(ostream &r, const char * = 0);
+ Dstream &identify_as(String s);
+ void switch_output(String s,bool);
+ Dstream &operator << (String s);
};
/**
a class for providing debug output of nested structures,
- with indents according to \{\}()[]
- */
+ with indents according to \{\}()[].
+
+ Using identify_as() one can turn on and off specific messages. Init
+ for these can be given in a rc file
+
+ */
#endif
+
s += String(active[i]) + " ";
}
- s+="|";
+ s+="| ";
for (int i=0; i< inactive.sz(); i++) {
s += String(inactive[i]) + " ";
}
// update of matrices
Vector Ha = H*a;
Real aHa = a*Ha;
+ if (ABS(aHa) > EPS) {
+ /*
+ a != 0, so if Ha = O(EPS), then
+ Ha * aH / aHa = O(EPS^2/EPS)
+
+ if H*a == 0, the constraints are dependent.
+ */
+ H -= Matrix(Ha , Ha)/(aHa);
+
- H -= Matrix(Ha , Ha)/(aHa);
-
- Vector addrow(Ha/(aHa));
- A -= Matrix(A*a, addrow);
- A.insert_row(addrow,A.rows());
+ /*
+ sorry, don't know how to justify this. ..
+ */
+ Vector addrow(Ha/(aHa));
+ A -= Matrix(A*a, addrow);
+ A.insert_row(addrow,A.rows());
+ }else
+ WARN << "degenerate constraints";
}
void
active.pop();
Vector a(A.row(q));
- H += Matrix(a,a)/(a*opt->quad*a);
- A -= A*opt->quad*Matrix(a,a)/(a*opt->quad*a);
-
+ if (a.norm() > EPS) {
+ /*
+
+ */
+ H += Matrix(a,a)/(a*opt->quad*a);
+ A -= A*opt->quad*Matrix(a,a)/(a*opt->quad*a);
+ }else
+ WARN << "degenerate constraints";
Vector rem_row(A.row(q));
assert(rem_row.norm() < EPS);
A.delete_row(q);
min_elt_index(Vector v)
{
Real m=INFTY; int idx=-1;
- for (int i = 0; i < v.dim(); i++)
+ for (int i = 0; i < v.dim(); i++){
if (v(i) < m) {
idx = i;
m = v(i);
}
+ assert(v(i) <= INFTY);
+ }
return idx;
}
while (iterations++ < MAXITER) {
Vector direction= - act.find_active_optimum(gradient);
- // mtor << "gradient "<< gradient<< "\ndirection " << direction<<"\n";
+ mtor << "gradient "<< gradient<< "\ndirection " << direction<<"\n";
if (direction.norm() > EPS) {
- // mtor << act.status() << '\n';
+ mtor << act.status() << '\n';
Real minalf = INFTY;
x += deltax;
gradient += optimal_step * (quad * deltax);
- //mtor << "step = " << optimal_step<< " (|dx| = " <<
- //deltax.norm() << ")\n";
+ mtor << "step = " << optimal_step<< " (|dx| = " <<
+ deltax.norm() << ")\n";
if (minalf < unbounded_alfa) {
/* bumped into an edge. try again, in smaller space. */
act.add(minidx.idx());
+ mtor << "adding cons "<< minidx.idx()<<'\n';
continue;
}
/*ASSERT: we are at optimal solution for this "plane"*/
if (m>=0 && lagrange_mult(m) > 0) {
break; // optimal sol.
- } else if (m<0 && gradient.norm() < EPS) {
+ } else if (m<0) {
+ assert(gradient.norm() < EPS) ;
+
break;
}
if (iterations >= MAXITER)
WARN<<"didn't converge!\n";
- // mtor << ": found " << x<<" in " << iterations <<" iterations\n";
+ mtor << ": found " << x<<" in " << iterations <<" iterations\n";
assert_solution(x);
return x;
}
Section 13.3
- This is a "projected gradient" algorithm. Starting
- from a point x the next point is found in a direction determined by
- projecting the gradient onto the active constraints. */
-
-/*
- thoroughly hacked to barely living tiny pieces by HW
+ This is a "projected gradient" algorithm. Starting from a point x
+ the next point is found in a direction determined by projecting
+ the gradient onto the active constraints. (well, not really the
+ gradient. The optimal solution obeying the active constraints is
+ tried. This is why H = Q^-1 in initialisation) )
+
+
*/
+