LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
DebConstraintGaMathematicalModelOptimizer.cs
Go to the documentation of this file.
1using System.Diagnostics;
3using MetaheuristicsPlatform.Algorithms.Constraints.DebConstraintGa;
4using MetaheuristicsPlatform.Catalog;
5using MetaheuristicsPlatform.Constraints;
6using MetaheuristicsPlatform.Core;
7
9
11{
14 DebConstraintGaBridgeOptions? options = null,
15 CancellationToken cancellationToken = default)
16 {
17 ArgumentNullException.ThrowIfNull(
18 model);
19
20 options ??=
22
25
26 var encoding =
28 model,
29 options.Encoding);
30
31 var problem =
33 model,
34 encoding,
35 options.EqualityTolerance);
36
37 var optimizer =
38 new DebConstraintGaOptimizer();
39
40 if (!string.Equals(
41 optimizer.Descriptor.Id,
42 MetaheuristicAlgorithmIds.DebConstraintGa,
43 StringComparison.Ordinal))
44 {
45 throw new InvalidOperationException(
46 "Unexpected Deb constraint-GA algorithm identity.");
47 }
48
49 var parameters =
50 new DebConstraintGaParameters
51 {
52 PopulationSize =
53 options.PopulationSize,
54
55 MaximumGenerations =
56 options.MaximumGenerations,
57
58 CrossoverProbability =
59 options.CrossoverProbability,
60
61 MutationProbability =
62 options.MutationProbability,
63
64 DistributionIndex =
65 options.DistributionIndex
66 };
67
68 var platformOptions =
69 new OptimizationOptions
70 {
71 Seed =
72 options.Seed
73 };
74
75 var stopwatch =
76 Stopwatch.StartNew();
77
78 ConstrainedOptimizationResult platformResult =
79 optimizer.Optimize(
80 problem,
81 parameters,
82 platformOptions,
83 cancellationToken);
84
85 stopwatch.Stop();
86
87 double[] decoded =
88 problem.DecodeCandidate(
89 platformResult.Best.Solution);
90
91 double objective =
92 problem.EvaluateObjective(
93 platformResult.Best.Solution);
94
95 double scale =
96 Math.Max(
97 1.0,
98 Math.Abs(objective));
99
100 if (Math.Abs(
101 objective -
102 platformResult.Best.Objective) >
103 1.0e-9 * scale)
104 {
105 throw new InvalidOperationException(
106 "MetaheuristicsPlatform objective result does not match independent bridge evaluation.");
107 }
108
109 bool isFeasible =
110 platformResult.Best.Constraints.IsFeasible &&
111 problem.IsDecodedCandidateFeasible(
112 platformResult.Best.Solution);
113
115 optimizer.Descriptor.Id,
116 platformResult.Best.Solution,
117 decoded,
118 objective,
119 platformResult.Best.Constraints.TotalViolation,
120 isFeasible,
121 platformResult.Evaluations,
122 platformResult.Iterations,
123 platformResult.Seed,
124 stopwatch.Elapsed);
125 }
126}
DebConstraintGaBridgeResult Optimize(MathematicalModel model, DebConstraintGaBridgeOptions? options=null, CancellationToken cancellationToken=default)
Represents a solver-independent mathematical optimization model.