public static double Solve(int population_size, int dimension_count, CostEvaluationMethod evaluator, CreateBeeMethod generator, out Bee global_best_solution, double[] lower_bounds = null, double[] upper_bounds = null, object constraints = null, double tolerance = 0.000001, int maxIterations = 100000) { BeeSwarm <Bee> solver = new BeeSwarm <Bee>(60, 15, 30, 20, 10, evaluator, generator); solver.LowerBounds = lower_bounds; solver.UpperBounds = upper_bounds; solver.Constraints = constraints; solver.LocalSearchRegion = new double[dimension_count]; for (int i = 0; i < dimension_count; ++i) { solver.LocalSearchRegion[i] = (upper_bounds[i] - lower_bounds[i]) / 1000; } solver.Initialize(); int iteration = 0; double cost_reduction = tolerance; double global_best_solution_cost = solver.GlobalBestSolutionCost; double prev_global_best_solution_cost = global_best_solution_cost; while (cost_reduction >= tolerance && iteration < maxIterations) { prev_global_best_solution_cost = global_best_solution_cost; solver.Iterate(); global_best_solution_cost = solver.GlobalBestSolutionCost; cost_reduction = prev_global_best_solution_cost - global_best_solution_cost; iteration++; } global_best_solution = solver.GlobalBestSolution.Clone() as Bee; return(global_best_solution_cost); }
public static double Solve(int dimension_count, CostEvaluationMethod evaluator, out Bee global_best_solution, double[] lower_bounds = null, double[] upper_bounds = null, int maxIterations = 100000, int displayEvery = 100, object constraints = null) { int scoutBeeCount = 60; int bestPatch = 15; int elitePatch = 30; BeeSwarm <Bee> solver = new BeeSwarm <Bee>(scoutBeeCount, bestPatch, elitePatch, 20, 10, evaluator); solver.LowerBounds = lower_bounds; solver.UpperBounds = upper_bounds; solver.Constraints = constraints; solver.LocalSearchRegion = new double[dimension_count]; for (int i = 0; i < dimension_count; ++i) { solver.LocalSearchRegion[i] = (upper_bounds[i] - lower_bounds[i]) / bestPatch; } solver.Initialize(); int iteration = 0; double global_best_solution_cost = solver.GlobalBestSolutionCost; double prev_global_best_solution_cost = global_best_solution_cost; while (iteration < maxIterations) { prev_global_best_solution_cost = global_best_solution_cost; solver.Iterate(); global_best_solution_cost = solver.GlobalBestSolutionCost; if (iteration % displayEvery == 0) { Console.WriteLine("Generation: {0}, Best Cost: {1}", iteration, global_best_solution_cost); } iteration++; } global_best_solution = solver.GlobalBestSolution.Clone() as Bee; return(global_best_solution_cost); }