public void OptimizationLBFGSB() { //Rosenbrock banana function //f(a,b)=100*(b-a^2)^2+(1-a)^2 //The minimum is at (1,1) and the function value is 0. //using DotNumerics.Optimization; L_BFGS_B LBFGSB = new L_BFGS_B(); double[] initialGuess = new double[2]; initialGuess[0] = 0.1; initialGuess[1] = 2; double[] minimum = LBFGSB.ComputeMin(BananaFunction, BananaGradient, initialGuess); ObjectDumper.Write("Limited memory Broyden–Fletcher–Goldfarb–Shanno method."); ObjectDumper.Write("a = " + minimum[0].ToString() + ", b = " + minimum[1].ToString()); //f(a,b) = 100*(b-a^2)^2 + (1-a)^2 //private double BananaFunction(double[] x) //{ // double f = 0; // f = 100 * Math.Pow((x[1] - x[0] * x[0]), 2) + Math.Pow((1 - x[0]), 2); // return f; //} //double[] bananaGrad = new double[2]; //private double[] BananaGradient(double[] x) //{ // this.bananaGrad[0] = 200 * (x[1] - x[0] * x[0]) * (-2 * x[0]) - 2 * (1 - x[0]); // this.bananaGrad[1] = 200 * (x[1] - x[0] * x[0]); // return bananaGrad; //} }
public void OptimizationLBFGSBConstrained() { //This example minimize the function //f(x0,x2,...,xn)= (x0-0)^2+(x1-1)^2+...(xn-n)^2 //The minimum is at (0,1,2,3...,n) for the unconstrained case. //using DotNumerics.Optimization; L_BFGS_B LBFGSB = new L_BFGS_B(); int numVariables = 5; OptBoundVariable[] variables = new OptBoundVariable[numVariables]; //Constrained Minimization on the interval (-10,10), initial Guess=-2; for (int i = 0; i < numVariables; i++) { variables[i] = new OptBoundVariable("x" + i.ToString(), -2, -10, 10); } double[] minimum = LBFGSB.ComputeMin(ObjetiveFunction, Gradient, variables); ObjectDumper.Write("L-BFGS-B Method. Constrained Minimization on the interval (-10,10)"); for (int i = 0; i < minimum.Length; i++) { ObjectDumper.Write("x" + i.ToString() + " = " + minimum[i].ToString()); } //Constrained Minimization on the interval (-10,3), initial Guess=-2; for (int i = 0; i < numVariables; i++) { variables[i].UpperBound = 3; } minimum = LBFGSB.ComputeMin(ObjetiveFunction, Gradient, variables); ObjectDumper.Write("L-BFGS-B Method. Constrained Minimization on the interval (-10,3)"); for (int i = 0; i < minimum.Length; i++) { ObjectDumper.Write("x" + i.ToString() + " = " + minimum[i].ToString()); } //f(x0,x2,...,xn)= (x0-0)^2+(x1-1)^2+...(xn-n)^2 //private double ObjetiveFunction(double[] x) //{ // int numVariables = 5; // double f = 0; // for (int i = 0; i < numVariables; i++) f += Math.Pow(x[i] - i, 2); // return f; //} //private double[] Gradient(double[] x) //{ // int numVariables = 5; // double[] grad = new double[x.Length]; // for (int i = 0; i < numVariables; i++) grad[i] = 2 * (x[i] - i); // return grad; //} }
public double[] StartMapping(SpotParsInitBox spotParsInitBox, string minimizator, double tau) { OptBoundVariable[] x = new OptBoundVariable[spotParsInitBox.SpotsNumber * 4]; for (int i = 0; i < x.Length; i++) { x[i] = new OptBoundVariable(); } for (int q = 0; q < this.lcObs.Length; q++) { this.modellers[q].StarM.RemoveAllSpots(); for (int s = 0; s < spotParsInitBox.SpotsNumber; s++) { this.modellers[q].StarM.AddUniformCircularSpot( spotParsInitBox.spots[s].longitude * Math.PI / 180, spotParsInitBox.spots[s].colatutude * Math.PI / 180, spotParsInitBox.spots[s].radius * Math.PI / 180, spotParsInitBox.spots[s].teff, spotParsInitBox.spots[s].beltsCount, spotParsInitBox.spots[s].nearEquatorialPatchesCount); } } this.fixedParsMask = new bool[x.Length]; for (int s = 0; s < spotParsInitBox.SpotsNumber; s++) { x[0 + s * 4].InitialGuess = spotParsInitBox.spots[s].longitude * Math.PI / 180; x[0 + s * 4].UpperBound = spotParsInitBox.spots[s].longitudeUpperLimit * Math.PI / 180; x[0 + s * 4].LowerBound = spotParsInitBox.spots[s].longitudeLowerLimit * Math.PI / 180; x[0 + s * 4].Fixed = spotParsInitBox.spots[s].longitudeFixed; this.fixedParsMask[0 + s * 4] = spotParsInitBox.spots[s].longitudeFixed; x[1 + s * 4].InitialGuess = spotParsInitBox.spots[s].colatutude * Math.PI / 180; x[1 + s * 4].UpperBound = spotParsInitBox.spots[s].colatitudeUpperLimit * Math.PI / 180; x[1 + s * 4].LowerBound = spotParsInitBox.spots[s].colatitudeLowerLimit * Math.PI / 180; x[1 + s * 4].Fixed = spotParsInitBox.spots[s].colatitudeFixed; this.fixedParsMask[1 + s * 4] = spotParsInitBox.spots[s].colatitudeFixed; x[2 + s * 4].InitialGuess = spotParsInitBox.spots[s].radius * Math.PI / 180; x[2 + s * 4].UpperBound = spotParsInitBox.spots[s].radiusUpperLimit * Math.PI / 180; x[2 + s * 4].LowerBound = spotParsInitBox.spots[s].radiusLowerLimit * Math.PI / 180; x[2 + s * 4].Fixed = spotParsInitBox.spots[s].radiusFixed; this.fixedParsMask[2 + s * 4] = spotParsInitBox.spots[s].radiusFixed; x[3 + s * 4].InitialGuess = spotParsInitBox.spots[s].teff; x[3 + s * 4].UpperBound = spotParsInitBox.spots[s].teffUpperLimit; x[3 + s * 4].LowerBound = spotParsInitBox.spots[s].teffLowerLimit; x[3 + s * 4].Fixed = spotParsInitBox.spots[s].teffFixed; this.fixedParsMask[3 + s * 4] = spotParsInitBox.spots[s].teffFixed; } double[] res = new double[4]; if (minimizator == "Simplex") { Simplex simplex = new Simplex(); res = simplex.ComputeMin(this.Khi2, x); } if (minimizator == "TN") { //DotNumerics.Optimization.TruncatedNewton tn = new TruncatedNewton(); DotNumerics.Optimization.L_BFGS_B bfgs = new L_BFGS_B(); //tn.SearchSeverity = 1; //res = tn.ComputeMin(this.Khi2, this.GradKhi2, x); res = bfgs.ComputeMin(this.Khi2, this.GradKhi2, x); } if (minimizator == "LM") { LevenbergMaquard gn = new LevenbergMaquard(); gn.MinimizedFunction = this.ResudalVector; double[] step = new double[x.Length]; double[] xinit = new double[x.Length]; bool[] isFixed = new bool[x.Length]; double[] lowLimits = new double[x.Length]; double[] highLimits = new double[x.Length]; for (int i = 0; i < xinit.Length; i++) { xinit[i] = x[i].InitialGuess; isFixed[i] = x[i].Fixed; lowLimits[i] = x[i].LowerBound; highLimits[i] = x[i].UpperBound; if (i != 0 && (i + 1) % 4 == 0) { step[i] = 10; // step for temperature; } else { step[i] = 0.02; } } gn.StepForDer = step; gn.IterMax = 100; gn.Tau = tau; res = gn.ComputeMin(xinit, isFixed, lowLimits, highLimits); } this.khi2 = this.Khi2(res); this.GenerateModLightCurve(); return(res); }