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. |
update |
Updates the existing constraint(s) to match the constraint(s) in |
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
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 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 | |
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
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 | |
update(new_constraint: Constraint) -> None
Updates the existing constraint(s) to match the constraint(s) in new_constraint.
new_constraint must have the same dimensions as the existing constraint. An equality constraint cannot be updated with an inequality constraint and vice versa.
Conflicting labels (dimensioned constraints only)
An error will be raised if new_constraint tries to introduce labels that were not already present in the constraint.
If the constraint has labels that are not present in new_constraint, those labels and their associated constraints will be left untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_constraint
|
Constraint
|
A constraint with the same dimensions and sense as the existing constraint in the model. The coefficients of this constraint will replace the coefficients of the existing constraint. |
required |
Examples:
Dimensionless constraints can be updated with new coefficients:
>>> m = pf.Model()
>>> m.X = pf.Variable(lb=0, ub=10)
>>> m.Y = pf.Variable(lb=0, ub=10)
>>> m.maximize = m.X + m.Y
>>> m.Con = m.X + 2 * m.Y <= 4
>>> m.optimize()
>>> m.X.solution, m.Y.solution
(4.0, 0.0)
>>> m.Con.update(m.X <= 6)
>>> m.optimize()
>>> m.X.solution, m.Y.solution
(6.0, 10.0)
Dimensioned constraints can also be updated:
>>> m = pf.Model()
>>> m.X = pf.Variable({"i": [1, 2, 3]}, lb=0, ub=10)
>>> m.maximize = m.X.sum()
>>> m.Con = m.X <= 4
>>> m.optimize()
>>> m.objective.value
12.0
>>> m.Con.update(m.X.filter(i=1) <= 5)
>>> m.optimize()
>>> m.objective.value
13.0
Source code in pyoframe/_core.py
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 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 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 | |
Comments