/************************************************************************* * Calculate test function #2 * * Simple variation of #1, much more nonlinear, which makes unlikely premature * convergence of algorithm . *************************************************************************/ private static void testfunc2(ref minasa.minasastate state) { if ((double)(state.x[0]) < (double)(100)) { state.f = AP.Math.Sqr(Math.Exp(state.x[0]) - 2) + AP.Math.Sqr(AP.Math.Sqr(state.x[1])) + AP.Math.Sqr(state.x[2] - state.x[0]); state.g[0] = 2 * (Math.Exp(state.x[0]) - 2) * Math.Exp(state.x[0]) + 2 * (state.x[0] - state.x[2]); state.g[1] = 4 * state.x[1] * AP.Math.Sqr(state.x[1]); state.g[2] = 2 * (state.x[2] - state.x[0]); } else { state.f = Math.Sqrt(AP.Math.MaxRealNumber); state.g[0] = Math.Sqrt(AP.Math.MaxRealNumber); state.g[1] = 0; state.g[2] = 0; } }
public static int Main(string[] args) { int n = 0; int i = 0; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); double[] s = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double x = 0; double y = 0; double z = 0; // // Function being minimized: // F = x+4y+9z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1. // // Take a look at MinASASetStpMax() - it restricts step length by // a small value, so we can see the current point traveling through // a feasible set, sticking to its bounds. // n = 3; s = new double[n]; bndl = new double[n]; bndu = new double[n]; for (i = 0; i <= n - 1; i++) { s[i] = 1; bndl[i] = 0; bndu[i] = 1; } minasa.minasacreate(n, ref s, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.00001, 0); minasa.minasasetxrep(ref state, true); minasa.minasasetstpmax(ref state, 0.2); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.Write("F = x+4y+9z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1"); System.Console.WriteLine(); System.Console.Write("OPTIMIZATION STARTED"); System.Console.WriteLine(); while (minasa.minasaiteration(ref state)) { if (state.needfg) { x = state.x[0]; y = state.x[1]; z = state.x[2]; state.f = x + 4 * y + 9 * z; state.g[0] = 1; state.g[1] = 4; state.g[2] = 9; } if (state.xupdated) { System.Console.Write(" F("); System.Console.Write("{0,4:F2}", state.x[0]); System.Console.Write(", "); System.Console.Write("{0,4:F2}", state.x[1]); System.Console.Write(", "); System.Console.Write("{0,4:F2}", state.x[2]); System.Console.Write(") = "); System.Console.Write("{0,0:F3}", state.f); System.Console.WriteLine(); } } System.Console.Write("OPTIMIZATION STOPPED"); System.Console.WriteLine(); minasa.minasaresults(ref state, ref s, ref rep); // // output results // System.Console.Write("X = "); System.Console.Write("{0,4:F2}", s[0]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Y = "); System.Console.Write("{0,4:F2}", s[1]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Z = "); System.Console.Write("{0,4:F2}", s[2]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return(0); }
public static bool testminasa(bool silent) { bool result = new bool(); bool waserrors = new bool(); bool referror = new bool(); bool converror = new bool(); bool othererrors = new bool(); int n = 0; double[] x = new double[0]; double[] xe = new double[0]; double[] c = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double[] xlast = new double[0]; double fprev = 0; double xprev = 0; double stpmax = 0; int i = 0; int j = 0; double v = 0; double s = 0; double tol = 0; int algotype = 0; double[,] a = new double[0, 0]; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); int i_ = 0; waserrors = false; referror = false; converror = false; othererrors = false; // // Different algorithms // for (algotype = -1; algotype <= 1; algotype++) { // // reference problem, simple convex optimization // for (n = 1; n <= 5; n++) { // // min(x'*diag(c)*x) on a random box // x = new double[n]; xe = new double[n]; c = new double[n]; bndl = new double[n]; bndu = new double[n]; for (i = 0; i <= n - 1; i++) { c[i] = 1 + AP.Math.RandomReal(); xe[i] = 4 * AP.Math.RandomReal() - 2; bndl[i] = -Math.Max(AP.Math.RandomReal(), 0.2); bndu[i] = +Math.Max(AP.Math.RandomReal(), 0.2); x[i] = 0.5 * (bndl[i] + bndu[i]); } tol = 0.001; minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, tol, 0.0, 0.0, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); state.f = 0; for (i = 0; i <= n - 1; i++) { state.f = state.f + c[i] * AP.Math.Sqr(state.x[i] - xe[i]); state.g[i] = 2 * c[i] * (state.x[i] - xe[i]); } } minasa.minasaresults(ref state, ref x, ref rep); referror = referror | rep.terminationtype <= 0; for (i = 0; i <= n - 1; i++) { referror = referror | (double)(Math.Abs(asaboundval(xe[i], bndl[i], bndu[i]) - x[i])) > (double)(0.01); } } // // reference problem 2: non-convex optimization on [-2,2] x [1,2] // // A saddle function is minimized: // * stationary point [0,0] (non-feasible) // * constrained minimum [-2,2]. // * starting point [+2,2] // // Path from start to end may be very complex, with multiple changes // in active constraints, so it is interesting task for our method. // // Scale parameter is used to make optimization more interesting // during GPA runs. // x = new double[2]; bndl = new double[2]; bndu = new double[2]; bndl[0] = -2; bndu[0] = 2; x[0] = 2; bndl[1] = 1; bndu[1] = 2; x[1] = 2; tol = 0.001; s = 0.01; minasa.minasacreate(2, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, tol, 0.0, 0.0, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, 2, ref othererrors); state.f = s * (AP.Math.Sqr(state.x[0] + state.x[1]) - AP.Math.Sqr(state.x[0] - state.x[1])); state.g[0] = s * (2 * (state.x[0] + state.x[1]) - 2 * (state.x[0] - state.x[1])); state.g[1] = s * (2 * (state.x[0] + state.x[1]) + 2 * (state.x[0] - state.x[1])); } minasa.minasaresults(ref state, ref x, ref rep); referror = referror | rep.terminationtype <= 0 | (double)(Math.Abs(state.x[0] + 2)) > (double)(0.01) | (double)(Math.Abs(state.x[1] - 2)) > (double)(0.01); // // function #1 with 'x[0]>=ln(2)' constraint. // may show very interesting behavior. // x = new double[3]; bndl = new double[3]; bndu = new double[3]; n = 3; for (i = 0; i <= 2; i++) { bndl[i] = -10000; bndu[i] = +10000; } bndl[0] = Math.Log(2); for (i = 0; i <= 2; i++) { x[i] = 3 * AP.Math.RandomReal() + 3; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0000001, 0.0, 0.0, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); testfunc1(ref state); } minasa.minasaresults(ref state, ref x, ref rep); referror = referror | rep.terminationtype <= 0; referror = referror | (double)(Math.Abs(x[0] - Math.Log(2))) > (double)(0.05); referror = referror | (double)(Math.Abs(x[1])) > (double)(0.05); referror = referror | (double)(Math.Abs(x[2] - Math.Log(2))) > (double)(0.05); // // Testing convergence properties // x = new double[3]; bndl = new double[3]; bndu = new double[3]; n = 3; for (i = 0; i <= 2; i++) { bndl[i] = -10000; bndu[i] = +10000; } bndl[0] = Math.Log(2); for (i = 0; i <= 2; i++) { x[i] = 3 * AP.Math.RandomReal() + 3; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.001, 0.0, 0.0, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); testfunc3(ref state); } minasa.minasaresults(ref state, ref x, ref rep); converror = converror | rep.terminationtype != 4; for (i = 0; i <= 2; i++) { x[i] = 3 * AP.Math.RandomReal() + 3; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.001, 0.0, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); testfunc3(ref state); } minasa.minasaresults(ref state, ref x, ref rep); converror = converror | rep.terminationtype != 1; for (i = 0; i <= 2; i++) { x[i] = 3 * AP.Math.RandomReal() + 3; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.001, 0); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); testfunc3(ref state); } minasa.minasaresults(ref state, ref x, ref rep); converror = converror | rep.terminationtype != 2; for (i = 0; i <= 2; i++) { x[i] = 3 * AP.Math.RandomReal() + 3; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.0, 3); minasa.minasasetalgorithm(ref state, algotype); while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); testfunc3(ref state); } minasa.minasaresults(ref state, ref x, ref rep); converror = converror | !(rep.terminationtype == 5 & rep.iterationscount == 3 | rep.terminationtype == 7); // // Other properties // // // Other properties: // 1. test reports (F should form monotone sequence) // 2. test maximum step // n = 50; x = new double[n]; xlast = new double[n]; bndl = new double[n]; bndu = new double[n]; for (i = 0; i <= n - 1; i++) { x[i] = 1; xlast[i] = AP.Math.RandomReal(); bndl[i] = -100000; bndu[i] = +100000; } minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0, 0, 0, 100); minasa.minasasetxrep(ref state, true); fprev = AP.Math.MaxRealNumber; while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); if (state.needfg) { state.f = 0; for (i = 0; i <= n - 1; i++) { state.f = state.f + AP.Math.Sqr((1 + i) * state.x[i]); state.g[i] = 2 * (1 + i) * state.x[i]; } } if (state.xupdated) { othererrors = othererrors | (double)(state.f) > (double)(fprev); if ((double)(fprev) == (double)(AP.Math.MaxRealNumber)) { for (i = 0; i <= n - 1; i++) { othererrors = othererrors | (double)(state.x[i]) != (double)(x[i]); } } fprev = state.f; for (i_ = 0; i_ <= n - 1; i_++) { xlast[i_] = state.x[i_]; } } } minasa.minasaresults(ref state, ref x, ref rep); for (i = 0; i <= n - 1; i++) { othererrors = othererrors | (double)(x[i]) != (double)(xlast[i]); } n = 1; x = new double[n]; bndl = new double[n]; bndu = new double[n]; x[0] = 100; bndl[0] = -1000000; bndu[0] = +1000000; stpmax = 0.05 + 0.05 * AP.Math.RandomReal(); minasa.minasacreate(n, ref x, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 1.0E-9, 0, 0, 0); minasa.minasasetstpmax(ref state, stpmax); minasa.minasasetxrep(ref state, true); xprev = x[0]; while (minasa.minasaiteration(ref state)) { checkbounds(ref state.x, ref bndl, ref bndu, n, ref othererrors); if (state.needfg) { state.f = Math.Exp(state.x[0]) + Math.Exp(-state.x[0]); state.g[0] = Math.Exp(state.x[0]) - Math.Exp(-state.x[0]); othererrors = othererrors | (double)(Math.Abs(state.x[0] - xprev)) > (double)((1 + Math.Sqrt(AP.Math.MachineEpsilon)) * stpmax); } if (state.xupdated) { othererrors = othererrors | (double)(Math.Abs(state.x[0] - xprev)) > (double)((1 + Math.Sqrt(AP.Math.MachineEpsilon)) * stpmax); xprev = state.x[0]; } } } // // end // waserrors = referror | converror | othererrors; if (!silent) { System.Console.Write("TESTING ASA OPTIMIZATION"); System.Console.WriteLine(); System.Console.Write("REFERENCE PROBLEMS: "); if (referror) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("CONVERGENCE PROPERTIES: "); if (converror) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("OTHER PROPERTIES: "); if (othererrors) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } if (waserrors) { System.Console.Write("TEST FAILED"); System.Console.WriteLine(); } else { System.Console.Write("TEST PASSED"); System.Console.WriteLine(); } System.Console.WriteLine(); System.Console.WriteLine(); } result = !waserrors; return(result); }
public minasastate(minasa.minasastate obj) { _innerobj = obj; }
public minasastate() { _innerobj = new minasa.minasastate(); }
public static int Main(string[] args) { int n = 0; int i = 0; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); double[] s = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double x = 0; double y = 0; double z = 0; // // Function being minimized: // F = x+2y+3z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1. // n = 3; s = new double[n]; bndl = new double[n]; bndu = new double[n]; for (i = 0; i <= n - 1; i++) { s[i] = 1; bndl[i] = 0; bndu[i] = 1; } minasa.minasacreate(n, ref s, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.00001, 0); minasa.minasasetxrep(ref state, true); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.Write("F = x+2y+3z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1"); System.Console.WriteLine(); System.Console.Write("OPTIMIZATION STARTED"); System.Console.WriteLine(); while (minasa.minasaiteration(ref state)) { if (state.needfg) { x = state.x[0]; y = state.x[1]; z = state.x[2]; state.f = x + 2 * y + 3 * z; state.g[0] = 1; state.g[1] = 2; state.g[2] = 3; } if (state.xupdated) { System.Console.Write(" F("); System.Console.Write("{0,4:F2}", state.x[0]); System.Console.Write(","); System.Console.Write("{0,4:F2}", state.x[1]); System.Console.Write(","); System.Console.Write("{0,4:F2}", state.x[2]); System.Console.Write(")="); System.Console.Write("{0,0:F3}", state.f); System.Console.WriteLine(); } } System.Console.Write("OPTIMIZATION STOPPED"); System.Console.WriteLine(); minasa.minasaresults(ref state, ref s, ref rep); // // output results // System.Console.Write("X = "); System.Console.Write("{0,4:F2}", s[0]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Y = "); System.Console.Write("{0,4:F2}", s[1]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Z = "); System.Console.Write("{0,4:F2}", s[2]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return(0); }
public static bool testminasa(bool silent) { bool result = new bool(); bool waserrors = new bool(); bool referror = new bool(); bool restartserror = new bool(); bool converror = new bool(); bool othererrors = new bool(); int n = 0; double[] x = new double[0]; double[] xe = new double[0]; double[] c = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double[] xlast = new double[0]; double fprev = 0; double xprev = 0; double stpmax = 0; int i = 0; int j = 0; double v = 0; double s = 0; double tol = 0; int algotype = 0; double[,] a = new double[0,0]; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); int i_ = 0; waserrors = false; referror = false; converror = false; othererrors = false; restartserror = false; // // Different algorithms // for(algotype=-1; algotype<=1; algotype++) { // // reference problem, simple convex optimization // for(n=1; n<=5; n++) { // // min(x'*diag(c)*x) on a random box // x = new double[n]; xe = new double[n]; c = new double[n]; bndl = new double[n]; bndu = new double[n]; for(i=0; i<=n-1; i++) { c[i] = 1+math.randomreal(); xe[i] = 4*math.randomreal()-2; bndl[i] = -Math.Max(math.randomreal(), 0.2); bndu[i] = Math.Max(math.randomreal(), 0.2); x[i] = 0.5*(bndl[i]+bndu[i]); } tol = 0.001; minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, tol, 0.0, 0.0, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); state.f = 0; for(i=0; i<=n-1; i++) { state.f = state.f+c[i]*math.sqr(state.x[i]-xe[i]); state.g[i] = 2*c[i]*(state.x[i]-xe[i]); } } minasa.minasaresults(state, ref x, rep); referror = referror | rep.terminationtype<=0; for(i=0; i<=n-1; i++) { referror = referror | (double)(Math.Abs(asaboundval(xe[i], bndl[i], bndu[i])-x[i]))>(double)(0.01); } } // // F2 problem with restarts: // * make several iterations and restart BEFORE termination // * iterate and restart AFTER termination // // NOTE: step is bounded from above to avoid premature convergence // x = new double[3]; bndl = new double[3]; bndu = new double[3]; n = 3; x[0] = 10+10*math.randomreal(); x[1] = 10+10*math.randomreal(); x[2] = 10+10*math.randomreal(); bndl[0] = -10000; bndl[1] = -10000; bndl[2] = -10000; bndu[0] = 10000; bndu[1] = 10000; bndu[2] = 10000; minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetalgorithm(state, algotype); minasa.minasasetstpmax(state, 0.1); minasa.minasasetcond(state, 0.0000001, 0.0, 0.0, 0); for(i=0; i<=10; i++) { if( !minasa.minasaiteration(state) ) { break; } testfunc2(state); } x[0] = 10+10*math.randomreal(); x[1] = 10+10*math.randomreal(); x[2] = 10+10*math.randomreal(); minasa.minasarestartfrom(state, x, bndl, bndu); while( minasa.minasaiteration(state) ) { testfunc2(state); } minasa.minasaresults(state, ref x, rep); restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01); x[0] = 10+10*math.randomreal(); x[1] = 10+10*math.randomreal(); x[2] = 10+10*math.randomreal(); minasa.minasarestartfrom(state, x, bndl, bndu); while( minasa.minasaiteration(state) ) { testfunc2(state); } minasa.minasaresults(state, ref x, rep); restartserror = (((restartserror | rep.terminationtype<=0) | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.01)) | (double)(Math.Abs(x[1]))>(double)(0.01)) | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.01); // // reference problem 2: non-convex optimization on [-2,2] x [1,2] // // A saddle function is minimized: // * stationary point [0,0] (non-feasible) // * constrained minimum [-2,2]. // * starting point [+2,2] // // Path from start to end may be very complex, with multiple changes // in active constraints, so it is interesting task for our method. // // Scale parameter is used to make optimization more interesting // during GPA runs. // x = new double[2]; bndl = new double[2]; bndu = new double[2]; bndl[0] = -2; bndu[0] = 2; x[0] = 2; bndl[1] = 1; bndu[1] = 2; x[1] = 2; tol = 0.001; s = 0.01; minasa.minasacreate(2, x, bndl, bndu, state); minasa.minasasetcond(state, tol, 0.0, 0.0, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, 2, ref othererrors); state.f = s*(math.sqr(state.x[0]+state.x[1])-math.sqr(state.x[0]-state.x[1])); state.g[0] = s*(2*(state.x[0]+state.x[1])-2*(state.x[0]-state.x[1])); state.g[1] = s*(2*(state.x[0]+state.x[1])+2*(state.x[0]-state.x[1])); } minasa.minasaresults(state, ref x, rep); referror = ((referror | rep.terminationtype<=0) | (double)(Math.Abs(state.x[0]+2))>(double)(0.01)) | (double)(Math.Abs(state.x[1]-2))>(double)(0.01); // // function #1 with 'x[0]>=ln(2)' constraint. // may show very interesting behavior. // x = new double[3]; bndl = new double[3]; bndu = new double[3]; n = 3; for(i=0; i<=2; i++) { bndl[i] = -10000; bndu[i] = 10000; } bndl[0] = Math.Log(2); for(i=0; i<=2; i++) { x[i] = 3*math.randomreal()+3; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0.0000001, 0.0, 0.0, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); testfunc1(state); } minasa.minasaresults(state, ref x, rep); referror = referror | rep.terminationtype<=0; referror = referror | (double)(Math.Abs(x[0]-Math.Log(2)))>(double)(0.05); referror = referror | (double)(Math.Abs(x[1]))>(double)(0.05); referror = referror | (double)(Math.Abs(x[2]-Math.Log(2)))>(double)(0.05); // // Testing convergence properties // x = new double[3]; bndl = new double[3]; bndu = new double[3]; n = 3; for(i=0; i<=2; i++) { bndl[i] = -10000; bndu[i] = 10000; } bndl[0] = Math.Log(2); for(i=0; i<=2; i++) { x[i] = 3*math.randomreal()+3; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0.001, 0.0, 0.0, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); testfunc3(state); } minasa.minasaresults(state, ref x, rep); converror = converror | rep.terminationtype!=4; for(i=0; i<=2; i++) { x[i] = 3*math.randomreal()+3; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0.0, 0.001, 0.0, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); testfunc3(state); } minasa.minasaresults(state, ref x, rep); converror = converror | rep.terminationtype!=1; for(i=0; i<=2; i++) { x[i] = 3*math.randomreal()+3; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0.0, 0.0, 0.001, 0); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); testfunc3(state); } minasa.minasaresults(state, ref x, rep); converror = converror | rep.terminationtype!=2; for(i=0; i<=2; i++) { x[i] = 3*math.randomreal()+3; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0.0, 0.0, 0.0, 3); minasa.minasasetalgorithm(state, algotype); while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); testfunc3(state); } minasa.minasaresults(state, ref x, rep); converror = converror | !((rep.terminationtype==5 & rep.iterationscount==3) | rep.terminationtype==7); // // Other properties // // // Other properties: // 1. test reports (F should form monotone sequence) // 2. test maximum step // n = 50; x = new double[n]; xlast = new double[n]; bndl = new double[n]; bndu = new double[n]; for(i=0; i<=n-1; i++) { x[i] = 1; xlast[i] = math.randomreal(); bndl[i] = -100000; bndu[i] = 100000; } minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 0, 0, 0, 100); minasa.minasasetxrep(state, true); fprev = math.maxrealnumber; while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); if( state.needfg ) { state.f = 0; for(i=0; i<=n-1; i++) { state.f = state.f+math.sqr((1+i)*state.x[i]); state.g[i] = 2*(1+i)*state.x[i]; } } if( state.xupdated ) { othererrors = othererrors | (double)(state.f)>(double)(fprev); if( (double)(fprev)==(double)(math.maxrealnumber) ) { for(i=0; i<=n-1; i++) { othererrors = othererrors | (double)(state.x[i])!=(double)(x[i]); } } fprev = state.f; for(i_=0; i_<=n-1;i_++) { xlast[i_] = state.x[i_]; } } } minasa.minasaresults(state, ref x, rep); for(i=0; i<=n-1; i++) { othererrors = othererrors | (double)(x[i])!=(double)(xlast[i]); } n = 1; x = new double[n]; bndl = new double[n]; bndu = new double[n]; x[0] = 100; bndl[0] = -1000000; bndu[0] = 1000000; stpmax = 0.05+0.05*math.randomreal(); minasa.minasacreate(n, x, bndl, bndu, state); minasa.minasasetcond(state, 1.0E-9, 0, 0, 0); minasa.minasasetstpmax(state, stpmax); minasa.minasasetxrep(state, true); xprev = x[0]; while( minasa.minasaiteration(state) ) { checkbounds(state.x, bndl, bndu, n, ref othererrors); if( state.needfg ) { state.f = Math.Exp(state.x[0])+Math.Exp(-state.x[0]); state.g[0] = Math.Exp(state.x[0])-Math.Exp(-state.x[0]); othererrors = othererrors | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax); } if( state.xupdated ) { othererrors = othererrors | (double)(Math.Abs(state.x[0]-xprev))>(double)((1+Math.Sqrt(math.machineepsilon))*stpmax); xprev = state.x[0]; } } } // // end // waserrors = ((referror | converror) | othererrors) | restartserror; if( !silent ) { System.Console.Write("TESTING ASA OPTIMIZATION"); System.Console.WriteLine(); System.Console.Write("REFERENCE PROBLEMS: "); if( referror ) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("RESTARTS: "); if( restartserror ) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("CONVERGENCE PROPERTIES: "); if( converror ) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("OTHER PROPERTIES: "); if( othererrors ) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } if( waserrors ) { System.Console.Write("TEST FAILED"); System.Console.WriteLine(); } else { System.Console.Write("TEST PASSED"); System.Console.WriteLine(); } System.Console.WriteLine(); System.Console.WriteLine(); } result = !waserrors; return result; }
public static int Main(string[] args) { int n = 0; int i = 0; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); double[] s = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double x = 0; double y = 0; double z = 0; // // Function being minimized: // F = x+2y+3z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1. // n = 3; s = new double[n]; bndl = new double[n]; bndu = new double[n]; for(i=0; i<=n-1; i++) { s[i] = 1; bndl[i] = 0; bndu[i] = 1; } minasa.minasacreate(n, ref s, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.00001, 0); minasa.minasasetxrep(ref state, true); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.Write("F = x+2y+3z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1"); System.Console.WriteLine(); System.Console.Write("OPTIMIZATION STARTED"); System.Console.WriteLine(); while( minasa.minasaiteration(ref state) ) { if( state.needfg ) { x = state.x[0]; y = state.x[1]; z = state.x[2]; state.f = x+2*y+3*z; state.g[0] = 1; state.g[1] = 2; state.g[2] = 3; } if( state.xupdated ) { System.Console.Write(" F("); System.Console.Write("{0,4:F2}",state.x[0]); System.Console.Write(","); System.Console.Write("{0,4:F2}",state.x[1]); System.Console.Write(","); System.Console.Write("{0,4:F2}",state.x[2]); System.Console.Write(")="); System.Console.Write("{0,0:F3}",state.f); System.Console.WriteLine(); } } System.Console.Write("OPTIMIZATION STOPPED"); System.Console.WriteLine(); minasa.minasaresults(ref state, ref s, ref rep); // // output results // System.Console.Write("X = "); System.Console.Write("{0,4:F2}",s[0]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Y = "); System.Console.Write("{0,4:F2}",s[1]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Z = "); System.Console.Write("{0,4:F2}",s[2]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return 0; }
public static int Main(string[] args) { int n = 0; int i = 0; minasa.minasastate state = new minasa.minasastate(); minasa.minasareport rep = new minasa.minasareport(); double[] s = new double[0]; double[] bndl = new double[0]; double[] bndu = new double[0]; double x = 0; double y = 0; double z = 0; // // Function being minimized: // F = x+4y+9z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1. // // Take a look at MinASASetStpMax() - it restricts step length by // a small value, so we can see the current point traveling through // a feasible set, sticking to its bounds. // n = 3; s = new double[n]; bndl = new double[n]; bndu = new double[n]; for(i=0; i<=n-1; i++) { s[i] = 1; bndl[i] = 0; bndu[i] = 1; } minasa.minasacreate(n, ref s, ref bndl, ref bndu, ref state); minasa.minasasetcond(ref state, 0.0, 0.0, 0.00001, 0); minasa.minasasetxrep(ref state, true); minasa.minasasetstpmax(ref state, 0.2); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.Write("F = x+4y+9z subject to 0<=x<=1, 0<=y<=1, 0<=z<=1"); System.Console.WriteLine(); System.Console.Write("OPTIMIZATION STARTED"); System.Console.WriteLine(); while( minasa.minasaiteration(ref state) ) { if( state.needfg ) { x = state.x[0]; y = state.x[1]; z = state.x[2]; state.f = x+4*y+9*z; state.g[0] = 1; state.g[1] = 4; state.g[2] = 9; } if( state.xupdated ) { System.Console.Write(" F("); System.Console.Write("{0,4:F2}",state.x[0]); System.Console.Write(", "); System.Console.Write("{0,4:F2}",state.x[1]); System.Console.Write(", "); System.Console.Write("{0,4:F2}",state.x[2]); System.Console.Write(") = "); System.Console.Write("{0,0:F3}",state.f); System.Console.WriteLine(); } } System.Console.Write("OPTIMIZATION STOPPED"); System.Console.WriteLine(); minasa.minasaresults(ref state, ref s, ref rep); // // output results // System.Console.Write("X = "); System.Console.Write("{0,4:F2}",s[0]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Y = "); System.Console.Write("{0,4:F2}",s[1]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.Write("Z = "); System.Console.Write("{0,4:F2}",s[2]); System.Console.Write(" (should be 0.00)"); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return 0; }