MathProgExtensions Class

Necessary extensions methods for enabling mathematical programming.

Definition

Namespace: Orx.MathProg
Assembly: Orx.MathProg (in Orx.MathProg.dll) Version: 1.0.0
C#
public static class MathProgExtensions
Inheritance
Object    MathProgExtensions

Properties

maximize Creates the 'maximization' direction for defining an objective function.

It is followed by a pipe operator and an expression to create the objective function.

C#
Objective maxRevenue
    key("total revenue")
    | "Total revenue obtained by completed tasks by assigning a resource"
    | maximize
    | sum(over(r, t), revenue[t] * x[r, t]);
minimize Creates the 'minimization' direction for defining an objective function.

It is followed by a pipe operator and an expression to create the objective function.

C#
Objective minCost
    key("total cost")
    | "Sum of total flow and design cost of all edges."
    | minimize
    | sum(over(j, i), costFlow[i, j] * x[i, j] + costDesign[i, j] * y[i, j]);
minimize0 Creates a dummy objective function, 'minimize 0' so to say.

This is a shorthand for

C#
Objective obj = minimize | 0
to be used in mathematical programs without an objective function; where only the feasibility of the problem is of interest.

Methods

forall(Set) Creates a forall sets expression for a constraint.
C#
Constraint conDesign = forall(i) | sum(over(j), x[i, j]) <= M * y[i];
forall(Set, Set) Creates a forall sets expression for a constraint.
C#
Constraint conDesign = forall(i, j) | x[i, j] <= M * y[i, j];
forall(Set, Set, Set) Creates a forall sets expression for a constraint.
C#
Constraint conDesign = forall(i, j, k) | x[i, j, k] <= M * y[i, j];
forall(Set, Set, Set, Set) Creates a forall sets expression for a constraint.
C#
Constraint conDesign = forall(i, j, k, l) | x[i, j, k] <= M * y[i, j] + b[l];
HasElements(SetBuilderDependsDimension1, FuncInt32, IEnumerableInt32, String) Finalizes the set builder and returns the resulting set.

The set generates elements in [0, getIndicesByDependsSet(i)); where i is the value of the dependent set.

Consider the following example.

C#
int numNodes = 4;
List<(int, int)> edges = new()
{
    (0, 1),
    (0, 2),
    (1, 2),
    (1, 3),
    (2, 3),
};
IEnumerable<int> getHeads(int i)
    => edges.Where(e => e.Item1 == i).Select(x => x.Item2);

Set i = Set("i").Represents("nodes").HasElementsUntil(numNodes);
Set j = Set("j").Represents("nodes having an arc from node i").DependsOn(i).HasElements(getHeads);

Here, set i has indices { 0, 1, 2, 3 }.

Set j, on the other hand, depends on set i. In other words, it will generate different elements for different values of i:

  • when i takes value 0; j has elements { 1, 2 },
  • when i takes value 1; j has elements { 2, 3 },
  • when i takes value 2; j has elements { 3 }, and
  • when i takes value 3; j is empty set.
HasElements(SetBuilderDependsDimension2, FuncInt32, Int32, IEnumerableInt32, String) Finalizes the set builder and returns the resulting set.

The set generates elements in [0, getIndicesByDependsSets(i, j)); where i and j are the values of the dependent sets.

Consider the following example.

C#
int numNodes = 4;
List<(int, int)> edges = new()
{
    (0, 1),
    (0, 2),
    (1, 2),
    (1, 3),
    (2, 3),
};
IEnumerable<int> getHeads(int i)
    => edges.Where(e => e.Item1 == i).Select(x => x.Item2);

Set i = Set("i").Represents("nodes").HasElementsUntil(numNodes);
Set j = Set("j").Represents("nodes having an arc from node i").DependsOn(i).HasElements(getHeads);

Here, set i has indices { 0, 1, 2, 3 }.

Set j, on the other hand, depends on set i. In other words, it will generate different elements for different values of i:

  • when i takes value 0; j has elements { 1, 2 },
  • when i takes value 1; j has elements { 2, 3 },
  • when i takes value 2; j has elements { 3 }, and
  • when i takes value 3; j is empty set.
HasElementsUntil Finalizes the set builder and returns the resulting set.

The set generates elements in [0, getUntil(i)); where i is the value of the dependent set.

