public OptVector Solve( OptVector[] A, OptVector b, OptVector startX, int nIter) { OptVector[] normA = A; OptVector normb = b; if (!OptVector.Equals(A, OptVector.Transpose(A))) { OptVector[] At = OptVector.Transpose(A); normA = OptVector.Mult(At, A); normb = OptVector.Mult(At, b); } OptVector rNew = normb - OptVector.Mult(normA, startX); OptVector p = rNew; OptVector x = new OptVector(startX); double r2Old = rNew * rNew; double alpha = 1.0; double beta = 1.0; for (int i = 0; i < nIter; i++) { alpha = GetAlpha(normA, p, r2Old); x = x + alpha * p; rNew = rNew - alpha * OptVector.Mult(normA, p); double r2New = rNew * rNew; if (r2New < Precision) { return(x); } beta = GetBeta(r2New, r2Old); p = rNew + beta * p; r2Old = r2New; } return(x); }
static void Main(string[] args) { double oneOver2Pi = 1.0 / (1.0 * Math.Sqrt(2 * Math.PI)); Console.WriteLine(NormalCDFInverse(0.99)); Console.WriteLine(inv_cdf(0.99)); Console.WriteLine(InverseCDF.QNorm(0.99, 0, 1, true, false)); Console.WriteLine(NormalCDFInverse(0.5)); Console.WriteLine(inv_cdf(0.5)); Console.WriteLine(InverseCDF.QNorm(0.5, 0, 1, true, false)); Console.WriteLine(NormalCDFInverse(0.31)); Console.WriteLine(inv_cdf(0.31)); Console.WriteLine(InverseCDF.QNorm(0.31, 0, 1, true, false)); //x4−8x2 + 5 Func <double[], double> testFunc1 = (x) => { return(Math.Pow(x[0], 4) - 8 * Math.Pow(x[0], 2) + 5); }; Func <double[], double>[] dtestFunc1 = new Func <double[], double> [1]; dtestFunc1[0] = (x) => { return(4 * Math.Pow(x[0], 3) - 16 * x[0]); }; Func <double[], double> testConstr = (x) => { return(5 - Math.Exp(x[0]) + 2.0 * Math.Pow(x[0] - 1, 2)); }; Func <double[], double> testv = (x) => { double a = x[0]; return(0.0); }; //Func<Variables, double> testFunc = (x) => { // return Math.Pow(x.Vars[0], 4) - 3 * Math.Pow(x.Vars[0], 3) + 2; //}; //Func<Variables, double> dTestfunc = (x) => //{ // return 4 * Math.Pow(x.Vars[0], 3) - 9 * Math.Pow(x.Vars[0], 2); //}; //x4−3x3 + 2 //TestFunc(); Func <double[], double> bananaFunc = (x) => { return(Math.Pow(1 - x[0], 2) + 100 * Math.Pow(x[1] - x[0] * x[0], 2)); }; Func <double[], double> powell = (x) => { return(Math.Pow(x[0] + 10 * x[1], 2) + 5 * Math.Pow(x[2] - x[3], 2) + Math.Pow(x[1] + 2 * x[2], 4) + 10 * Math.Pow(x[0] - x[3], 4)); }; OptVector[] ttt = new OptVector[3]; ttt[0] = new OptVector(new double[3] { 1, 2, 3 }); ttt[1] = new OptVector(new double[3] { 4, 5, 6 }); ttt[2] = new OptVector(new double[3] { 7, 8, 9 }); var tr = OptVector.Transpose(ttt); //TestsSQP.Test0(); TestsSQP.Test1(); TestsSQP.Test2(); TestsSQP.Test3(); ////TestsSQP.Test4(); ////TestsSQP.Test5(); TestsSQP.Test6(); TestsSQP.Test7(); TestsSQP.Test8(); TestsSQP.Test9(); TestsSQP.Test10(); TestsSQP.Test11(); TestsSQP.Test12(); TestsSQP.Test13(); TestsSQP.Test14(); TestsSQP.Test15(); TestsSQP.Test16(); TestsSQP.Test17(); TestsSQP.Test18(); TestsSQP.Test19(); TestsSQP.Test20(); TestsSQP.Test21(); TestsSQP.Test22(); TestsSQP.Test23(); TestsSQP.Test24(); TestsSQP.Test25(); TestsSQP.Test26(); TestsSQP.Test27(); TestsSQP.Test28(); //TestCGMethod(); BFGS bfsg = new BFGS(); var result0 = bfsg.Solve(powell, new double[] { 3.0, -1.0, 0.0, 1.0 }, 10000); Console.WriteLine("Min " + powell(result0)); NLCG gradient = new NLCG(); var result = gradient.Solve(powell, new double[] { 3.0, -1.0, 0.0, 1.0 }, 4000); Console.WriteLine("Min " + powell(result)); SteepestDescent gradientSteep = new SteepestDescent(); var result1 = gradientSteep.Solve(powell, new double[] { 3.0, -1.0, 0.0, 1.0 }, 10000); Console.WriteLine("Min " + powell(result1)); //Variables result = SteepestDescent(testFunc, dTestFunc, 2, 20); for (int i = 0; i < result.Length; i++) { Console.WriteLine("result " + result[i]); } //Console.WriteLine("ver " + testFunc(result)); Console.ReadLine(); }
public OptVector Solve( OptVector[] A, OptVector b, OptVector startX, int nIter) { OptVector[] symmA = A; OptVector normb = b; //Symmetrize matrix if (CheckSymmetry) { OptVector[] At = OptVector.Transpose(A); if (!OptVector.Equals(A, At)) { symmA = OptVector.Mult(At, A); normb = OptVector.Mult(At, b); } } OptVector v0 = new OptVector(b.Count); OptVector v1 = normb - OptVector.Mult(symmA, startX); double beta1 = v1.Length(); double betaN = 0.0; double n = beta1; double c0 = 1.0; double c1 = 1.0; double s0 = 0.0; double s1 = 0.0; OptVector w0 = new OptVector(v1.Count); OptVector w_1 = new OptVector(v1.Count); OptVector x = new OptVector(startX); for (int i = 0; i < nIter; i++) { //Calculate Lanczos Vectors OptVector v = (1.0 / beta1) * v1; OptVector Av = OptVector.Mult(symmA, v); double alpha = v * Av; v1 = Av - alpha * v - beta1 * v0; betaN = v1.Length(); //Calculate QR factors double lambda = c1 * alpha - c0 * s1 * beta1; double p1 = Math.Sqrt(lambda * lambda + betaN * betaN); double p2 = s1 * alpha + c0 * c1 * beta1; double p3 = s0 * beta1; //Calculate New Givens Rotations c0 = c1; c1 = lambda / p1; s0 = s1; s1 = betaN / p1; //Update Solution OptVector w = (1.0 / p1) * (v - p3 * w_1 - p2 * w0); x = x + c1 * n * w; n = -s1 * n; residual = Math.Abs(n); if (residual < precisionConst) { break; } beta1 = betaN; v0 = v; w_1 = w0; w0 = w; } return(x); }