LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelBuilder.cs
Go to the documentation of this file.
1using System;
3
5
6/// <summary>
7/// Builds a solver-independent mathematical model while
8/// assigning unique variable and constraint identifiers.
9/// </summary>
10public sealed class MathematicalModelBuilder
11{
12 private readonly MathematicalModel _model;
13
14 private int _nextVariableId =
15 1;
16
17 private int _nextConstraintId =
18 1;
19
20 /// <summary>
21 /// Initializes a mathematical-model builder.
22 /// </summary>
23 /// <param name="modelName">
24 /// Mathematical model name.
25 /// </param>
26 /// <exception cref="ArgumentException">
27 /// Thrown when <paramref name="modelName"/> is empty.
28 /// </exception>
30 string modelName)
31 {
32 if (string.IsNullOrWhiteSpace(
33 modelName))
34 {
35 throw new ArgumentException(
36 "A mathematical model name is required.",
37 nameof(modelName));
38 }
39
40 _model =
42 {
43 Name =
44 modelName.Trim()
45 };
46 }
47
48 /// <summary>
49 /// Gets the model currently being built.
50 /// </summary>
52 _model;
53
54 /// <summary>
55 /// Sets the model description.
56 /// </summary>
57 /// <param name="description">
58 /// Model description.
59 /// </param>
60 /// <returns>
61 /// Current builder.
62 /// </returns>
64 string description)
65 {
66 _model.Description =
67 description?.Trim() ??
68 string.Empty;
69
70 return this;
71 }
72
73 /// <summary>
74 /// Sets the mathematical objective.
75 /// </summary>
76 /// <param name="objective">
77 /// Objective to assign.
78 /// </param>
79 /// <returns>
80 /// Current builder.
81 /// </returns>
82 /// <exception cref="ArgumentNullException">
83 /// Thrown when <paramref name="objective"/> is
84 /// <see langword="null"/>.
85 /// </exception>
87 MathematicalObjective objective)
88 {
89 ArgumentNullException.ThrowIfNull(
90 objective);
91
92 objective.EnsureValid();
93
94 _model.Objective =
95 objective;
96
97 return this;
98 }
99
100 /// <summary>
101 /// Creates and adds a mathematical variable.
102 /// </summary>
103 /// <param name="name">
104 /// Variable name.
105 /// </param>
106 /// <param name="variableType">
107 /// Variable domain.
108 /// </param>
109 /// <param name="lowerBound">
110 /// Variable lower bound.
111 /// </param>
112 /// <param name="upperBound">
113 /// Variable upper bound.
114 /// </param>
115 /// <param name="domainKey">
116 /// Optional business-domain key.
117 /// </param>
118 /// <param name="description">
119 /// Optional description.
120 /// </param>
121 /// <returns>
122 /// Created mathematical variable.
123 /// </returns>
125 string name,
126 MathematicalVariableType variableType,
127 double lowerBound = 0.0,
128 double upperBound = double.PositiveInfinity,
129 string domainKey = "",
130 string description = "")
131 {
132 var variable =
134 _nextVariableId,
135 name,
136 variableType,
137 lowerBound,
138 upperBound)
139 {
140 DomainKey =
141 domainKey?.Trim() ??
142 string.Empty,
143
144 Description =
145 description?.Trim() ??
146 string.Empty
147 };
148
149 _model.AddVariable(
150 variable);
151
152 _nextVariableId++;
153
154 return variable;
155 }
156
157 /// <summary>
158 /// Creates and adds a linear constraint.
159 /// </summary>
160 /// <param name="name">
161 /// Constraint name.
162 /// </param>
163 /// <param name="leftHandSide">
164 /// Left-hand-side expression.
165 /// </param>
166 /// <param name="sense">
167 /// Constraint relational sense.
168 /// </param>
169 /// <param name="rightHandSide">
170 /// Right-hand-side constant.
171 /// </param>
172 /// <param name="domainKey">
173 /// Optional business-domain key.
174 /// </param>
175 /// <param name="description">
176 /// Optional description.
177 /// </param>
178 /// <returns>
179 /// Created linear constraint.
180 /// </returns>
182 string name,
183 LinearExpression leftHandSide,
185 double rightHandSide,
186 string domainKey = "",
187 string description = "")
188 {
189 ArgumentNullException.ThrowIfNull(
190 leftHandSide);
191
192 var constraint =
194 _nextConstraintId,
195 name,
196 leftHandSide,
197 sense,
198 rightHandSide)
199 {
200 DomainKey =
201 domainKey?.Trim() ??
202 string.Empty,
203
204 Description =
205 description?.Trim() ??
206 string.Empty
207 };
208
209 _model.AddConstraint(
210 constraint);
211
212 _nextConstraintId++;
213
214 return constraint;
215 }
216
217 /// <summary>
218 /// Builds and validates the mathematical model.
219 /// </summary>
220 /// <param name="clone">
221 /// Indicates whether an independent clone should be
222 /// returned.
223 /// </param>
224 /// <returns>
225 /// Valid mathematical model.
226 /// </returns>
228 bool clone = true)
229 {
230 _model.EnsureValid();
231
232 return clone
233 ? _model.Clone()
234 : _model;
235 }
236}
LinearConstraint AddConstraint(string name, LinearExpression leftHandSide, MathematicalConstraintSense sense, double rightHandSide, string domainKey="", string description="")
Creates and adds a linear constraint.
MathematicalVariable AddVariable(string name, MathematicalVariableType variableType, double lowerBound=0.0, double upperBound=double.PositiveInfinity, string domainKey="", string description="")
Creates and adds a mathematical variable.
MathematicalModelBuilder SetDescription(string description)
Sets the model description.
MathematicalModel Model
Gets the model currently being built.
MathematicalModelBuilder SetObjective(MathematicalObjective objective)
Sets the mathematical objective.
MathematicalModel Build(bool clone=true)
Builds and validates the mathematical model.
MathematicalModelBuilder(string modelName)
Initializes a mathematical-model builder.
Represents one linear constraint in a mathematical optimization model.
Represents a linear expression composed of variable terms and a constant value.
Represents a solver-independent mathematical optimization model.
void EnsureValid()
Validates the complete mathematical model.
Represents the objective function of a mathematical optimization model.
void EnsureValid()
Validates the mathematical objective.
Represents one decision variable in a mathematical optimization model.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.
MathematicalVariableType
Identifies the domain of a mathematical decision variable.