Ejemplo n.º 1
0
        public bool RunSolver(List <Variable> variables, Func <IList <decimal>, double> evaluate, string preset, string expertsettings, string installFolder, string documentPath)
        {
            var settings = _presets[preset];

            //System.Windows.Forms.MessageBox.Show(expertsettings);     //use expertsettings to input custom solver parameters

            var dvar    = variables.Count;
            var lb      = new double[dvar];
            var ub      = new double[dvar];
            var integer = new bool[dvar];

            for (var i = 0; i < dvar; i++)
            {
                lb[i]      = Convert.ToDouble(variables[i].LowerB);
                ub[i]      = Convert.ToDouble(variables[i].UpperB);
                integer[i] = variables[i].Integer;
            }

            Func <double[], double> eval = x =>
            {
                var decis = x.Select(Convert.ToDecimal).ToList();
                return(evaluate(decis));
            };

            try
            {
                var seed     = (int)settings["seed"];
                var stepsize = settings["stepsize"];
                var itermax  = (int)settings["itermax"];
                var hc       = new HillclimberAlgorithm(lb, ub, stepsize, itermax, eval, seed);
                hc.Solve();
                Xopt  = hc.get_Xoptimum();
                Fxopt = hc.get_fxoptimum();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool RunSolver(List <Variable> variables, Func <IList <decimal>, double> evaluate, string preset, string expertsettings, string installFolder, string documentPath)
        {
            var settings = _presets[preset];

            //System.Windows.Forms.MessageBox.Show(expertsettings);     //use expertsettings to input custom solver parameters

            int?seedin = null;
            //string [] expsets = expertsettings.Split(';');
            //foreach (string strexp in expsets)
            //{
            //    string[] stre = strexp.Split('=');
            //    if(string.Equals(stre[0],"seed"))
            //    {
            //        seedin = Convert.ToInt16(stre[1]);
            //    }
            //}

            var random = new Random();

            seedin = random.Next(-32768, 32767);

            var dvar    = variables.Count;
            var lb      = new double[dvar];
            var ub      = new double[dvar];
            var integer = new bool[dvar];

            for (var i = 0; i < dvar; i++)
            {
                lb[i]      = Convert.ToDouble(variables[i].LowerB);
                ub[i]      = Convert.ToDouble(variables[i].UpperB);
                integer[i] = variables[i].Integer;
            }

            Func <double[], double> eval = x =>
            {
                var decis = x.Select(Convert.ToDecimal).ToList();
                return(evaluate(decis));
            };

            try
            {
                if (preset.Equals("SGA_n4_A") || preset.Equals("SGA_n4_B") || preset.Equals("SGA_n4_C") || preset.Equals("SGA_WW"))
                {
                    Dictionary <string, object> GAsettings = new Dictionary <string, object>();
                    GAsettings.Add("maxgen", (int)settings["maxgen"]);
                    GAsettings.Add("popsize", (int)settings["popsize"]);
                    GAsettings.Add("k", settings["k"]);
                    GAsettings.Add("pcross", settings["pcross"]);
                    GAsettings.Add("pmut", settings["pmut"]);
                    GAsettings.Add("d", settings["d"]);
                    GAsettings.Add("r", settings["r"]);
                    GAsettings.Add("elite", settings["elite"]);
                    int seed;

                    if (seedin != null)
                    {
                        seed = Convert.ToInt16(seedin);
                    }
                    else
                    {
                        seed = (int)settings["seed"];
                    }

                    int itermax = (int)settings["itermax"];

                    var ga = new MetaheuristicsLibrary.SolversSO.SimpleGA(lb, ub, integer, itermax, eval, seed, GAsettings);
                    ga.solve();
                    Xopt  = ga.get_Xoptimum();
                    Fxopt = ga.get_fxoptimum();
                }
                else
                {
                    var seed     = (int)settings["seed"];
                    var stepsize = settings["stepsize"];
                    var itermax  = (int)settings["itermax"];
                    var hc       = new HillclimberAlgorithm(lb, ub, stepsize, itermax, eval, seed);
                    hc.Solve();
                    Xopt  = hc.get_Xoptimum();
                    Fxopt = hc.get_fxoptimum();
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }