-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bfgs.pde
57 lines (47 loc) · 1.65 KB
/
Bfgs.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//import lbfgsb.Minimizer;
class ContinuousDiff implements DifferentiableFunction {
Config cfg = null;
public ContinuousDiff(Config x) { cfg = x; }
public FunctionValues getValues(double[] point) {
Config temp = new Config(point, cfg.p);
double functionValue = func_eval(temp);
double[] gradient = func_diff(temp, 0.001);//func_diff_2(temp, 0.001);
return new FunctionValues(functionValue, gradient);
}
}
class BfgsListener implements IterationFinishedListener {
public boolean iterationFinished(double[] point, double functionValue, double[] gradient) {
//println(String.format("Listener iteration - fval: %g", functionValue));
return true; // continue algorithm
}
}
List<Bound> bounds = null;
Minimizer solver = new Minimizer();
public void init_bfgs_bounds(int n) {
bounds = new ArrayList<Bound>();
Bound bx = new Bound((double)c.xmin, (double)c.xmax);
Bound by = new Bound((double)c.ymin, (double)c.ymax);
for(int i=0; i<n; i++) {
if (i%2==0) {
bounds.add(bx);
} else {
bounds.add(by);
}
}
}
class BfgsSolver {
public double minimize(Config x) {
try {
// optimizes only rect centers, p - fixed
//mini.setNoBounds(n); // solve unbounded
solver.setBounds(bounds); // set bounds, should speed it up
//solver.setIterationFinishedListener(new BfgsListener());
Result result = solver.run(new ContinuousDiff(x), x.c);
x.c = result.point;
return result.functionValue;
} catch (LBFGSBException ex) {
println(ex);
}
return 0; // error
}
}