LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalObjective.cs
Go to the documentation of this file.
1using System;
2using System.Xml.Serialization;
3
5
6/// <summary>
7/// Represents the objective function of a mathematical
8/// optimization model.
9/// </summary>
10[Serializable]
11[XmlType(TypeName = "mathematicalObjective")]
12public sealed class MathematicalObjective
13{
14 /// <summary>
15 /// Initializes an empty mathematical objective.
16 /// </summary>
18 {
19 Name =
20 "objective";
21
22 Sense =
23 ObjectiveSense.Minimize;
24
26 new LinearExpression();
27 }
28
29 /// <summary>
30 /// Initializes a mathematical objective.
31 /// </summary>
32 /// <param name="name">
33 /// Objective name.
34 /// </param>
35 /// <param name="sense">
36 /// Optimization direction.
37 /// </param>
38 /// <param name="expression">
39 /// Linear objective expression.
40 /// </param>
42 string name,
43 ObjectiveSense sense,
44 LinearExpression expression)
45 : this()
46 {
47 ArgumentNullException.ThrowIfNull(
48 expression);
49
50 Name =
51 name;
52
53 Sense =
54 sense;
55
57 expression;
58
60 }
61
62 /// <summary>
63 /// Gets or sets the objective name.
64 /// </summary>
65 [XmlAttribute("name")]
66 public string Name
67 {
68 get;
69 set;
70 }
71
72 /// <summary>
73 /// Gets or sets the optimization direction.
74 /// </summary>
75 [XmlAttribute("sense")]
77 {
78 get;
79 set;
80 }
81
82 /// <summary>
83 /// Gets or sets the linear objective expression.
84 /// </summary>
85 [XmlElement("expression")]
87 {
88 get;
89 set;
90 }
91
92 /// <summary>
93 /// Gets or sets an optional description of the objective.
94 /// </summary>
95 [XmlElement("description")]
96 public string Description
97 {
98 get;
99 set;
100 } =
101 string.Empty;
102
103 /// <summary>
104 /// Validates the mathematical objective.
105 /// </summary>
106 /// <exception cref="InvalidOperationException">
107 /// Thrown when the objective definition is invalid.
108 /// </exception>
109 public void EnsureValid()
110 {
111 if (string.IsNullOrWhiteSpace(
112 Name))
113 {
114 throw new InvalidOperationException(
115 "A mathematical objective name is required.");
116 }
117
118 if (Sense ==
119 ObjectiveSense.Unknown)
120 {
121 throw new InvalidOperationException(
122 "A mathematical objective sense is required.");
123 }
124
125 ArgumentNullException.ThrowIfNull(
126 Expression);
127
128 Expression.EnsureValid();
129 }
130
131 /// <summary>
132 /// Creates an independent copy of this objective.
133 /// </summary>
134 /// <returns>
135 /// Cloned mathematical objective.
136 /// </returns>
138 {
139 return new MathematicalObjective
140 {
141 Name =
142 Name,
143
144 Sense =
145 Sense,
146
147 Expression =
148 Expression.Clone(),
149
152 };
153 }
154}
Represents a linear expression composed of variable terms and a constant value.
MathematicalObjective()
Initializes an empty mathematical objective.
MathematicalObjective Clone()
Creates an independent copy of this objective.
string Description
Gets or sets an optional description of the objective.
ObjectiveSense Sense
Gets or sets the optimization direction.
void EnsureValid()
Validates the mathematical objective.
MathematicalObjective(string name, ObjectiveSense sense, LinearExpression expression)
Initializes a mathematical objective.
LinearExpression Expression
Gets or sets the linear objective expression.
ObjectiveSense
Identifies the optimization direction of a mathematical objective.