本文整理汇总了C#中Solver.Start方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.Start方法的具体用法?C# Solver.Start怎么用?C# Solver.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.Start方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SolveBackPackProblemWith16RandomItems
public void SolveBackPackProblemWith16RandomItems()
{
const int tests = 10;
double bfFitness = 0.0;
double gsFitness = 0.0;
for (int n = 0; n < tests; n++)
{
var backPack = new BackPack(2000);
var items = BackPackEnvironmentTest.RandomItems(16, backPack.Volume, 200);
var environment = new BackPackEnvironment(backPack, items, 100);
var solver = new Solver<BackPackIndividual>(environment);
solver.Start(() => solver.CurrentGeneration > 10);
var bf = BruteForce(environment);
var gs = solver.CurrentOptimum;
Console.WriteLine(environment.RateFitness(bf));
Console.WriteLine(environment.RateFitness(gs));
bfFitness += environment.RateFitness(bf);
gsFitness += environment.RateFitness(gs);
}
// Should be atleast 90% of BF'ed fitness
Console.WriteLine(gsFitness / bfFitness);
Assert.IsTrue(bfFitness * 0.9
<= gsFitness);
}
示例2: DoSolve
/// <summary>
/// Solves the problem using a GA.
/// </summary>
/// <param name="problem"></param>
/// <returns></returns>
protected override IRoute DoSolve(OsmSharp.Tools.Math.TSP.Problems.IProblem problem)
{
//int population_size = 10;
//if (problem.Size < 100)
//{
// population_size = System.Math.Max(problem.Size * 3, 10);
// if (problem.Size < 10)
// {
// population_size = 1;
// }
//}
//if (problem.Size < 1000)
//{
// population_size = problem.Size / 4;
//}
// create the settings.
SolverSettings settings = new SolverSettings(
_stagnation_count,
_population,
1000000000,
_eltism,
_cross,
_mutation);
//List<IMutationOperation<List<int>, GeneticProblem, Fitness>> mutators = new List<IMutationOperation<int,GeneticProblem,Fitness>>();
////mutators.Add(new DefaultMutationOperation());
////mutators.Add(new BestPlacementMutationOperation());
//mutators.Add(new BestDetailedPlacementMutationOperation());
//List<double> probabilities = new List<double>();
//probabilities.Add(1);
////probabilities.Add(0.5);
////probabilities.Add(0.3);
//CombinedMutation<List<int>, GeneticProblem, Fitness> mutation = new CombinedMutation<int,GeneticProblem,Fitness>(
// StaticRandomGenerator.Get(),
// mutators,
// probabilities);
////SequentialContructiveCrossoverOperator cross_over = new SequentialContructiveCrossoverOperator();
//BestDetailedPlacementCrossOverOperation cross_over = new BestDetailedPlacementCrossOverOperation();
////BestPlacementCrossOverOperation cross_over = new BestPlacementCrossOverOperation();
////EdgeRecombinationCrossOverOperation cross_over = new EdgeRecombinationCrossOverOperation();
//BestPlacementGenerationOperation generation = new BestPlacementGenerationOperation();
////RandomGenerationOperation generation = new RandomGenerationOperation();
ISelector<List<int>, GeneticProblem, Fitness> selector = new RandomSelector<List<int>, GeneticProblem, Fitness>();
//ISelector<List<int>, GeneticProblem, Fitness> selector = new TournamentBasedSelector<List<int>, GeneticProblem, Fitness>(75, 0.01);
solver =
new Solver<List<int>, GeneticProblem, Fitness>(
new GeneticProblem(problem),
settings,
selector,
_mutation_operation,
_cross_over_operation,
_generation_operation,
new FitnessCalculator(),
true, false);
solver.NewFittest += new Solver<List<int>, GeneticProblem, Fitness>.NewFittestDelegate(solver_NewFittest);
solver.NewGeneration += new Solver<List<int>, GeneticProblem, Fitness>.NewGenerationDelegate(solver_NewGeneration);
List<int> result = new List<int>(solver.Start(null).Genomes);
result.Insert(0, 0);
return new SimpleAsymmetricRoute(result, true);
}
示例3: SolveBackPackProblemWith512RandomItems
public void SolveBackPackProblemWith512RandomItems()
{
var backPack = new BackPack(2000);
var items = BackPackEnvironmentTest.RandomItems(512, backPack.Volume, 100);
var environment = new BackPackEnvironment(backPack, items, individualCountAtStart: 100);
var solver = new Solver<BackPackIndividual>(environment);
double previous = 0;
int c = 0;
Func<bool> terminationCondition = () =>
{
if (solver.CurrentGeneration > 10)
{
double current = environment.RateFitness(solver.CurrentOptimum);
if (previous != current)
{
previous = current;
c = 0;
}
else
{
c++;
if (c > 3)
{
return true;
}
}
}
return false;
};
solver.Start(terminationCondition);
}
示例4: Main
public static void Main(string[] args)
{
const int backPackVolume = 2000;
const int highestItemVolume = backPackVolume;
const int highestItemValue = 100;
const int itemCount = 512;
const int individualCountAtStart = 100;
List<BackPackItem> randomItems = RandomItems(itemCount, highestItemVolume, highestItemValue);
for (int i = 0; i < randomItems.Count; i++)
{
BackPackItem item = randomItems[i];
Console.WriteLine("\n" + (i + 1));
Console.WriteLine("Value: " + item.Value + "\n" + "Volume: " + item.Volume + "\n\n");
}
BackPackEnvironment environment = new BackPackEnvironment(new BackPack(backPackVolume), randomItems, individualCountAtStart);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
double previous = 0;
int c = 0;
Solver<BackPackIndividual> solver = new Solver<BackPackIndividual>(environment);
Func<bool> terminationCondition = () =>
{
if (solver.CurrentGeneration > 10)
{
double current = environment.RateFitness(solver.CurrentOptimum);
if (previous != current)
{
previous = current;
c = 0;
}
else
{
c++;
if (c > 3)
{
return true;
}
}
}
return false;
};
solver.Start(terminationCondition);
stopwatch.Stop();
double gsNeeded = stopwatch.ElapsedMilliseconds;
Console.WriteLine("Genetic Solver Needed: " + gsNeeded + "ms");
BackPackIndividual fittest = solver.CurrentOptimum;
Console.WriteLine("Optimum: " + fittest);
Console.WriteLine("Optimum-Fitness: " + environment.RateFitness(fittest));
}
示例5: Solve
/// <summary>
/// Executes a solver procedure.
/// </summary>
/// <param name="problem"></param>
/// <returns></returns>
internal override MaxTimeSolution Solve(MaxTimeProblem problem)
{
// _customers = problem.Customers;
//float[] solutions = OsmSharp.Math.VRP.Core.BestPlacement.CheapestInsertionHelper.CalculateBestValues(
// problem, _customers);
generations = 0;
_max_generations = 10000000;
// calculate one tsp solution.
//Tools.Math.TSP.ISolver tsp_solver = new OsmSharp.Math.TSP.EdgeAssemblyGenetic.EdgeAssemblyCrossOverSolver(_population, _stagnation,
// new OsmSharp.Math.TSP.Genetic.Solver.Operations.Generation._3OptGenerationOperation(),
// new OsmSharp.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover(30,
// OsmSharp.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover.EdgeAssemblyCrossoverSelectionStrategyEnum.SingleRandom,
// true));
//IRoute tsp_solution = tsp_solver.Solve(new OsmSharp.Routing.VRP.NoDepot.MaxTime.TSPPlacement.TSPProblem(
// problem));
// initialize the generation.
IGenerationOperation<MaxTimeSolution, MaxTimeProblem, Fitness> generation =
//new SolverGenerationOperation(new TSPPlacement.TSPPlacementSolver<ResolvedType>(
// this.Router, this.Max, this.DeliveryTime, tsp_solution));
new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.Generation.RandomBestPlacement();
//new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>(
// this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, false, 1f, null, null));
//new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>(
// this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, true, 1f));
// initialize the crossover.
ICrossOverOperation<MaxTimeSolution, MaxTimeProblem, Fitness> cross_over =
new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.CrossOver.RouteExchangeOperation();
// initialize the mutation.
//IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness> mutation =
// new VehicleMutation();
List<IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness>> mutators =
new List<IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness>>();
mutators.Add(new VehicleMutation());
//mutators.Add(new ThreeOptMutation());
//mutators.Add(new RedivideRouteMutation());
mutators.Add(new RoutePartExchangeMutation());
if (_probabilities == null)
{
_probabilities = new List<double>();
_probabilities.Add(0.2);
_probabilities.Add(0.6);
_probabilities.Add(0.2);
}
CombinedMutation<MaxTimeSolution, MaxTimeProblem, Fitness> mutation = new CombinedMutation<MaxTimeSolution, MaxTimeProblem, Fitness>(
StaticRandomGenerator.Get(),
mutators,
_probabilities);
SolverSettings settings = new SolverSettings(_stagnation, _population, _max_generations,
_elitism_percentage, _cross_percentage, _mutation_percentage);
MaxTimeProblem genetic_problem = problem;// new MaxTimeProblem(max, problem, solutions);
Solver<MaxTimeSolution, MaxTimeProblem, Fitness> solver =
new Solver<MaxTimeSolution, MaxTimeProblem, Fitness>(genetic_problem, settings,
new TournamentBasedSelector<MaxTimeSolution, MaxTimeProblem, Fitness>(5, 0.5),
mutation, //new ThreeOptMutation(),
cross_over, // new RouteExchangeOperation(), //new RouteExchangeOperation(), //new RouteExchangeAndVehicleOperation(), // Order1CrossoverOperation()
generation, //new RandomBestPlacement(),//new RandomGeneration(), //new RandomBestPlacement(),
new FitnessCalculator());
solver.NewFittest += new Solver<MaxTimeSolution, MaxTimeProblem, Fitness>.NewFittestDelegate(solver_NewFittest);
//solver.NewGeneration += new Solver<MaxTimeSolution, Problem, Fitness>.NewGenerationDelegate(solver_NewGeneration);
Individual<MaxTimeSolution, MaxTimeProblem, Fitness> solution = solver.Start(null);
//this.solver_NewFittest(solution);
MaxTimeSolution routes = solution.Genomes;
long ticks_after = DateTime.Now.Ticks;
StringBuilder sizes = new StringBuilder();
foreach (int size in routes.Sizes)
{
sizes.Append(size);
sizes.Append(" ");
}
StringBuilder weights = new StringBuilder();
foreach (double weight in solution.Fitness.Weights)
{
weights.Append(weight.ToString(CultureInfo.InvariantCulture));
weights.Append(" ");
}
return routes;
}