public double[] StartMapping(SpotParsInitBox spotParsInitBox) { 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++) { for (int s = 0; s < spotParsInitBox.SpotsNumber; s++) { this.modeller[q].StarM.AddUniformCircularSpot(0.0, 0.0, 1.0, 4000, 30, 90); } } 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; 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; 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; 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; } double[] res; Simplex simplex = new Simplex(); DotNumerics.Optimization.TruncatedNewton tn = new TruncatedNewton(); //res = simplex.ComputeMin(this.Khi2, x); res = tn.ComputeMin(this.Khi2, this.GradKhi2, x); this.GenerateModLightCurve(); return(res); }
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 void OptimizationTruncatedNewtonConstrained() { //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; TruncatedNewton tNewton = new TruncatedNewton(); 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 = tNewton.ComputeMin(ObjetiveFunction, Gradient, variables); ObjectDumper.Write("Truncated Newton 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 = tNewton.ComputeMin(ObjetiveFunction, Gradient, variables); ObjectDumper.Write("Truncated Newton 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(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 double[] ComputeMin(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables, double tolerance, double factr, ref int nMax) { double[] minimum; this.Initialize(variables); if (this._NumFreeVariables == 0) return this._ExternalVariables; minimum = this.Compute(function, gradient, tolerance, factr, ref nMax); return minimum; }
private void Initialize(OptBoundVariable[] variables) { this._OptVariable = null; this._OptBoundVariable = OptBoundVariable.GetClon(variables); this._ExternalVariables = new double[this._OptBoundVariable.Length]; this._ExternalGradientArray = new double[this._OptBoundVariable.Length]; this.CheckAndSetBounds(this._OptBoundVariable); int numFreeVariable = 0; for (int i = 0; i < this._OptBoundVariable.Length; i++) { this._ExternalVariables[i] = this._OptBoundVariable[i].InitialGuess; if (this._OptBoundVariable[i].Fixed == false) { numFreeVariable++; } } this._NumFreeVariables = numFreeVariable; this._FreeVariables = new double[numFreeVariable]; int index = 0; for (int i = 0; i < this._OptBoundVariable.Length; i++) { if (this._OptBoundVariable[i].Fixed == false) { this._FreeVariables[index] = this._OptBoundVariable[i].InitialGuess; index++; } } this._GradientArray = new double[numFreeVariable]; //c nmax is the dimension of the largest problem to be solved. //c mmax is the maximum number of limited memory corrections. int NMAX = this._NumFreeVariables; int MMAX = this.M; // c wa is a double precision working array of length // c (2mmax + 4)nmax + 12mmax^2 + 12mmax int WADimension = (2 * MMAX + 4) * NMAX + 12 * (MMAX * MMAX) + 12 * MMAX; this.WA = new double[WADimension]; this.IWA = new int[3 * NMAX]; }
private void Initialize(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables) { this.internalFunction = new SFUN(function, gradient, variables); this.MeOptVariable = null; this.MeOptBoundVariable = OptBoundVariable.GetClon(variables); this.CheckAndSetBounds(this.MeOptBoundVariable); this.MeExternalVariables = new double[this.MeOptBoundVariable.Length]; int numFreeVariable = 0; for (int i = 0; i < this.MeOptBoundVariable.Length; i++) { this.MeExternalVariables[i] = variables[i].InitialGuess; if (this.MeOptBoundVariable[i].Fixed == false) { numFreeVariable++; } } this.MeF = function(this.MeExternalVariables); this.MeNumFreeVariables = numFreeVariable; this.MeFreeVariables = new double[numFreeVariable]; this.MeGradientArray = new double[numFreeVariable]; int index = 0; for (int i = 0; i < this.MeOptBoundVariable.Length; i++) { if (this.MeOptBoundVariable[i].Fixed == false) { this.MeFreeVariables[index] = this.MeOptBoundVariable[i].InitialGuess; index++; } } //W - (REAL*8)(REAL*8) WORK VECTOR OF LENGTH AT LEAST 14*N //LW - (INTEGER) THE DECLARED DIMENSION OF W this.MeLW = 14 * this.MeNumFreeVariables; this.MeW = new double[this.MeLW]; this.MeMAXIT = Math.Max(1, this.MeNumFreeVariables / 2); }
internal SFUN(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables) { //this.MeNParameters = nParameters; this.MeFunction = function; this.MeGradient = gradient; this.MeOptVariable = null; this.MeOptBoundVariable = variables; this.MeExternalVariables = new double[variables.Length]; this.MeExternalGradientArray = new double[variables.Length]; for (int i = 0; i < variables.Length; i++) { this.MeExternalVariables[i] = variables[i].InitialGuess; } }
private void CheckAndSetBounds(OptBoundVariable[] variables) { int numFreeVariable = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) numFreeVariable++; } this._LowerBounds = new double[numFreeVariable]; this._UpperBounds = new double[numFreeVariable]; // c nbd(i)=0 if x(i) is unbounded, // c 1 if x(i) has only a lower bound, // c 2 if x(i) has both lower and upper bounds, and // c 3 if x(i) has only an upper bound. this._NBD = new int[numFreeVariable]; int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { //Se invierten los limites de ser necesario if (variables[i].LowerBound > variables[i].UpperBound) { double tempBound = variables[i].LowerBound; variables[i].LowerBound = variables[i].UpperBound; variables[i].UpperBound = tempBound; } //Se revisa que la variable inicial este dentro de los limites if (variables[i].InitialGuess < variables[i].LowerBound) { variables[i].InitialGuess = variables[i].LowerBound; } if (variables[i].InitialGuess > variables[i].UpperBound) { variables[i].InitialGuess = variables[i].UpperBound; } this._LowerBounds[index] = variables[i].LowerBound; this._UpperBounds[index] = variables[i].UpperBound; if (this._LowerBounds[index] == double.NegativeInfinity && this._UpperBounds[index] == double.PositiveInfinity) { this._NBD[index] = 0; //nbd(i)=0 if x(i) is unbounded, } else if (this._LowerBounds[index] == double.NegativeInfinity) { this._NBD[index] = 3; //3 if x(i) has only an upper bound. } else if (this._UpperBounds[index] == double.PositiveInfinity) { this._NBD[index] = 3; //1 if x(i) has only a lower bound, } else { this._NBD[index] = 2; //2 if x(i) has both lower and upper bounds } index++; } } }
private void UpdateExternalVariables(OptBoundVariable[] variables, double[] X, int o_x) { int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { this.MeExternalVariables[i] = X[index + o_x]; index++; } } }
private void UpdateInternalGradient(OptBoundVariable[] variables) { int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { this._GradientArray[index] = this._ExternalGradientArray[i]; index++; } } }
private void UpdateExternalVariables(OptBoundVariable[] variables) { int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { this._ExternalVariables[i] = this._FreeVariables[index]; index++; } } }
private void Initialize(OptBoundVariable[] variables) { this._OptVariable = null; this._OptBoundVariable = OptBoundVariable.GetClon(variables); this._ExternalVariables = new double[this._OptBoundVariable.Length]; this._ExternalGradientArray = new double[this._OptBoundVariable.Length]; this.CheckAndSetBounds(this._OptBoundVariable); int numFreeVariable = 0; for (int i = 0; i < this._OptBoundVariable.Length; i++) { this._ExternalVariables[i] = this._OptBoundVariable[i].InitialGuess; if (this._OptBoundVariable[i].Fixed == false) numFreeVariable++; } this._NumFreeVariables = numFreeVariable; this._FreeVariables = new double[numFreeVariable]; int index = 0; for (int i = 0; i < this._OptBoundVariable.Length; i++) { if (this._OptBoundVariable[i].Fixed == false) { this._FreeVariables[index] = this._OptBoundVariable[i].InitialGuess; index++; } } this._GradientArray = new double[numFreeVariable]; //c nmax is the dimension of the largest problem to be solved. //c mmax is the maximum number of limited memory corrections. int NMAX = this._NumFreeVariables; int MMAX = this.M; // c wa is a double precision working array of length // c (2mmax + 4)nmax + 12mmax^2 + 12mmax int WADimension = (2 * MMAX + 4) * NMAX + 12 * (MMAX * MMAX) + 12 * MMAX; this.WA = new double[WADimension]; this.IWA = new int[3 * NMAX]; }
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); }
private void UpdateInternalGradient(OptBoundVariable[] variables, double[] G, int o_g) { int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { G[index + o_g] = this.MeExternalGradientArray[i]; index++; } } }
public double[] ComputeMin(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables, double tolerance, double ACCRCY, ref int nMax) { if (this.MeLMQNBC == null) this.MeLMQNBC = new LMQNBC(); this.Initialize(function, gradient, variables); if (this.MeNumFreeVariables == 0) return this.MeExternalVariables; int IERROR = 0; int[] IPIVOT = new int[this.MeNumFreeVariables]; this.MeLMQNBC.Run(ref IERROR, this.MeNumFreeVariables, ref this.MeFreeVariables, 0, ref this.MeF, ref this.MeGradientArray, 0, ref this.MeW, 0, this.MeLW, this.internalFunction, this.MeLowerBounds, 0, this.MeUpperBounds, 0, ref this.MeIPIVOT, 0, this.MeMSGLVL, this.MeMAXIT, nMax,this.MeETA, this.MeSTEPMX, ACCRCY, tolerance); int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { this.MeExternalVariables[i] = this.MeFreeVariables[index]; index++; } } nMax = this.internalFunction.FunEvaluations; return this.MeExternalVariables; }
private void Initialize(OptMultivariateFunction function, OptMultivariateGradient gradient, OptBoundVariable[] variables) { this.internalFunction = new SFUN(function, gradient, variables); this.MeOptVariable = null; this.MeOptBoundVariable = OptBoundVariable.GetClon(variables); this.CheckAndSetBounds(this.MeOptBoundVariable); this.MeExternalVariables = new double[this.MeOptBoundVariable.Length]; int numFreeVariable = 0; for (int i = 0; i < this.MeOptBoundVariable.Length; i++) { this.MeExternalVariables[i] = variables[i].InitialGuess; if (this.MeOptBoundVariable[i].Fixed == false) numFreeVariable++; } this.MeF = function(this.MeExternalVariables); this.MeNumFreeVariables = numFreeVariable; this.MeFreeVariables = new double[numFreeVariable]; this.MeGradientArray = new double[numFreeVariable]; int index = 0; for (int i = 0; i < this.MeOptBoundVariable.Length; i++) { if (this.MeOptBoundVariable[i].Fixed == false) { this.MeFreeVariables[index] = this.MeOptBoundVariable[i].InitialGuess; index++; } } //W - (REAL*8)(REAL*8) WORK VECTOR OF LENGTH AT LEAST 14*N //LW - (INTEGER) THE DECLARED DIMENSION OF W this.MeLW = 14 * this.MeNumFreeVariables; this.MeW = new double[this.MeLW]; this.MeMAXIT = Math.Max(1, this.MeNumFreeVariables / 2); }
private void CheckAndSetBounds(OptBoundVariable[] variables) { int numFreeVariable = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) numFreeVariable++; } this.MeLowerBounds = new double[numFreeVariable]; this.MeUpperBounds = new double[numFreeVariable]; this.MeIPIVOT = new int[numFreeVariable]; int index = 0; for (int i = 0; i < variables.Length; i++) { if (variables[i].Fixed == false) { //Se invierten los limites de ser necesario if (variables[i].LowerBound > variables[i].UpperBound) { double tempBound = variables[i].LowerBound; variables[i].LowerBound = variables[i].UpperBound; variables[i].UpperBound = tempBound; } //Se revisa que la variable inicial este dentro de los limites if (variables[i].InitialGuess < variables[i].LowerBound) { variables[i].InitialGuess = variables[i].LowerBound; } if (variables[i].InitialGuess > variables[i].UpperBound) { variables[i].InitialGuess = variables[i].UpperBound; } this.MeLowerBounds[index] = variables[i].LowerBound; this.MeUpperBounds[index] = variables[i].UpperBound; // C LOW, UP - (REAL*8) VECTORS OF LENGTH AT LEAST N CONTAINING // C THE LOWER AND UPPER BOUNDS ON THE VARIABLES. IF // C THERE ARE NO BOUNDS ON A PARTICULAR VARIABLE, SET // C THE BOUNDS TO -1.D38 AND 1.D38, RESPECTIVELY. if (this.MeLowerBounds[index] == double.NegativeInfinity && this.MeUpperBounds[index] == double.PositiveInfinity) { this.MeLowerBounds[index] = 1E-38; this.MeUpperBounds[index] = 1E38; } else if (this.MeLowerBounds[index] == double.NegativeInfinity) { this.MeLowerBounds[index] = 1E-38; } else if (this.MeUpperBounds[index] == double.PositiveInfinity) { this.MeUpperBounds[index] = 1E38; } index++; } } }