LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
PeriodicOperatingExpenditureBudgetConstraintFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using LotSizingDataModel.Instance;
8
10
11/// <summary>
12/// Builds one operating-expenditure budget constraint per planning period.
13/// </summary>
14/// <remarks>
15/// The constraint reuses the already assembled economic objective expression.
16/// Only positive objective coefficients associated with variables whose
17/// canonical domain key contains the current period are included.
18/// Negative revenue coefficients and non-period-indexed positive terms are
19/// intentionally excluded from this periodic operating envelope.
20/// </remarks>
21public sealed class
24{
25 public override string ConstraintFamilyId =>
26 "periodicOperatingExpenditureBudget";
27
28 public override bool IsEnabled(
29 LotSizingInstance instance,
31 {
32 ArgumentNullException.ThrowIfNull(instance);
33 ArgumentNullException.ThrowIfNull(options);
34
35 return
36 instance.SupplyChain
37 .PeriodicOperatingExpenditureBudget
38 is not null;
39 }
40
41 protected override ValueTask BuildConstraintsAsync(
42 LotSizingInstance instance,
45 CancellationToken cancellationToken)
46 {
47 var budget =
48 instance.SupplyChain
49 .PeriodicOperatingExpenditureBudget;
50
51 if (budget is null)
52 {
53 return ValueTask.CompletedTask;
54 }
55
56 MathematicalObjective objective =
57 context.Model.Objective;
58
59 if (
60 objective.Sense !=
61 ObjectiveSense.Minimize)
62 {
63 throw new InvalidOperationException(
64 "Periodic operating expenditure budget requires the " +
65 "standard minimized economic objective.");
66 }
67
68 for (
69 int period = 1;
70 period <= instance.PlanningHorizon;
71 period++)
72 {
73 cancellationToken.ThrowIfCancellationRequested();
74
75 var expression =
77
78 foreach (
79 LinearTerm term
80 in objective.Expression.Terms)
81 {
82 if (
83 term.Coefficient <=
85 {
86 continue;
87 }
88
89 MathematicalVariable variable =
90 context.Model.FindVariableById(
91 term.VariableId) ??
92 throw new InvalidOperationException(
93 $"Objective term references unknown variable " +
94 $"'{term.VariableId}'.");
95
96 if (
98 variable.DomainKey,
99 out MathematicalDomainKey? domainKey) ||
100 domainKey is null ||
101 !domainKey.TryGetInt32(
103 out int variablePeriod) ||
104 variablePeriod != period)
105 {
106 continue;
107 }
108
109 expression.Add(
110 variable,
111 term.Coefficient);
112 }
113
115 context,
116 $"periodicOperatingExpenditureBudget_t{period}",
117 expression.Build(),
118 MathematicalConstraintSense.LessThanOrEqual,
119 budget.GetBudget(period),
120 description:
121 "Maximum period-indexed positive economic expenditure.");
122 }
123
124 return ValueTask.CompletedTask;
125 }
126}
Provides a fluent builder for linear expressions.
Provides the shared state required while constructing a solver-independent mathematical model.
MathematicalModel Build(bool clone=true)
Builds and validates the mathematical model.
MathematicalModel Model
Gets the mathematical model currently being built.
override ValueTask BuildConstraintsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
override bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this constraint family is enabled.
Provides common validation and helper methods for one constraint family of the standard lot-sizing fo...
static void AddConstraint(MathematicalModelBuildContext context, string name, LinearExpression expression, MathematicalConstraintSense sense, double rightHandSide, string domainKey="", string description="")
Adds one linear constraint to the mathematical model.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
Represents a parsed mathematical-model business-domain key.
static bool TryParse(string domainKey, out MathematicalDomainKey? result)
Attempts to parse a canonical mathematical domain key.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...
List< LinearTerm > Terms
Gets the linear terms of the expression.
Represents one coefficient-variable term in a linear expression.
Definition LinearTerm.cs:13
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
Definition LinearTerm.cs:49
double Coefficient
Gets or sets the coefficient multiplying the referenced variable.
Definition LinearTerm.cs:60
MathematicalObjective Objective
Gets or sets the mathematical objective.
MathematicalVariable? FindVariableById(int variableId)
Finds a mathematical variable by identifier.
Represents the objective function of a mathematical optimization model.
ObjectiveSense Sense
Gets or sets the optimization direction.
LinearExpression Expression
Gets or sets the linear objective expression.
Represents one decision variable in a mathematical optimization model.
string DomainKey
Gets or sets an optional domain key used to map the mathematical variable back to a lot-sizing decisi...
ObjectiveSense
Identifies the optimization direction of a mathematical objective.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.