For instance, consider the following sets.

C#
Set t2 = Set("t2").Represents("time periods").HasElementsUntil(4);
Set t1 = Set("t1").Represents("time periods until t2").DependsOn(t2).HasElementsUntil(t1 => t1 + 1);

Set t2 has elements { 0, 1, 2, 3 }.

Set t1 depends on t2; hence, generates different indices for different values of t2:

  • when t1 = 0; t2 has elements { 0 },
  • when t1 = 1; t2 has elements { 0, 1 },
  • when t1 = 2; t2 has elements { 0, 1, 2 },
  • when t1 = 3; t2 has elements { 0, 1, 2, 3 }.
HasSameElementsExceptParent Finalizes the set builder and returns the resulting set.

The set generates exactly the elements of the dependent set except for its value.

For instance, consider the following sets.

C#
Set i = Set("i").Represents("node").HasElementsUntil(3);
Set j = Set("j").Represents("node except for i").DependsOn(i).HasSameElementsExceptParent();

Set i has elements { 0, 1, 2 }.

Set j depends on set i; hence, has different elements for different values of i:

  • when i=0; j has elements { 1, 2 },
  • when i=1; j has elements { 0, 2 },
  • when i=2; j has elements { 0, 1 }.
HasValue(ParameterBuilderIndicesDimension0, Double, String) Finalizes the parameter builder and returns the resulting 0-dimensional parameter ParD0.

The resulting parameter has the given value.

HasValue(ParameterBuilderIndicesDimension0, FuncDouble, String) Finalizes the parameter builder and returns the resulting 0-dimensional parameter ParD0.

Value of the resulting parameter will be obtained by the given function.

HasValues(ParameterBuilderIndicesDimension1, FunVec1Double, String) Finalizes the parameter builder and returns the resulting 1-dimensional parameter ParD1.

The resulting parameter has values defined by the given 1-dimensional functional vector.

HasValues(ParameterBuilderIndicesDimension2, FunVec2Double, String) Finalizes the parameter builder and returns the resulting 2-dimensional parameter ParD2.

The resulting parameter has values defined by the given 2-dimensional functional vector.

HasValues(ParameterBuilderIndicesDimension3, FunVec3Double, String) Finalizes the parameter builder and returns the resulting 3-dimensional parameter ParD3.

The resulting parameter has values defined by the given 3-dimensional functional vector.

HasValues(ParameterBuilderIndicesDimension4, FunVec4Double, String) Finalizes the parameter builder and returns the resulting 4-dimensional parameter ParD4.

The resulting parameter has values defined by the given 4-dimensional functional vector.

IsBetweenZeroAndOne(VariableBuilderTypeDimension0) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable is unboundd; i.e., in [0, 1].

IsBetweenZeroAndOne(VariableBuilderTypeDimension1) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable is unboundd; i.e., in [0, 1].

IsBetweenZeroAndOne(VariableBuilderTypeDimension2) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable is unboundd; i.e., in [0, 1].

IsBetweenZeroAndOne(VariableBuilderTypeDimension3) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable is unboundd; i.e., in [0, 1].

IsBetweenZeroAndOne(VariableBuilderTypeDimension4) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable is unboundd; i.e., in [0, 1].

IsBinary(VariableBuilderIndicesDimension0) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable is binary (0/1) which can take either value 0 or 1.

IsBinary(VariableBuilderIndicesDimension1) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable is binary (0/1) which can take either value 0 or 1.

IsBinary(VariableBuilderIndicesDimension2) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable is binary (0/1) which can take either value 0 or 1.

IsBinary(VariableBuilderIndicesDimension3) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable is binary (0/1) which can take either value 0 or 1.

IsBinary(VariableBuilderIndicesDimension4) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable is binary (0/1) which can take either value 0 or 1.

IsNonnegative(VariableBuilderTypeDimension0) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable is nonnegative; i.e., in [0, infinity).

IsNonnegative(VariableBuilderTypeDimension1) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable is nonnegative; i.e., in [0, infinity).

IsNonnegative(VariableBuilderTypeDimension2) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable is nonnegative; i.e., in [0, infinity).

IsNonnegative(VariableBuilderTypeDimension3) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable is nonnegative; i.e., in [0, infinity).

IsNonnegative(VariableBuilderTypeDimension4) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable is nonnegative; i.e., in [0, infinity).

