LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelBuildContext.cs
Go to the documentation of this file.
1using System;
3
5
6/// <summary>
7/// Provides the shared state required while constructing a
8/// solver-independent mathematical model.
9/// </summary>
10/// <remarks>
11/// The context combines the mathematical model builder and the
12/// domain-key variable registry. It ensures that every business
13/// decision variable added through the context is immediately
14/// registered and can later be retrieved while generating
15/// constraints.
16/// </remarks>
18{
19 /// <summary>
20 /// Initializes a mathematical-model build context.
21 /// </summary>
22 /// <param name="modelName">
23 /// Mathematical model name.
24 /// </param>
26 string modelName)
27 {
30 modelName);
31
34 }
35
36 /// <summary>
37 /// Gets the mathematical model builder.
38 /// </summary>
40 {
41 get;
42 }
43
44 /// <summary>
45 /// Gets the registry that maps business-domain keys to
46 /// mathematical variables.
47 /// </summary>
52
53 /// <summary>
54 /// Gets the mathematical model currently being built.
55 /// </summary>
57 ModelBuilder.Model;
58
59 /// <summary>
60 /// Sets the model description.
61 /// </summary>
62 /// <param name="description">
63 /// Model description.
64 /// </param>
65 /// <returns>
66 /// Current build context.
67 /// </returns>
69 string description)
70 {
71 ModelBuilder.SetDescription(
72 description);
73
74 return this;
75 }
76
77 /// <summary>
78 /// Sets the mathematical objective.
79 /// </summary>
80 /// <param name="objective">
81 /// Objective to assign.
82 /// </param>
83 /// <returns>
84 /// Current build context.
85 /// </returns>
86 /// <exception cref="ArgumentNullException">
87 /// Thrown when <paramref name="objective"/> is
88 /// <see langword="null"/>.
89 /// </exception>
91 MathematicalObjective objective)
92 {
93 ArgumentNullException.ThrowIfNull(
94 objective);
95
96 ModelBuilder.SetObjective(
97 objective);
98
99 return this;
100 }
101
102 /// <summary>
103 /// Creates, adds, and registers a mathematical variable.
104 /// </summary>
105 /// <param name="name">
106 /// Variable name.
107 /// </param>
108 /// <param name="domainKey">
109 /// Unique business-domain key.
110 /// </param>
111 /// <param name="variableType">
112 /// Variable domain.
113 /// </param>
114 /// <param name="lowerBound">
115 /// Variable lower bound.
116 /// </param>
117 /// <param name="upperBound">
118 /// Variable upper bound.
119 /// </param>
120 /// <param name="description">
121 /// Optional variable description.
122 /// </param>
123 /// <returns>
124 /// Created and registered mathematical variable.
125 /// </returns>
127 string name,
128 string domainKey,
129 MathematicalVariableType variableType,
130 double lowerBound = 0.0,
131 double upperBound = double.PositiveInfinity,
132 string description = "")
133 {
134 MathematicalVariable variable =
135 ModelBuilder.AddVariable(
136 name,
137 variableType,
138 lowerBound,
139 upperBound,
140 domainKey,
141 description);
142
143 try
144 {
145 VariableRegistry.Register(
146 variable);
147 }
148 catch
149 {
150 ModelBuilder.Model.Variables.Remove(
151 variable);
152
153 throw;
154 }
155
156 return variable;
157 }
158
159 /// <summary>
160 /// Adds a linear constraint to the mathematical model.
161 /// </summary>
162 /// <param name="name">
163 /// Constraint name.
164 /// </param>
165 /// <param name="leftHandSide">
166 /// Left-hand-side linear expression.
167 /// </param>
168 /// <param name="sense">
169 /// Constraint relational sense.
170 /// </param>
171 /// <param name="rightHandSide">
172 /// Right-hand-side constant.
173 /// </param>
174 /// <param name="domainKey">
175 /// Optional business-domain key.
176 /// </param>
177 /// <param name="description">
178 /// Optional constraint description.
179 /// </param>
180 /// <returns>
181 /// Created linear constraint.
182 /// </returns>
184 string name,
185 LinearExpression leftHandSide,
187 double rightHandSide,
188 string domainKey = "",
189 string description = "")
190 {
191 return ModelBuilder.AddConstraint(
192 name,
193 leftHandSide,
194 sense,
195 rightHandSide,
196 domainKey,
197 description);
198 }
199
200 /// <summary>
201 /// Gets a required mathematical variable from its
202 /// business-domain key.
203 /// </summary>
204 /// <param name="domainKey">
205 /// Business-domain key.
206 /// </param>
207 /// <returns>
208 /// Registered mathematical variable.
209 /// </returns>
211 string domainKey)
212 {
213 return VariableRegistry.GetRequired(
214 domainKey);
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 return ModelBuilder.Build(
231 clone);
232 }
233}
MathematicalModel Build(bool clone=true)
Builds and validates the mathematical model.
MathematicalVariable AddVariable(string name, string domainKey, MathematicalVariableType variableType, double lowerBound=0.0, double upperBound=double.PositiveInfinity, string description="")
Creates, adds, and registers a mathematical variable.
MathematicalModel Model
Gets the mathematical model currently being built.
MathematicalModelBuildContext SetObjective(MathematicalObjective objective)
Sets the mathematical objective.
MathematicalModelBuilder ModelBuilder
Gets the mathematical model builder.
LinearConstraint AddConstraint(string name, LinearExpression leftHandSide, MathematicalConstraintSense sense, double rightHandSide, string domainKey="", string description="")
Adds a linear constraint to the mathematical model.
MathematicalModelBuildContext SetDescription(string description)
Sets the model description.
MathematicalVariable GetVariable(string domainKey)
Gets a required mathematical variable from its business-domain key.
MathematicalModelBuildContext(string modelName)
Initializes a mathematical-model build context.
MathematicalVariableRegistry VariableRegistry
Gets the registry that maps business-domain keys to mathematical variables.
Builds a solver-independent mathematical model while assigning unique variable and constraint identif...
Maintains the correspondence between business-domain keys and mathematical variables during model con...
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.
Represents the objective function of a mathematical optimization model.
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.