LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
LinearExpressionBuilder.cs
Go to the documentation of this file.
1using System;
3
5
6/// <summary>
7/// Provides a fluent builder for linear expressions.
8/// </summary>
9public sealed class LinearExpressionBuilder
10{
11 private readonly LinearExpression _expression =
12 new();
13
14 /// <summary>
15 /// Gets the expression currently being built.
16 /// </summary>
18 _expression;
19
20 /// <summary>
21 /// Adds a variable term to the expression.
22 /// </summary>
23 /// <param name="variable">
24 /// Referenced mathematical variable.
25 /// </param>
26 /// <param name="coefficient">
27 /// Linear coefficient.
28 /// </param>
29 /// <returns>
30 /// Current builder.
31 /// </returns>
32 /// <exception cref="ArgumentNullException">
33 /// Thrown when <paramref name="variable"/> is
34 /// <see langword="null"/>.
35 /// </exception>
37 MathematicalVariable variable,
38 double coefficient = 1.0)
39 {
40 ArgumentNullException.ThrowIfNull(
41 variable);
42
43 variable.EnsureValid();
44
45 _expression.AddTerm(
46 variable.Id,
47 coefficient);
48
49 return this;
50 }
51
52 /// <summary>
53 /// Subtracts a variable term from the expression.
54 /// </summary>
55 /// <param name="variable">
56 /// Referenced mathematical variable.
57 /// </param>
58 /// <param name="coefficient">
59 /// Positive coefficient to subtract.
60 /// </param>
61 /// <returns>
62 /// Current builder.
63 /// </returns>
64 /// <exception cref="ArgumentNullException">
65 /// Thrown when <paramref name="variable"/> is
66 /// <see langword="null"/>.
67 /// </exception>
68 /// <exception cref="ArgumentOutOfRangeException">
69 /// Thrown when <paramref name="coefficient"/> is negative.
70 /// </exception>
72 MathematicalVariable variable,
73 double coefficient = 1.0)
74 {
75 ArgumentNullException.ThrowIfNull(
76 variable);
77
78 if (coefficient < 0.0)
79 {
80 throw new ArgumentOutOfRangeException(
81 nameof(coefficient),
82 coefficient,
83 "The coefficient to subtract cannot be negative.");
84 }
85
86 return Add(
87 variable,
88 -coefficient);
89 }
90
91 /// <summary>
92 /// Adds a constant value to the expression.
93 /// </summary>
94 /// <param name="value">
95 /// Constant value to add.
96 /// </param>
97 /// <returns>
98 /// Current builder.
99 /// </returns>
101 double value)
102 {
103 _expression.AddConstant(
104 value);
105
106 return this;
107 }
108
109 /// <summary>
110 /// Subtracts a constant value from the expression.
111 /// </summary>
112 /// <param name="value">
113 /// Constant value to subtract.
114 /// </param>
115 /// <returns>
116 /// Current builder.
117 /// </returns>
119 double value)
120 {
121 _expression.AddConstant(
122 -value);
123
124 return this;
125 }
126
127 /// <summary>
128 /// Adds another linear expression.
129 /// </summary>
130 /// <param name="expression">
131 /// Expression to add.
132 /// </param>
133 /// <returns>
134 /// Current builder.
135 /// </returns>
136 /// <exception cref="ArgumentNullException">
137 /// Thrown when <paramref name="expression"/> is
138 /// <see langword="null"/>.
139 /// </exception>
141 LinearExpression expression)
142 {
143 ArgumentNullException.ThrowIfNull(
144 expression);
145
146 _expression.Add(
147 expression);
148
149 return this;
150 }
151
152 /// <summary>
153 /// Subtracts another linear expression.
154 /// </summary>
155 /// <param name="expression">
156 /// Expression to subtract.
157 /// </param>
158 /// <returns>
159 /// Current builder.
160 /// </returns>
161 /// <exception cref="ArgumentNullException">
162 /// Thrown when <paramref name="expression"/> is
163 /// <see langword="null"/>.
164 /// </exception>
166 LinearExpression expression)
167 {
168 ArgumentNullException.ThrowIfNull(
169 expression);
170
171 LinearExpression negatedExpression =
172 expression.Clone();
173
174 negatedExpression.MultiplyBy(
175 -1.0);
176
177 _expression.Add(
178 negatedExpression);
179
180 return this;
181 }
182
183 /// <summary>
184 /// Multiplies the entire expression by a scalar.
185 /// </summary>
186 /// <param name="factor">
187 /// Scalar multiplier.
188 /// </param>
189 /// <returns>
190 /// Current builder.
191 /// </returns>
193 double factor)
194 {
195 _expression.MultiplyBy(
196 factor);
197
198 return this;
199 }
200
201 /// <summary>
202 /// Clears the current expression.
203 /// </summary>
204 /// <returns>
205 /// Current builder.
206 /// </returns>
208 {
209 _expression.Clear();
210
211 return this;
212 }
213
214 /// <summary>
215 /// Builds and validates the linear expression.
216 /// </summary>
217 /// <param name="clone">
218 /// Indicates whether an independent clone should be
219 /// returned.
220 /// </param>
221 /// <returns>
222 /// Valid linear expression.
223 /// </returns>
225 bool clone = true)
226 {
227 _expression.EnsureValid();
228
229 return clone
230 ? _expression.Clone()
231 : _expression;
232 }
233}
Provides a fluent builder for linear expressions.
LinearExpression Expression
Gets the expression currently being built.
LinearExpressionBuilder Subtract(MathematicalVariable variable, double coefficient=1.0)
Subtracts a variable term from the expression.
LinearExpressionBuilder Add(MathematicalVariable variable, double coefficient=1.0)
Adds a variable term to the expression.
LinearExpressionBuilder Clear()
Clears the current expression.
LinearExpressionBuilder MultiplyBy(double factor)
Multiplies the entire expression by a scalar.
LinearExpressionBuilder AddConstant(double value)
Adds a constant value to the expression.
LinearExpressionBuilder Add(LinearExpression expression)
Adds another linear expression.
LinearExpressionBuilder Subtract(LinearExpression expression)
Subtracts another linear expression.
LinearExpressionBuilder SubtractConstant(double value)
Subtracts a constant value from the expression.
LinearExpression Build(bool clone=true)
Builds and validates the linear expression.
Represents a linear expression composed of variable terms and a constant value.
void MultiplyBy(double factor)
Multiplies the entire expression by a scalar.
LinearExpression Clone()
Creates an independent copy of this expression.
void EnsureValid()
Validates the linear expression.
Represents one decision variable in a mathematical optimization model.
int Id
Gets or sets the unique variable identifier within the mathematical model.
void EnsureValid()
Validates the mathematical variable.