public static void Run(CostFunction f, int max_iterations = 200) { BFGS s = new BFGS(); double[] x_0 = f.CreateRandomSolution(); s.SolutionUpdated += (best_solution, step) => { Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost); }; s.Minimize(x_0, f, max_iterations); }
static void Main(string[] args) { RandomGenerator generator = new RandomGenerator(); //RandomGenerator generator = new RandomGenerator(3); ReadData(@"d:\Projects\HeroesModel\HeroesModel\data.csv"); //GenerateModelData(generator, 1000); data.Sort((x, y) => { if (x.red != y.red) { return(x.red.CompareTo(y.red)); } return(x.blue.CompareTo(y.blue)); }); PrintData(); BFGS bfgs = new BFGS(TOTAL_PARAMS + 1 + TOWN_TYPES * TOWN_TYPES * 4, LogLikelyhood, LogLikelyHoodGradient); //BFGS bfgs = new BFGS(TOTAL_PARAMS + 1, LogLikelyhood, LogLikelyHoodGradient); //bfgs.FunctionTolerance = EPS; DateTime startTime = DateTime.Now; ManualResetEvent interrupted = new ManualResetEvent(false); Thread reportThread = new Thread(() => { while (!interrupted.WaitOne(10000)) { Console.WriteLine($"Time elapsed {(DateTime.Now - startTime)}, iterations complete {bfgs.IterationsDone}"); } }); reportThread.Start(); //bfgs.Minimize(new Vector(TOTAL_PARAMS, 0.01)); //RandomGenerator generator = new RandomGenerator(1); //RandomGenerator generator = new RandomGenerator(1); Normal normal = new Normal(generator, 0, 0.1); bfgs.Minimize(normal.Sample(TOTAL_PARAMS + 1 + TOWN_TYPES * TOWN_TYPES * 4)); //bfgs.Minimize(normal.Sample(TOTAL_PARAMS + 1)); interrupted.Set(); reportThread.Join(); Vector result = bfgs.MinimumPoint; double min = -LogLikelyhood(result); Console.WriteLine($"Converged. Time elapsed {(DateTime.Now - startTime)}. Iterations: {bfgs.IterationsDone}"); for (double money = -0.5; money <= 0.5; money += 0.1) { Console.WriteLine($"Red money {(money + 1) * 10000}"); PrintProbabilities(result, money); } PrintCoeficients(result); EstimateInputData(result, @"d:\Projects\HeroesModel\display\data.js"); PrintCoefficients(result, @"d:\Projects\HeroesModel\display\coefficients.js"); //GaussKronrodSingular integration = new GaussKronrodSingular(); //integration.Integrate(x => ConditionalDensity(x, 0, 1, 0, 1), Double.NegativeInfinity, Double.PositiveInfinity, 100); //Console.WriteLine(integration.Result); }