IsNonpositive(VariableBuilderTypeDimension0) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable is nonpositive; i.e., in (-infinity, 0].

IsNonpositive(VariableBuilderTypeDimension1) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable is nonpositive; i.e., in (-infinity, 0].

IsNonpositive(VariableBuilderTypeDimension2) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable is nonpositive; i.e., in (-infinity, 0].

IsNonpositive(VariableBuilderTypeDimension3) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable is nonpositive; i.e., in (-infinity, 0].

IsNonpositive(VariableBuilderTypeDimension4) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable is nonpositive; i.e., in (-infinity, 0].

IsUnbounded(VariableBuilderTypeDimension0) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable is unboundd; i.e., in (-infinity, infinity).

IsUnbounded(VariableBuilderTypeDimension1) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable is unboundd; i.e., in (-infinity, infinity).

IsUnbounded(VariableBuilderTypeDimension2) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable is unboundd; i.e., in (-infinity, infinity).

IsUnbounded(VariableBuilderTypeDimension3) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable is unboundd; i.e., in (-infinity, infinity).

IsUnbounded(VariableBuilderTypeDimension4) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable is unboundd; i.e., in (-infinity, infinity).

key Creates a symbol key for an objective function or a constraint.

See below three ways to create a flow balance constraint with different levels of details.

C#
Set j = Set("j").Represents("Nodes of the network.").HasElementsUntil(data.NumNodes);
Set i = Set("i").Represents("Tails of edges incoming to j").DependsOn(j).HasElements(data.TailsOf);
Set k = Set("k").Represents("Heads of edges outgoing from j").DependsOn(j).HasElements(data.HeadsOf);
ParD1 demand = Parameter("dem").Represents("Node demand; i.e., amount of flow that needs to be transported to each node.")
    .HasIndices(i).HasValues(data.NodeDemand);

// no details
Constraint flowBalance =
    forall(j)
    | sum(over(i), x[i, j]) - sum(over(k), x[j, k]) == demand[j];

// only with constraint key; this enables matching the constraint in the lp files;
// also makes automatically generated LaTeX, html or text documentation easier to read.
Constraint flowBalance =
    key("flowbal")
    | forall(j)
    | sum(over(i), x[i, j]) - sum(over(k), x[j, k]) == demand[j];

// or with definition to be included in the documentations
Constraint flowBalance =
    key("flowbal")
    | "Flow balance constraints for every node j."
    | forall(j)
    | sum(over(i), x[i, j]) - sum(over(k), x[j, k]) == demand[j];

Similarly, keys and definitions can be included in or omitted from objective function definitions.

C#
// no details
Objective minTotalCost =
    minimize
    | sum(over(j, i), costFlow[i, j] * x[i, j]);

// only key
Objective minTotalCost =
    key("totalcost")
    | minimize
    | sum(over(j, i), costFlow[i, j] * x[i, j]);

// with definition to be included in the documentations
Objective minTotalCost =
    key("totalcost")
    | "Total flow cost of all edges."
    | minimize
    | sum(over(j, i), costFlow[i, j] * x[i, j]);

over(Set) Creates a sum-over expression for a summation.
C#
Constraint flowBalance =
    forall(j)
    | sum(over(i), x[i, j]) - sum(over(k), x[j, k]) == demand[j];
over(Set, Set) Creates a sum-over expression for a summation.
C#
Objective minTotalCost =
    minimize
    | sum(over(j, i), costFlow[i, j] * x[i, j] + costDesign[i, j] * y[i, j]);
over(Set, Set, Set) Creates a sum-over expression for a summation.
C#
Objective minTotalCost =
    minimize
    | sum(over(j, i, c), costFlow[i, j] * x[i, j, c]);
over(Set, Set, Set, Set) Creates a sum-over expression for a summation.
C#
Objective minTotalCost =
    minimize
    | sum(over(t, j, i, c), costFlow[i, j] * x[i, j, c] * alpha[t]);
Parameter Initiates a parameter builder for different dimensions (ParD0, ParD1, ParD2, etc.).

Builder pattern is used for creating all mathematical symbols, to make creating rather complicated variants more convenient.

