static void Main(string[] args) { // The optimization algorithm Algorithm main_alg = Algorithm.AUGLAG; // The local/subsidiary optimization algorithm, which may or may not be used (see NLOpt wiki) Algorithm secondary_alg = Algorithm.GN_CRS2_LM; // Create optimization object, setting algorithm type and number of variables NLOptWrapper wrapper = new NLOptWrapper(main_alg, 2); // Turn verbose (console) output on or off wrapper.VERBOSE_OUTPUT = VERBOSE_OUTPUT; // Set some stopping criteria wrapper.MaxTime = 100; //in seconds wrapper.XTolRelative = 1e-4; wrapper.FTolAbsolute = 1e-2; wrapper.MaxEval = 5000; wrapper.StopVal = 0.55; // Create design vector and set initial guess double[] x = new double[2] { 1.234, 5.678 }; // Apply lower bounds on the design vector wrapper.SetUpperBounds(new double[2] { 10000, 10000 }); wrapper.SetLowerBounds(new double[2] { -10000, 0.0001 }); // Create local optimization object, if appropriate (all AUGLAG formulations and MLSL require this) // ...Note that this has to be done before the main wrapper's objectives or constraints are set // to prevent crashing. Don't know exactly why yet. Part of the joys of mixing unmanaged and managed code! if (main_alg.ToString().Contains("AUGLAG") || main_alg.ToString().Contains("MLSL")) { NLOptWrapper local_wrapper = new NLOptWrapper(secondary_alg, wrapper.Dimension); /* add stopping criteria */ local_wrapper.XTolRelative = 1e-4; local_wrapper.FTolRelative = 1e-2; // Haven't figured out whether the local or main algorithm stopping criteria dominate, and when. // Need to inspect nlopt source code to be sure of specifics, and differences between AUGLAG and MLSL local_wrapper.VERBOSE_OUTPUT = VERBOSE_OUTPUT; wrapper.SetLocalOptimizer(local_wrapper); } // Delegate the objective function. Data can be passed in as type System.Object and cast in the objective function wrapper.SetMinObjective(new FunctionDelegate(Objective), null); // Add inequality constraints. Data can be passed in as type System.Object. Feasibility tolerance passed as an argument my_constraint_data[] data = new my_constraint_data[2] { new my_constraint_data(2, 0), new my_constraint_data(-1, 1) }; wrapper.AddInequalityConstraint(new FunctionDelegate(Constraint), data[0], 1e-8); wrapper.AddInequalityConstraint(new FunctionDelegate(Constraint), data[1], 1e-8); // create variable to store min objective double minf = 0; //Run the optimization, passing minf by reference. NLOptDotNet.Result returned Result r = wrapper.Optimize(x, ref minf); Console.WriteLine("\nFound minimum after " + neval + " objective function evaluations"); Console.WriteLine("Found minimum at f(" + x[0] + ", " + x[1] + ") = " + minf); Console.WriteLine("\nNLOpt Result: " + r.ToString()); }
public void Configure(string algorithmType, int size, string objectiveType) { Algorithm alg = Enum.Parse(typeof(Algorithm), algorithmType.ToUpper()); _optimizer = new NLOptWrapper(alg, size); // Parse out objectiveType. It can be either a string for // MINIMUM_OBJECTIVE or MAXIMUM_OBJECTIVE FunctionType function = Enum.Parse(typeof(FunctionType), objectiveType.ToUpper()); _optimizer.AddFunction(function, _objectiveFunction, null, 0.0); }