diff --git a/src/cad/SolveServer.java b/src/cad/SolveServer.java index 2384fe69..4e1fb23f 100644 --- a/src/cad/SolveServer.java +++ b/src/cad/SolveServer.java @@ -118,7 +118,7 @@ class SolveHandler extends AbstractHandler { } for (int i = 0; i < locked.length(); i++) { - paramsDict.get(locked.getInt(i)).setLocked(true); +// paramsDict.get(locked.getInt(i)).setLocked(true); } Solver.SubSystem subSystem = new Solver.SubSystem(constraints); diff --git a/src/cad/gcs/GlobalSolver.java b/src/cad/gcs/GlobalSolver.java index 61c38eae..098d6997 100644 --- a/src/cad/gcs/GlobalSolver.java +++ b/src/cad/gcs/GlobalSolver.java @@ -31,8 +31,9 @@ public class GlobalSolver { java.lang.System.out.println("Solve system with error: " + subSystem.errorSquared()); boolean triedShrink = false; while (abs(subSystem.value()) > eps && !triedShrink) { - solveLM_COMMONS(subSystem); +// solveLM_COMMONS(subSystem); // Solver.solve_LM(subSystem); + Solver.solve_BFGS(subSystem, false); if (abs(subSystem.value()) > eps) { // solveWorse(subSystem, eps); if(subSystem.constraints.size() > 1) { diff --git a/src/cad/gcs/Solver.java b/src/cad/gcs/Solver.java index c2520fc0..8aac7824 100644 --- a/src/cad/gcs/Solver.java +++ b/src/cad/gcs/Solver.java @@ -625,7 +625,7 @@ public class Solver { lineSearch(subsys, xdir); double err = subsys.errorSquared(); - h = new Array2DRowRealMatrix(x.getData()); + h = x.copy(); subsys.fillParams(x); h = x.subtract(h); // = x - xold @@ -643,7 +643,7 @@ public class Solver { break; } - y = new Array2DRowRealMatrix(grad.getData()); + y = grad.copy(); subsys.calcGrad(grad); y = grad.subtract(y); // = grad - gradold @@ -669,7 +669,7 @@ public class Solver { lineSearch(subsys, xdir); err = subsys.errorSquared(); - h = new Array2DRowRealMatrix(x.getData()); + h = x.copy(); subsys.fillParams(x); h = x.subtract(h); // = x - xold } @@ -862,8 +862,28 @@ public class Solver { return valueSquared(); } + public double[] calcGrad() { + double[] grad = new double[params.size()]; + for (Constraint c : constraints) { + double error = c.error(); + + double[] localGrad = new double[c.pSize()]; + c.gradient(localGrad); + + Param[] localParams = c.getParams(); + for (int i = 0; i < localParams.length; i++) { + grad[params.get(localParams[i]).id] += error * localGrad[i]; + } + } + return grad; + } + public void calcGrad(RealMatrix out) { - throw new UnsupportedOperationException("men at work"); + double[] grad = calcGrad(); + for (int i = 0; i < grad.length; i++) { + double v = calcGrad()[i]; + out.setEntry(i, 0, v); + } } } diff --git a/src/cad/gcs/Solver2.java b/src/cad/gcs/Solver2.java new file mode 100644 index 00000000..530b7eb6 --- /dev/null +++ b/src/cad/gcs/Solver2.java @@ -0,0 +1,541 @@ +package cad.gcs; + +import java.util.List; + +public class Solver2 { + + double pertMag = 1e-6; + double pertMin = 1e-10; + double XconvergenceRough = 1e-8; + double XconvergenceFine = 1e-10; + double smallF = 1e-20; + double validSolutionFine = 1e-12; + double validSoltuionRough = 1e-4; + double rough = 0; + double fine = 1; + double MaxIterations = 50 ; + + int succsess = 0; + int noSolution = 1; + +// int solve(double x, int xLength, List cons, int isFine) +// { +// std::stringstream cstr; +// double convergence,pert ; +// //Save the original parameters for later. +// double *origSolution = new double[xLength]; +// for(int i=0;i0) convergence = XconvergenceFine; +// else convergence = XconvergenceRough; +// //integer to keep track of how many times calc is called +// int ftimes=0; +// //Calculate Function at the starting point: +// double f0; +// f0 = calc(cons,consLength); +// if(f0f2f1 || f2>f3) +// { +// if(f2>f1) +// { +// //If f2 is greater than f1 then we shorten alpha2 and alpha3 closer to f1 +// //Effectively both are shortened by a factor of two. +// alpha3=alpha2; +// f3=f2; +// alpha2=alpha2/2; +// for(int i=0;if3) +// { +// //If f2 is greater than f3 then we length alpah2 and alpha3 closer to f1 +// //Effectively both are lengthened by a factor of two. +// alpha2=alpha3; +// f2=f3; +// alpha3=alpha3*2; +// for(int i=0;ialpha3 || alphaStarconvergence && fnew>smallF && iterationsf2f1 || f2>f3) +// { +// if(f2>f1) +// { +// //If f2 is greater than f1 then we shorten alpha2 and alpha3 closer to f1 +// //Effectively both are shortened by a factor of two. +// alpha3=alpha2; +// f3=f2; +// alpha2=alpha2/2; +// for(int i=0;if3) +// { +// //If f2 is greater than f3 then we length alpah2 and alpha3 closer to f1 +// //Effectively both are lengthened by a factor of two. +// alpha2=alpha3; +// f2=f3; +// alpha3=alpha3*2; +// for(int i=0;i100) +// { +// continue; +// } +// */ +// steps=steps+1; +// } +// +// // get the alpha for the minimum f of the quadratic approximation +// alphaStar= alpha2+((alpha2-alpha1)*(f1-f3))/(3*(f1-2*f2+f3)); +// +// +// //Guarantee that the new alphaStar is within the bracket +// if(alphaStar>=alpha3 || alphaStar<=alpha1) +// { +// alphaStar=alpha2; +// } +// if(alphaStar!=alphaStar) alphaStar=0; +// +// /// Set the values to alphaStar +// for(int i=0;i