Constraint
Bases: BaseBlock
An optimization constraint that can be added to a Model.
Implementation Note
Pyoframe simplifies constraints by moving all the constraint's mathematical terms to the left-hand side. This way, the right-hand side is always zero, and constraints only need to manage one expression.
Use <=, >=, or == operators to create constraints
Constraints should be created using the <=, >=, or == operators, not by directly calling the Constraint constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Expression
|
The constraint's left-hand side expression. |
required |
sense
|
ConstraintSense
|
The sense of the constraint. |
required |
Methods:
| Name | Description |
|---|---|
estimated_size |
Returns the estimated size of the constraint. |
filter |
Syntactic sugar on |
relax |
Allows the constraint to be violated at a |
to_str |
Converts the constraint to a human-readable string, or several arranged in a table. |
Attributes:
| Name | Type | Description |
|---|---|---|
attr |
Container
|
Allows reading and writing constraint attributes similarly to Model.attr. |
dual |
DataFrame | float
|
Returns the constraint's dual values. |
is_quadratic |
bool
|
Returns |
lhs |
Expression
|
|
sense |
|
Source code in pyoframe/_core.py
attr: Container
Allows reading and writing constraint attributes similarly to Model.attr.
dual: pl.DataFrame | float
Returns the constraint's dual values.
Examples:
Notice that for every unit increase in the right-hand side, the objective only improves by 0.5.
For every unit increase in the right-hand side of constraint_x, the objective improves by 0.5.
For every unit increase in the right-hand side of constraint_y, the objective worsens by 0.5.
is_quadratic: bool
Returns True if the constraint is quadratic, False otherwise.
lhs: Expression = lhs
sense = sense
estimated_size(*args, **kwargs)
Returns the estimated size of the constraint.
Includes the size of the underlying expression (Constraint.lhs).
See Expression.estimated_size for details on signature and behavior.
Examples:
An dimensionless constraint has contains a 32 bit constraint id and, for each term, a 64 bit coefficient with a 32 bit variable id. For a two-term expression that is: (32 + 2 * (64 + 32)) = 224 bits = 28 bytes.
Source code in pyoframe/_core.py
filter(*args, **kwargs) -> pl.DataFrame
relax(cost: Operable, max: Operable | None = None) -> Constraint
Allows the constraint to be violated at a cost and, optionally, up to a maximum.
Warning
.relax() must be called before the constraint is assigned to the Model (see examples below).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost
|
Operable
|
The cost of violating the constraint. Costs should be positive because Pyoframe will automatically make them negative for maximization problems. |
required |
max
|
Operable | None
|
The maximum value of the relaxation variable. |
None
|
Returns:
| Type | Description |
|---|---|
Constraint
|
The same constraint |
Examples:
>>> m = pf.Model()
>>> m.hours_sleep = pf.Variable(lb=0)
>>> m.hours_day = pf.Variable(lb=0)
>>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
>>> m.maximize = m.hours_day
>>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
>>> m.optimize()
>>> m.hours_day.solution
16.0
>>> m.maximize += 2 * m.hours_day
>>> m.optimize()
>>> m.hours_day.solution
19.0
relax can only be called after the sense of the model has been defined.
>>> m = pf.Model()
>>> m.hours_sleep = pf.Variable(lb=0)
>>> m.hours_day = pf.Variable(lb=0)
>>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
>>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
Traceback (most recent call last):
...
ValueError: Cannot relax a constraint before the objective sense has been set. Try setting the objective first or using Model(sense=...).
One way to solve this is by setting the sense directly on the model. See how this works fine:
>>> m = pf.Model(sense="max")
>>> m.hours_sleep = pf.Variable(lb=0)
>>> m.hours_day = pf.Variable(lb=0)
>>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
>>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
And now an example with dimensions:
>>> homework_due_tomorrow = pl.DataFrame(
... {
... "project": ["A", "B", "C"],
... "cost_per_hour_underdelivered": [10, 20, 30],
... "hours_to_finish": [9, 9, 9],
... "max_underdelivered": [1, 9, 9],
... }
... )
>>> m.hours_spent = pf.Variable(homework_due_tomorrow["project"], lb=0)
>>> m.must_finish_project = (
... m.hours_spent
... >= homework_due_tomorrow[["project", "hours_to_finish"]]
... ).relax(
... homework_due_tomorrow[["project", "cost_per_hour_underdelivered"]],
... max=homework_due_tomorrow[["project", "max_underdelivered"]],
... )
>>> m.only_one_day = m.hours_spent.sum("project") <= 24
>>> # Relaxing a constraint after it has already been assigned will give an error
>>> m.only_one_day.relax(1)
Traceback (most recent call last):
...
ValueError: .relax() must be called before the Constraint is added to the model
>>> m.optimize()
>>> m.maximize.value
-50.0
>>> m.hours_spent.solution
shape: (3, 2)
┌─────────┬──────────┐
│ project ┆ solution │
│ --- ┆ --- │
│ str ┆ f64 │
╞═════════╪══════════╡
│ A ┆ 8.0 │
│ B ┆ 7.0 │
│ C ┆ 9.0 │
└─────────┴──────────┘
Source code in pyoframe/_core.py
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
to_str(return_df: bool = False) -> str | pl.DataFrame
Converts the constraint to a human-readable string, or several arranged in a table.
Long expressions are truncated according to Config.print_max_terms and Config.print_polars_config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_df
|
bool
|
If |
False
|
Examples:
>>> import polars as pl
>>> m = pf.Model()
>>> x = pf.Set(x=range(1000))
>>> y = pf.Set(y=range(1000))
>>> m.V = pf.Variable(x, y)
>>> expr = 2 * m.V * m.V
>>> print((expr <= 3).to_str())
┌────────┬────────┬────────────────────────────────┐
│ x ┆ y ┆ constraint │
│ (1000) ┆ (1000) ┆ │
╞════════╪════════╪════════════════════════════════╡
│ 0 ┆ 0 ┆ 2 V[0,0] * V[0,0] <= 3 │
│ 0 ┆ 1 ┆ 2 V[0,1] * V[0,1] <= 3 │
│ 0 ┆ 2 ┆ 2 V[0,2] * V[0,2] <= 3 │
│ 0 ┆ 3 ┆ 2 V[0,3] * V[0,3] <= 3 │
│ 0 ┆ 4 ┆ 2 V[0,4] * V[0,4] <= 3 │
│ … ┆ … ┆ … │
│ 999 ┆ 995 ┆ 2 V[999,995] * V[999,995] <= 3 │
│ 999 ┆ 996 ┆ 2 V[999,996] * V[999,996] <= 3 │
│ 999 ┆ 997 ┆ 2 V[999,997] * V[999,997] <= 3 │
│ 999 ┆ 998 ┆ 2 V[999,998] * V[999,998] <= 3 │
│ 999 ┆ 999 ┆ 2 V[999,999] * V[999,999] <= 3 │
└────────┴────────┴────────────────────────────────┘
>>> expr = expr.sum("x")
>>> print((expr >= 3).to_str())
┌────────┬─────────────────────────────────────────────────────────────────────────────────────────┐
│ y ┆ constraint │
│ (1000) ┆ │
╞════════╪═════════════════════════════════════════════════════════════════════════════════════════╡
│ 0 ┆ 2 V[0,0] * V[0,0] +2 V[1,0] * V[1,0] +2 V[2,0] * V[2,0] +2 V[3,0] * V[3,0] +2 V[4,0] * │
│ ┆ V[4,0] … >= 3 │
│ 1 ┆ 2 V[0,1] * V[0,1] +2 V[1,1] * V[1,1] +2 V[2,1] * V[2,1] +2 V[3,1] * V[3,1] +2 V[4,1] * │
│ ┆ V[4,1] … >= 3 │
│ 2 ┆ 2 V[0,2] * V[0,2] +2 V[1,2] * V[1,2] +2 V[2,2] * V[2,2] +2 V[3,2] * V[3,2] +2 V[4,2] * │
│ ┆ V[4,2] … >= 3 │
│ 3 ┆ 2 V[0,3] * V[0,3] +2 V[1,3] * V[1,3] +2 V[2,3] * V[2,3] +2 V[3,3] * V[3,3] +2 V[4,3] * │
│ ┆ V[4,3] … >= 3 │
│ 4 ┆ 2 V[0,4] * V[0,4] +2 V[1,4] * V[1,4] +2 V[2,4] * V[2,4] +2 V[3,4] * V[3,4] +2 V[4,4] * │
│ ┆ V[4,4] … >= 3 │
│ … ┆ … │
│ 995 ┆ 2 V[0,995] * V[0,995] +2 V[1,995] * V[1,995] +2 V[2,995] * V[2,995] +2 V[3,995] * │
│ ┆ V[3,995] +2 V[4,995] * V[4,995] … >= 3 │
│ 996 ┆ 2 V[0,996] * V[0,996] +2 V[1,996] * V[1,996] +2 V[2,996] * V[2,996] +2 V[3,996] * │
│ ┆ V[3,996] +2 V[4,996] * V[4,996] … >= 3 │
│ 997 ┆ 2 V[0,997] * V[0,997] +2 V[1,997] * V[1,997] +2 V[2,997] * V[2,997] +2 V[3,997] * │
│ ┆ V[3,997] +2 V[4,997] * V[4,997] … >= 3 │
│ 998 ┆ 2 V[0,998] * V[0,998] +2 V[1,998] * V[1,998] +2 V[2,998] * V[2,998] +2 V[3,998] * │
│ ┆ V[3,998] +2 V[4,998] * V[4,998] … >= 3 │
│ 999 ┆ 2 V[0,999] * V[0,999] +2 V[1,999] * V[1,999] +2 V[2,999] * V[2,999] +2 V[3,999] * │
│ ┆ V[3,999] +2 V[4,999] * V[4,999] … >= 3 │
└────────┴─────────────────────────────────────────────────────────────────────────────────────────┘
>>> expr = expr.sum("y")
>>> print((expr == 3).to_str())
2 V[0,0] * V[0,0] +2 V[0,1] * V[0,1] +2 V[0,2] * V[0,2] +2 V[0,3] * V[0,3] +2 V[0,4] * V[0,4] … = 3
Source code in pyoframe/_core.py
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 | |
Comments