Quick start
The primary purpose of ConstraintTrees.jl is to make the representation of constraint systems neat, and thus make their manipulation easy and high-level. In short, the package abstracts the users from keeping track of variable and constraint indexes in matrix form, and gives a nice data structure that describes the system, while keeping all variable allocation and constraint organization completely implicit.
Here we demonstrate the absolutely basic concepts on the "field allocation" problem.
The problem: Field area allocation
Suppose we have 100 square kilometers of field, 500 kilos of fertilizer and 300 kilos of insecticide. We also have a practically infinite supply of wheat and barley seeds. If we decide to sow barley, we can make 550🪙 per square kilometer of harvest; if we decide to go with wheat instead, we can make 350🪙. Unfortunately each square kilometer of wheat requires 6 kilos of fertilizer, and 1 kilo of insecticide, whereas each square kilometer of barley requires 2 kilos of fertilizer and 4 kilos of insecticide, because insects love barley.
How much of our fields should we allocate to wheat and barley to maximize our profit?
Field area allocation with ConstraintTrees.jl
Let's import the package and start constructing the problem:
import ConstraintTrees as C
Let's name our system s
. We first need a few variables:
s = C.variables(keys = [:wheat, :barley])
ConstraintTrees.Tree{ConstraintTrees.Constraint} with 2 elements:
+Quick start · ConstraintTrees.jl Quick start
The primary purpose of ConstraintTrees.jl is to make the representation of constraint systems neat, and thus make their manipulation easy and high-level. In short, the package abstracts the users from keeping track of variable and constraint indexes in matrix form, and gives a nice data structure that describes the system, while keeping all variable allocation and constraint organization completely implicit.
Here we demonstrate the absolutely basic concepts on the "field allocation" problem.
The problem: Field area allocation
Suppose we have 100 square kilometers of field, 500 kilos of fertilizer and 300 kilos of insecticide. We also have a practically infinite supply of wheat and barley seeds. If we decide to sow barley, we can make 550🪙 per square kilometer of harvest; if we decide to go with wheat instead, we can make 350🪙. Unfortunately each square kilometer of wheat requires 6 kilos of fertilizer, and 1 kilo of insecticide, whereas each square kilometer of barley requires 2 kilos of fertilizer and 4 kilos of insecticide, because insects love barley.
How much of our fields should we allocate to wheat and barley to maximize our profit?
Field area allocation with ConstraintTrees.jl
Let's import the package and start constructing the problem:
import ConstraintTrees as C
Let's name our system s
. We first need a few variables:
s = C.variables(keys = [:wheat, :barley])
ConstraintTrees.Tree{ConstraintTrees.Constraint} with 2 elements:
:barley => ConstraintTrees.Constraint(ConstraintTrees.LinearValue(#= ... =#))
:wheat => ConstraintTrees.Constraint(ConstraintTrees.LinearValue(#= ... =#))
With ConstraintTrees.jl, we can (and want to!) label everything very nicely – the constraint trees are essentially directory structures, so one can prefix everything with symbols to put it into nice directories, e.g. as such:
:area^s
ConstraintTrees.Tree{ConstraintTrees.Constraint} with 1 element:
:area => ConstraintTrees.Tree{ConstraintTrees.Constraint}(#= 2 elements =#)
To be absolutely realistic, we also want to make sure that all areas are non-negative. To demonstrate how to do that nicely from the start, we rather re-do the constraints with an appropriate interval bound:
s = :area^C.variables(keys = [:wheat, :barley], bounds = C.Between(0, Inf))
ConstraintTrees.Tree{ConstraintTrees.Constraint} with 1 element:
@@ -123,4 +123,4 @@
:bread => 8.86076
:weizen => 2.05696
How much extra resources is consumed by the factory?
optimal_c.factory.resources
ConstraintTrees.Tree{Float64} with 2 elements:
:heat => 50.4747
- :water => 38.2911
And what is the factory profit in the end?
optimal_c.factory.profit
361.5506329113926
This page was generated using Literate.jl.
Settings
This document was generated with Documenter.jl version 1.4.1 on Wednesday 19 June 2024. Using Julia version 1.10.4.
+ :water => 38.2911
And what is the factory profit in the end?
optimal_c.factory.profit
361.5506329113926
This page was generated using Literate.jl.