C#
ParD0 bigM = Parameter("M").Represents("sufficiently large number").HasValue(Math.Max(demands.Max(), capacities.Max()));
ParD0 lazyBigM = Parameter("M").Represents("sufficiently large number").HasValue(() => Math.Max(demands.Max(), capacities.Max()));

ParD1 demand = Parameter("dem").Represents("Node demand; i.e., amount of flow that needs to be transported to each node.")
    .HasIndices(i).HasValues(data.NodeDemand);

ParD2 costFlow = Parameter("c").Represents("Unit flow cost on edge (i, j).").HasIndices(i, j).HasValues(data.EdgeFlowCost);

// or definitions can be omitted; however, they might be useful in automatically generated LaTex, html or text documentations.
ParD0 bigM = Parameter("M").HasValue(Math.Max(demands.Max(), capacities.Max()));
ParD0 lazyBigM = Parameter("M").HasValue(() => Math.Max(demands.Max(), capacities.Max()));
ParD1 demand = Parameter("dem").HasIndices(i).HasValues(data.NodeDemand);
ParD2 costFlow = Parameter("c").HasIndices(i, j).HasValues(data.EdgeFlowCost);
Set Initiates a Set builder.

Builder pattern is used for creating all mathematical symbols, to make creating rather complicated variants more convenient.

Some example set constructions are given below:

C#
Set j = Set("j").Represents("Nodes of the network.").HasElementsUntil(data.NumNodes);
Set i = Set("i").Represents("Tails of edges incoming to j").DependsOn(j).HasElements(data.TailsOf);
Set k = Set("k").Represents("Heads of edges outgoing from j").DependsOn(j).HasElements(data.HeadsOf);

// or definitions can be omitted; however, they might be useful in automatically generated LaTex, html or text documentations.
Set j = Set("j").HasElementsUntil(data.NumNodes);
Set i = Set("i").DependsOn(j).HasElements(data.TailsOf);
Set k = Set("k").DependsOn(j).HasElements(data.HeadsOf);
sum Creates a summation with the given sum over sets expression and linear expression.
Variable Initiates a variable builder for different dimensions (VarD0, VarD1, VarD2, etc.).

Builder pattern is used for creating all mathematical symbols, to make creating rather complicated variants more convenient.

C#
VarD0 maxTardiness = Variable("maxTard").Represents("Maximum tardiness of all tasks").IsContinuous().IsNonnegative();

VarD1 y = Variable("y").Represents("Whether or not location p is included in the design").HasIndices(p).IsBinary();

VarD2 x = Variable("x").Represents("Amount of flow on each edge (i, j)").HasIndices(i, j).IsContinuous().WithBounds(0.0, getEdgeCapacity);

// or definitions can be omitted; however, they might be useful in automatically generated LaTex, html or text documentations.
VarD0 maxTardiness = Variable("maxTard").IsContinuous().IsNonnegative();
VarD1 y = Variable("y").HasIndices(p).IsBinary();
VarD2 x = Variable("x").HasIndices(i, j).IsContinuous().WithBounds(0.0, getEdgeCapacity);
WithBounds(VariableBuilderTypeDimension0, Double, Double) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension0, Double, FuncDouble) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension0, FuncDouble, Double) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension0, FuncDouble, FuncDouble) Finalizes the variable builder and returns the resulting 0-dimensional variable VarD0.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension1, FunVec1Double, FunVec1Double) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension1, FunVec1Double, Double) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension1, Double, FunVec1Double) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension1, Double, Double) Finalizes the variable builder and returns the resulting 1-dimensional variable VarD1.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension2, FunVec2Double, FunVec2Double) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension2, FunVec2Double, Double) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension2, Double, FunVec2Double) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension2, Double, Double) Finalizes the variable builder and returns the resulting 2-dimensional variable VarD2.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension3, FunVec3Double, FunVec3Double) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension3, FunVec3Double, Double) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension3, Double, FunVec3Double) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension3, Double, Double) Finalizes the variable builder and returns the resulting 3-dimensional variable VarD3.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension4, FunVec4Double, FunVec4Double) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension4, FunVec4Double, Double) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension4, Double, FunVec4Double) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable has the given lower and upper bounds.

WithBounds(VariableBuilderTypeDimension4, Double, Double) Finalizes the variable builder and returns the resulting 4-dimensional variable VarD4.

The resulting variable has the given lower and upper bounds.

See Also