pyoframe 0.2.1__py3-none-any.whl → 1.0.0a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyoframe/core.py DELETED
@@ -1,1787 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from abc import ABC, abstractmethod
4
- from typing import (
5
- TYPE_CHECKING,
6
- Any,
7
- Dict,
8
- Iterable,
9
- List,
10
- Mapping,
11
- Optional,
12
- Protocol,
13
- Sequence,
14
- Union,
15
- overload,
16
- )
17
-
18
- import numpy as np
19
- import pandas as pd
20
- import polars as pl
21
- import pyoptinterface as poi
22
-
23
- from pyoframe._arithmetic import (
24
- _add_expressions,
25
- _get_dimensions,
26
- _multiply_expressions,
27
- _simplify_expr_df,
28
- )
29
- from pyoframe.constants import (
30
- COEF_KEY,
31
- CONST_TERM,
32
- CONSTRAINT_KEY,
33
- DUAL_KEY,
34
- KEY_TYPE,
35
- QUAD_VAR_KEY,
36
- RESERVED_COL_KEYS,
37
- SOLUTION_KEY,
38
- VAR_KEY,
39
- Config,
40
- ConstraintSense,
41
- ObjSense,
42
- PyoframeError,
43
- UnmatchedStrategy,
44
- VType,
45
- VTypeValue,
46
- )
47
- from pyoframe.model_element import (
48
- ModelElement,
49
- ModelElementWithId,
50
- SupportPolarsMethodMixin,
51
- )
52
- from pyoframe.util import (
53
- Container,
54
- FuncArgs,
55
- cast_coef_to_string,
56
- concat_dimensions,
57
- dataframe_to_tupled_list,
58
- get_obj_repr,
59
- parse_inputs_as_iterable,
60
- unwrap_single_values,
61
- )
62
-
63
- if TYPE_CHECKING: # pragma: no cover
64
- from pyoframe.model import Model
65
-
66
-
67
- def _forward_to_expression(func_name: str):
68
- def wrapper(self: "SupportsMath", *args, **kwargs) -> "Expression":
69
- expr = self.to_expr()
70
- return getattr(expr, func_name)(*args, **kwargs)
71
-
72
- return wrapper
73
-
74
-
75
- class SupportsToExpr(Protocol):
76
- def to_expr(self) -> "Expression": ...
77
-
78
-
79
- class SupportsMath(ABC, SupportsToExpr):
80
- """Any object that can be converted into an expression."""
81
-
82
- def __init__(self, **kwargs):
83
- self.unmatched_strategy = UnmatchedStrategy.UNSET
84
- self.allowed_new_dims: List[str] = []
85
- super().__init__(**kwargs)
86
-
87
- def keep_unmatched(self):
88
- self.unmatched_strategy = UnmatchedStrategy.KEEP
89
- return self
90
-
91
- def drop_unmatched(self):
92
- self.unmatched_strategy = UnmatchedStrategy.DROP
93
- return self
94
-
95
- def add_dim(self, *dims: str):
96
- self.allowed_new_dims.extend(dims)
97
- return self
98
-
99
- @abstractmethod
100
- def to_expr(self) -> "Expression": ...
101
-
102
- __add__ = _forward_to_expression("__add__")
103
- __mul__ = _forward_to_expression("__mul__")
104
- sum = _forward_to_expression("sum")
105
- map = _forward_to_expression("map")
106
-
107
- def __pow__(self, power: int):
108
- """
109
- Support squaring expressions:
110
- >>> m = pf.Model()
111
- >>> m.v = pf.Variable()
112
- >>> m.v ** 2
113
- <Expression size=1 dimensions={} terms=1 degree=2>
114
- v * v
115
- >>> m.v ** 3
116
- Traceback (most recent call last):
117
- ...
118
- ValueError: Raising an expressions to **3 is not supported. Expressions can only be squared (**2).
119
- """
120
- if power == 2:
121
- return self * self
122
- raise ValueError(
123
- f"Raising an expressions to **{power} is not supported. Expressions can only be squared (**2)."
124
- )
125
-
126
- def __neg__(self):
127
- res = self.to_expr() * -1
128
- # Negating a constant term should keep the unmatched strategy
129
- res.unmatched_strategy = self.unmatched_strategy
130
- return res
131
-
132
- def __sub__(self, other):
133
- """
134
- >>> import polars as pl
135
- >>> m = pf.Model()
136
- >>> df = pl.DataFrame({"dim1": [1,2,3], "value": [1,2,3]})
137
- >>> m.v = pf.Variable(df["dim1"])
138
- >>> m.v - df
139
- <Expression size=3 dimensions={'dim1': 3} terms=6>
140
- [1]: v[1] -1
141
- [2]: v[2] -2
142
- [3]: v[3] -3
143
- """
144
- if not isinstance(other, (int, float)):
145
- other = other.to_expr()
146
- return self.to_expr() + (-other)
147
-
148
- def __rmul__(self, other):
149
- return self.to_expr() * other
150
-
151
- def __radd__(self, other):
152
- return self.to_expr() + other
153
-
154
- def __truediv__(self, other):
155
- """
156
-
157
- Examples:
158
- Support division.
159
- >>> m = pf.Model()
160
- >>> m.v = Variable({"dim1": [1,2,3]})
161
- >>> m.v / 2
162
- <Expression size=3 dimensions={'dim1': 3} terms=3>
163
- [1]: 0.5 v[1]
164
- [2]: 0.5 v[2]
165
- [3]: 0.5 v[3]
166
- """
167
- return self.to_expr() * (1 / other)
168
-
169
- def __rsub__(self, other):
170
- """
171
- Support right subtraction.
172
-
173
- Examples:
174
- >>> m = pf.Model()
175
- >>> m.v = Variable({"dim1": [1,2,3]})
176
- >>> 1 - m.v
177
- <Expression size=3 dimensions={'dim1': 3} terms=6>
178
- [1]: 1 - v[1]
179
- [2]: 1 - v[2]
180
- [3]: 1 - v[3]
181
- """
182
- return other + (-self.to_expr())
183
-
184
- def __le__(self, other):
185
- """Equality constraint.
186
-
187
- Examples:
188
- >>> m = pf.Model()
189
- >>> m.v = pf.Variable()
190
- >>> m.v <= 1
191
- <Constraint sense='<=' size=1 dimensions={} terms=2>
192
- v <= 1
193
- """
194
- return Constraint(self - other, ConstraintSense.LE)
195
-
196
- def __ge__(self, other):
197
- """Equality constraint.
198
-
199
- Examples:
200
- >>> m = pf.Model()
201
- >>> m.v = pf.Variable()
202
- >>> m.v >= 1
203
- <Constraint sense='>=' size=1 dimensions={} terms=2>
204
- v >= 1
205
- """
206
- return Constraint(self - other, ConstraintSense.GE)
207
-
208
- def __eq__(self, value: object): # type: ignore
209
- """Equality constraint.
210
-
211
- Examples:
212
- >>> m = pf.Model()
213
- >>> m.v = pf.Variable()
214
- >>> m.v == 1
215
- <Constraint sense='=' size=1 dimensions={} terms=2>
216
- v = 1
217
- """
218
- return Constraint(self - value, ConstraintSense.EQ)
219
-
220
-
221
- SetTypes = Union[
222
- pl.DataFrame,
223
- pd.Index,
224
- pd.DataFrame,
225
- SupportsMath,
226
- Mapping[str, Sequence[object]],
227
- "Set",
228
- "Constraint",
229
- ]
230
-
231
-
232
- class Set(ModelElement, SupportsMath, SupportPolarsMethodMixin):
233
- """
234
- A set which can then be used to index variables.
235
-
236
- Examples:
237
- >>> pf.Set(x=range(2), y=range(3))
238
- <Set size=6 dimensions={'x': 2, 'y': 3}>
239
- [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
240
- """
241
-
242
- def __init__(self, *data: SetTypes | Iterable[SetTypes], **named_data):
243
- data_list = list(data)
244
- for name, set in named_data.items():
245
- data_list.append({name: set})
246
- df = self._parse_acceptable_sets(*data_list)
247
- if not df.is_empty() and df.is_duplicated().any():
248
- raise ValueError("Duplicate rows found in input data.")
249
- super().__init__(df)
250
-
251
- def _new(self, data: pl.DataFrame):
252
- s = Set(data)
253
- s._model = self._model
254
- # Copy over the unmatched strategy on operations like .rename(), .with_columns(), etc.
255
- s.unmatched_strategy = self.unmatched_strategy
256
- return s
257
-
258
- @staticmethod
259
- def _parse_acceptable_sets(
260
- *over: SetTypes | Iterable[SetTypes],
261
- ) -> pl.DataFrame:
262
- """
263
- Examples:
264
- >>> import pandas as pd
265
- >>> dim1 = pd.Index([1, 2, 3], name="dim1")
266
- >>> dim2 = pd.Index(["a", "b"], name="dim1")
267
- >>> Set._parse_acceptable_sets([dim1, dim2])
268
- Traceback (most recent call last):
269
- ...
270
- AssertionError: All coordinates must have unique column names.
271
- >>> dim2.name = "dim2"
272
- >>> Set._parse_acceptable_sets([dim1, dim2])
273
- shape: (6, 2)
274
- ┌──────┬──────┐
275
- │ dim1 ┆ dim2 │
276
- │ --- ┆ --- │
277
- │ i64 ┆ str │
278
- ╞══════╪══════╡
279
- │ 1 ┆ a │
280
- │ 1 ┆ b │
281
- │ 2 ┆ a │
282
- │ 2 ┆ b │
283
- │ 3 ┆ a │
284
- │ 3 ┆ b │
285
- └──────┴──────┘
286
- """
287
- assert len(over) > 0, "At least one set must be provided."
288
- over_iter: Iterable[SetTypes] = parse_inputs_as_iterable(*over)
289
-
290
- over_frames: List[pl.DataFrame] = [Set._set_to_polars(set) for set in over_iter]
291
-
292
- over_merged = over_frames[0]
293
-
294
- for df in over_frames[1:]:
295
- assert set(over_merged.columns) & set(df.columns) == set(), (
296
- "All coordinates must have unique column names."
297
- )
298
- over_merged = over_merged.join(df, how="cross")
299
- return over_merged
300
-
301
- def to_expr(self) -> Expression:
302
- return Expression(
303
- self.data.with_columns(
304
- pl.lit(1).alias(COEF_KEY), pl.lit(CONST_TERM).alias(VAR_KEY)
305
- )
306
- )
307
-
308
- def __mul__(self, other):
309
- if isinstance(other, Set):
310
- assert set(self.data.columns) & set(other.data.columns) == set(), (
311
- "Cannot multiply two sets with columns in common."
312
- )
313
- return Set(self.data, other.data)
314
- return super().__mul__(other)
315
-
316
- def __add__(self, other):
317
- if isinstance(other, Set):
318
- try:
319
- return self._new(
320
- pl.concat([self.data, other.data]).unique(maintain_order=True)
321
- )
322
- except pl.exceptions.ShapeError as e:
323
- if "unable to vstack, column names don't match" in str(e):
324
- raise PyoframeError(
325
- f"Failed to add sets '{self.friendly_name}' and '{other.friendly_name}' because dimensions do not match ({self.dimensions} != {other.dimensions}) "
326
- ) from e
327
- raise e
328
-
329
- return super().__add__(other)
330
-
331
- def __repr__(self):
332
- return (
333
- get_obj_repr(self, ("name",), size=self.data.height, dimensions=self.shape)
334
- + "\n"
335
- + dataframe_to_tupled_list(
336
- self.data, num_max_elements=Config.print_max_set_elements
337
- )
338
- )
339
-
340
- @staticmethod
341
- def _set_to_polars(set: "SetTypes") -> pl.DataFrame:
342
- if isinstance(set, dict):
343
- df = pl.DataFrame(set)
344
- elif isinstance(set, Constraint):
345
- df = set.data.select(set.dimensions_unsafe)
346
- elif isinstance(set, SupportsMath):
347
- df = (
348
- set.to_expr()
349
- .data.drop(RESERVED_COL_KEYS, strict=False)
350
- .unique(maintain_order=True)
351
- )
352
- elif isinstance(set, pd.Index):
353
- df = pl.from_pandas(pd.DataFrame(index=set).reset_index())
354
- elif isinstance(set, pd.DataFrame):
355
- df = pl.from_pandas(set)
356
- elif isinstance(set, pl.DataFrame):
357
- df = set
358
- elif isinstance(set, pl.Series):
359
- df = set.to_frame()
360
- elif isinstance(set, Set):
361
- df = set.data
362
- elif isinstance(set, range):
363
- raise ValueError(
364
- "Cannot convert a range to a set without a dimension name. Try Set(dim_name=range(...))"
365
- )
366
- else:
367
- raise ValueError(f"Cannot convert type {type(set)} to a polars DataFrame")
368
-
369
- if "index" in df.columns:
370
- raise ValueError(
371
- "Please specify a custom dimension name rather than using 'index' to avoid confusion."
372
- )
373
-
374
- for reserved_key in RESERVED_COL_KEYS:
375
- if reserved_key in df.columns:
376
- raise ValueError(
377
- f"Cannot use reserved column names {reserved_key} as dimensions."
378
- )
379
-
380
- return df
381
-
382
-
383
- class Expression(ModelElement, SupportsMath, SupportPolarsMethodMixin):
384
- """A linear or quadratic expression."""
385
-
386
- def __init__(self, data: pl.DataFrame):
387
- """
388
- A linear expression.
389
-
390
- Examples:
391
- >>> import pandas as pd
392
- >>> df = pd.DataFrame({"item" : [1, 1, 1, 2, 2], "time": ["mon", "tue", "wed", "mon", "tue"], "cost": [1, 2, 3, 4, 5]}).set_index(["item", "time"])
393
- >>> m = pf.Model()
394
- >>> m.Time = pf.Variable(df.index)
395
- >>> m.Size = pf.Variable(df.index)
396
- >>> expr = df["cost"] * m.Time + df["cost"] * m.Size
397
- >>> expr
398
- <Expression size=5 dimensions={'item': 2, 'time': 3} terms=10>
399
- [1,mon]: Time[1,mon] + Size[1,mon]
400
- [1,tue]: 2 Time[1,tue] +2 Size[1,tue]
401
- [1,wed]: 3 Time[1,wed] +3 Size[1,wed]
402
- [2,mon]: 4 Time[2,mon] +4 Size[2,mon]
403
- [2,tue]: 5 Time[2,tue] +5 Size[2,tue]
404
- """
405
- # Sanity checks, VAR_KEY and COEF_KEY must be present
406
- assert VAR_KEY in data.columns, "Missing variable column."
407
- assert COEF_KEY in data.columns, "Missing coefficient column."
408
-
409
- # Sanity check no duplicates indices
410
- if Config.enable_is_duplicated_expression_safety_check:
411
- duplicated_mask = data.drop(COEF_KEY).is_duplicated()
412
- # In theory this should never happen unless there's a bug in the library
413
- if duplicated_mask.any(): # pragma: no cover
414
- duplicated_data = data.filter(duplicated_mask)
415
- raise ValueError(
416
- f"Cannot create an expression with duplicate indices:\n{duplicated_data}."
417
- )
418
-
419
- data = _simplify_expr_df(data)
420
-
421
- super().__init__(data)
422
-
423
- @classmethod
424
- def constant(cls, constant: int | float) -> "Expression":
425
- """
426
- Examples:
427
- >>> pf.Expression.constant(5)
428
- <Expression size=1 dimensions={} terms=1>
429
- 5
430
- """
431
- return cls(
432
- pl.DataFrame(
433
- {
434
- COEF_KEY: [constant],
435
- VAR_KEY: [CONST_TERM],
436
- },
437
- schema={COEF_KEY: pl.Float64, VAR_KEY: KEY_TYPE},
438
- )
439
- )
440
-
441
- def sum(self, over: Union[str, Iterable[str]]):
442
- """
443
- Examples:
444
- >>> import pandas as pd
445
- >>> m = pf.Model()
446
- >>> df = pd.DataFrame({"item" : [1, 1, 1, 2, 2], "time": ["mon", "tue", "wed", "mon", "tue"], "cost": [1, 2, 3, 4, 5]}).set_index(["item", "time"])
447
- >>> m.quantity = Variable(df.reset_index()[["item"]].drop_duplicates())
448
- >>> expr = (m.quantity * df["cost"]).sum("time")
449
- >>> expr.data
450
- shape: (2, 3)
451
- ┌──────┬─────────┬───────────────┐
452
- │ item ┆ __coeff ┆ __variable_id │
453
- │ --- ┆ --- ┆ --- │
454
- │ i64 ┆ f64 ┆ u32 │
455
- ╞══════╪═════════╪═══════════════╡
456
- │ 1 ┆ 6.0 ┆ 1 │
457
- │ 2 ┆ 9.0 ┆ 2 │
458
- └──────┴─────────┴───────────────┘
459
- """
460
- if isinstance(over, str):
461
- over = [over]
462
- dims = self.dimensions
463
- if not dims:
464
- raise ValueError(
465
- f"Cannot sum over dimensions {over} since the current expression has no dimensions."
466
- )
467
- assert set(over) <= set(dims), f"Cannot sum over {over} as it is not in {dims}"
468
- remaining_dims = [dim for dim in dims if dim not in over]
469
-
470
- return self._new(
471
- self.data.drop(over)
472
- .group_by(remaining_dims + self._variable_columns, maintain_order=True)
473
- .sum()
474
- )
475
-
476
- @property
477
- def _variable_columns(self) -> List[str]:
478
- if self.is_quadratic:
479
- return [VAR_KEY, QUAD_VAR_KEY]
480
- else:
481
- return [VAR_KEY]
482
-
483
- def map(self, mapping_set: SetTypes, drop_shared_dims: bool = True) -> Expression:
484
- """
485
- Replaces the dimensions that are shared with mapping_set with the other dimensions found in mapping_set.
486
-
487
- This is particularly useful to go from one type of dimensions to another. For example, to convert data that
488
- is indexed by city to data indexed by country (see example).
489
-
490
- Parameters:
491
- mapping_set:
492
- The set to map the expression to. This can be a DataFrame, Index, or another Set.
493
- drop_shared_dims:
494
- If True, the dimensions shared between the expression and the mapping set are dropped from the resulting expression and
495
- repeated rows are summed.
496
- If False, the shared dimensions are kept in the resulting expression.
497
-
498
- Returns:
499
- A new Expression containing the result of the mapping operation.
500
-
501
- Examples:
502
- >>> import polars as pl
503
- >>> pop_data = pl.DataFrame({"city": ["Toronto", "Vancouver", "Boston"], "year": [2024, 2024, 2024], "population": [10, 2, 8]}).to_expr()
504
- >>> cities_and_countries = pl.DataFrame({"city": ["Toronto", "Vancouver", "Boston"], "country": ["Canada", "Canada", "USA"]})
505
- >>> pop_data.map(cities_and_countries)
506
- <Expression size=2 dimensions={'year': 1, 'country': 2} terms=2>
507
- [2024,Canada]: 12
508
- [2024,USA]: 8
509
- >>> pop_data.map(cities_and_countries, drop_shared_dims=False)
510
- <Expression size=3 dimensions={'city': 3, 'year': 1, 'country': 2} terms=3>
511
- [Toronto,2024,Canada]: 10
512
- [Vancouver,2024,Canada]: 2
513
- [Boston,2024,USA]: 8
514
- """
515
- mapping_set = Set(mapping_set)
516
-
517
- dims = self.dimensions
518
- if dims is None:
519
- raise ValueError("Cannot use .map() on an expression with no dimensions.")
520
-
521
- mapping_dims = mapping_set.dimensions
522
- if mapping_dims is None:
523
- raise ValueError(
524
- "Cannot use .map() with a mapping set containing no dimensions."
525
- )
526
-
527
- shared_dims = [dim for dim in dims if dim in mapping_dims]
528
- if not shared_dims:
529
- raise ValueError(
530
- f"Cannot apply .map() as there are no shared dimensions between the expression (dims={self.dimensions}) and the mapping set (dims={mapping_set.dimensions})."
531
- )
532
-
533
- mapped_expression = self * mapping_set
534
-
535
- if drop_shared_dims:
536
- return sum(shared_dims, mapped_expression)
537
-
538
- return mapped_expression
539
-
540
- def rolling_sum(self, over: str, window_size: int) -> Expression:
541
- """
542
- Calculates the rolling sum of the Expression over a specified window size for a given dimension.
543
-
544
- This method applies a rolling sum operation over the dimension specified by `over`,
545
- using a window defined by `window_size`.
546
-
547
-
548
- Parameters:
549
- over :
550
- The name of the dimension (column) over which the rolling sum is calculated.
551
- This dimension must exist within the Expression's dimensions.
552
- window_size :
553
- The size of the moving window in terms of number of records.
554
- The rolling sum is calculated over this many consecutive elements.
555
-
556
- Returns:
557
- A new Expression instance containing the result of the rolling sum operation.
558
- This new Expression retains all dimensions (columns) of the original data,
559
- with the rolling sum applied over the specified dimension.
560
-
561
- Examples:
562
- >>> import polars as pl
563
- >>> cost = pl.DataFrame({"item" : [1, 1, 1, 2, 2], "time": [1, 2, 3, 1, 2], "cost": [1, 2, 3, 4, 5]})
564
- >>> m = pf.Model()
565
- >>> m.quantity = pf.Variable(cost[["item", "time"]])
566
- >>> (m.quantity * cost).rolling_sum(over="time", window_size=2)
567
- <Expression size=5 dimensions={'item': 2, 'time': 3} terms=8>
568
- [1,1]: quantity[1,1]
569
- [1,2]: quantity[1,1] +2 quantity[1,2]
570
- [1,3]: 2 quantity[1,2] +3 quantity[1,3]
571
- [2,1]: 4 quantity[2,1]
572
- [2,2]: 4 quantity[2,1] +5 quantity[2,2]
573
- """
574
- dims = self.dimensions
575
- if dims is None:
576
- raise ValueError(
577
- "Cannot use rolling_sum() with an expression with no dimensions."
578
- )
579
- assert over in dims, f"Cannot sum over {over} as it is not in {dims}"
580
- remaining_dims = [dim for dim in dims if dim not in over]
581
-
582
- return self._new(
583
- pl.concat(
584
- [
585
- df.with_columns(pl.col(over).max())
586
- for _, df in self.data.rolling(
587
- index_column=over,
588
- period=f"{window_size}i",
589
- group_by=remaining_dims,
590
- )
591
- ]
592
- )
593
- )
594
-
595
- def within(self, set: "SetTypes") -> Expression:
596
- """
597
- Examples:
598
- >>> import pandas as pd
599
- >>> general_expr = pd.DataFrame({"dim1": [1, 2, 3], "value": [1, 2, 3]}).to_expr()
600
- >>> filter_expr = pd.DataFrame({"dim1": [1, 3], "value": [5, 6]}).to_expr()
601
- >>> general_expr.within(filter_expr).data
602
- shape: (2, 3)
603
- ┌──────┬─────────┬───────────────┐
604
- │ dim1 ┆ __coeff ┆ __variable_id │
605
- │ --- ┆ --- ┆ --- │
606
- │ i64 ┆ f64 ┆ u32 │
607
- ╞══════╪═════════╪═══════════════╡
608
- │ 1 ┆ 1.0 ┆ 0 │
609
- │ 3 ┆ 3.0 ┆ 0 │
610
- └──────┴─────────┴───────────────┘
611
- """
612
- df: pl.DataFrame = Set(set).data
613
- set_dims = _get_dimensions(df)
614
- assert set_dims is not None, (
615
- "Cannot use .within() with a set with no dimensions."
616
- )
617
- dims = self.dimensions
618
- assert dims is not None, (
619
- "Cannot use .within() with an expression with no dimensions."
620
- )
621
- dims_in_common = [dim for dim in dims if dim in set_dims]
622
- by_dims = df.select(dims_in_common).unique(maintain_order=True)
623
- return self._new(self.data.join(by_dims, on=dims_in_common))
624
-
625
- @property
626
- def is_quadratic(self) -> bool:
627
- """
628
- Returns True if the expression is quadratic, False otherwise.
629
-
630
- Computes in O(1) since expressions are quadratic if and
631
- only if self.data contain the QUAD_VAR_KEY column.
632
-
633
- Examples:
634
- >>> import pandas as pd
635
- >>> m = pf.Model()
636
- >>> m.v = Variable()
637
- >>> expr = pd.DataFrame({"dim1": [1, 2, 3], "value": [1, 2, 3]}) * m.v
638
- >>> expr *= m.v
639
- >>> expr.is_quadratic
640
- True
641
- """
642
- return QUAD_VAR_KEY in self.data.columns
643
-
644
- def degree(self) -> int:
645
- """
646
- Returns the degree of the expression (0=constant, 1=linear, 2=quadratic).
647
-
648
- Examples:
649
- >>> import pandas as pd
650
- >>> m = pf.Model()
651
- >>> m.v1 = pf.Variable()
652
- >>> m.v2 = pf.Variable()
653
- >>> expr = pd.DataFrame({"dim1": [1, 2, 3], "value": [1, 2, 3]}).to_expr()
654
- >>> expr.degree()
655
- 0
656
- >>> expr *= m.v1
657
- >>> expr.degree()
658
- 1
659
- >>> expr += (m.v2 ** 2).add_dim("dim1")
660
- >>> expr.degree()
661
- 2
662
- """
663
- if self.is_quadratic:
664
- return 2
665
- elif (self.data.get_column(VAR_KEY) != CONST_TERM).any():
666
- return 1
667
- else:
668
- return 0
669
-
670
- def __add__(self, other):
671
- """
672
- Examples:
673
- >>> import pandas as pd
674
- >>> m = pf.Model()
675
- >>> add = pd.DataFrame({"dim1": [1,2,3], "add": [10, 20, 30]}).to_expr()
676
- >>> m.v = Variable(add)
677
- >>> m.v + add
678
- <Expression size=3 dimensions={'dim1': 3} terms=6>
679
- [1]: v[1] +10
680
- [2]: v[2] +20
681
- [3]: v[3] +30
682
- >>> m.v + add + 2
683
- <Expression size=3 dimensions={'dim1': 3} terms=6>
684
- [1]: v[1] +12
685
- [2]: v[2] +22
686
- [3]: v[3] +32
687
- >>> m.v + pd.DataFrame({"dim1": [1,2], "add": [10, 20]})
688
- Traceback (most recent call last):
689
- ...
690
- pyoframe.constants.PyoframeError: Failed to add expressions:
691
- <Expression size=3 dimensions={'dim1': 3} terms=3> + <Expression size=2 dimensions={'dim1': 2} terms=2>
692
- Due to error:
693
- Dataframe has unmatched values. If this is intentional, use .drop_unmatched() or .keep_unmatched()
694
- shape: (1, 2)
695
- ┌──────┬────────────┐
696
- │ dim1 ┆ dim1_right │
697
- │ --- ┆ --- │
698
- │ i64 ┆ i64 │
699
- ╞══════╪════════════╡
700
- │ 3 ┆ null │
701
- └──────┴────────────┘
702
- >>> m.v2 = Variable()
703
- >>> 5 + 2 * m.v2
704
- <Expression size=1 dimensions={} terms=2>
705
- 2 v2 +5
706
- """
707
- if isinstance(other, str):
708
- raise ValueError(
709
- "Cannot add a string to an expression. Perhaps you meant to use pf.sum() instead of sum()?"
710
- )
711
- if isinstance(other, (int, float)):
712
- return self._add_const(other)
713
- other = other.to_expr()
714
- self._learn_from_other(other)
715
- return _add_expressions(self, other)
716
-
717
- def __mul__(
718
- self: "Expression", other: int | float | SupportsToExpr
719
- ) -> "Expression":
720
- if isinstance(other, (int, float)):
721
- return self.with_columns(pl.col(COEF_KEY) * other)
722
-
723
- other = other.to_expr()
724
- self._learn_from_other(other)
725
- return _multiply_expressions(self, other)
726
-
727
- def to_expr(self) -> Expression:
728
- return self
729
-
730
- def _learn_from_other(self, other: Expression):
731
- if self._model is None and other._model is not None:
732
- self._model = other._model
733
-
734
- def _new(self, data: pl.DataFrame) -> Expression:
735
- e = Expression(data)
736
- e._model = self._model
737
- # Note: We intentionally don't propogate the unmatched strategy to the new expression
738
- e.allowed_new_dims = self.allowed_new_dims
739
- return e
740
-
741
- def _add_const(self, const: int | float) -> Expression:
742
- """
743
- Examples:
744
- >>> m = pf.Model()
745
- >>> m.x1 = Variable()
746
- >>> m.x2 = Variable()
747
- >>> m.x1 + 5
748
- <Expression size=1 dimensions={} terms=2>
749
- x1 +5
750
- >>> m.x1 ** 2 + 5
751
- <Expression size=1 dimensions={} terms=2 degree=2>
752
- x1 * x1 +5
753
- >>> m.x1 ** 2 + m.x2 + 5
754
- <Expression size=1 dimensions={} terms=3 degree=2>
755
- x1 * x1 + x2 +5
756
-
757
- It also works with dimensions
758
-
759
- >>> m = pf.Model()
760
- >>> m.v = Variable({"dim1": [1, 2, 3]})
761
- >>> m.v * m.v + 5
762
- <Expression size=3 dimensions={'dim1': 3} terms=6 degree=2>
763
- [1]: 5 + v[1] * v[1]
764
- [2]: 5 + v[2] * v[2]
765
- [3]: 5 + v[3] * v[3]
766
- """
767
- dim = self.dimensions
768
- data = self.data
769
- # Fill in missing constant terms
770
- if not dim:
771
- if CONST_TERM not in data[VAR_KEY]:
772
- const_df = pl.DataFrame(
773
- {COEF_KEY: [0.0], VAR_KEY: [CONST_TERM]},
774
- schema={COEF_KEY: pl.Float64, VAR_KEY: KEY_TYPE},
775
- )
776
- if self.is_quadratic:
777
- const_df = const_df.with_columns(
778
- pl.lit(CONST_TERM).alias(QUAD_VAR_KEY).cast(KEY_TYPE)
779
- )
780
- data = pl.concat(
781
- [data, const_df],
782
- how="vertical_relaxed",
783
- )
784
- else:
785
- keys = (
786
- data.select(dim)
787
- .unique(maintain_order=True)
788
- .with_columns(pl.lit(CONST_TERM).alias(VAR_KEY).cast(KEY_TYPE))
789
- )
790
- if self.is_quadratic:
791
- keys = keys.with_columns(
792
- pl.lit(CONST_TERM).alias(QUAD_VAR_KEY).cast(KEY_TYPE)
793
- )
794
- data = data.join(
795
- keys, on=dim + self._variable_columns, how="full", coalesce=True
796
- ).with_columns(pl.col(COEF_KEY).fill_null(0.0))
797
-
798
- data = data.with_columns(
799
- pl.when(pl.col(VAR_KEY) == CONST_TERM)
800
- .then(pl.col(COEF_KEY) + const)
801
- .otherwise(pl.col(COEF_KEY))
802
- )
803
-
804
- return self._new(data)
805
-
806
- @property
807
- def constant_terms(self):
808
- dims = self.dimensions
809
- constant_terms = self.data.filter(pl.col(VAR_KEY) == CONST_TERM).drop(VAR_KEY)
810
- if self.is_quadratic:
811
- constant_terms = constant_terms.drop(QUAD_VAR_KEY)
812
- if dims is not None:
813
- dims_df = self.data.select(dims).unique(maintain_order=True)
814
- df = constant_terms.join(dims_df, on=dims, how="full", coalesce=True)
815
- return df.with_columns(pl.col(COEF_KEY).fill_null(0.0))
816
- else:
817
- if len(constant_terms) == 0:
818
- return pl.DataFrame(
819
- {COEF_KEY: [0.0], VAR_KEY: [CONST_TERM]},
820
- schema={COEF_KEY: pl.Float64, VAR_KEY: KEY_TYPE},
821
- )
822
- return constant_terms
823
-
824
- @property
825
- def variable_terms(self):
826
- return self.data.filter(pl.col(VAR_KEY) != CONST_TERM)
827
-
828
- @unwrap_single_values
829
- def evaluate(self) -> pl.DataFrame:
830
- """
831
- The value of the expression. Only available after the model has been solved.
832
-
833
- Examples:
834
- >>> m = pf.Model()
835
- >>> m.X = pf.Variable({"dim1": [1, 2, 3]}, ub=10)
836
- >>> m.expr_1 = 2 * m.X + 1
837
- >>> m.expr_2 = pf.sum(m.expr_1)
838
- >>> m.maximize = m.expr_2 - 3
839
- >>> m.attr.Silent = True
840
- >>> m.optimize()
841
- >>> m.expr_1.evaluate()
842
- shape: (3, 2)
843
- ┌──────┬──────────┐
844
- │ dim1 ┆ solution │
845
- │ --- ┆ --- │
846
- │ i64 ┆ f64 │
847
- ╞══════╪══════════╡
848
- │ 1 ┆ 21.0 │
849
- │ 2 ┆ 21.0 │
850
- │ 3 ┆ 21.0 │
851
- └──────┴──────────┘
852
- >>> m.expr_2.evaluate()
853
- 63.0
854
- """
855
- assert self._model is not None, (
856
- "Expression must be added to the model to use .value"
857
- )
858
-
859
- df = self.data
860
- sm = self._model.poi
861
- attr = poi.VariableAttribute.Value
862
- for var_col in self._variable_columns:
863
- df = df.with_columns(
864
- (
865
- pl.col(COEF_KEY)
866
- * pl.col(var_col).map_elements(
867
- lambda v_id: (
868
- sm.get_variable_attribute(poi.VariableIndex(v_id), attr)
869
- if v_id != CONST_TERM
870
- else 1
871
- ),
872
- return_dtype=pl.Float64,
873
- )
874
- ).alias(COEF_KEY)
875
- ).drop(var_col)
876
-
877
- df = df.rename({COEF_KEY: SOLUTION_KEY})
878
-
879
- dims = self.dimensions
880
- if dims is not None:
881
- df = df.group_by(dims, maintain_order=True)
882
- return df.sum()
883
-
884
- def to_poi(self) -> poi.ScalarAffineFunction | poi.ScalarQuadraticFunction:
885
- if self.dimensions is not None:
886
- raise ValueError(
887
- "Only non-dimensioned expressions can be converted to PyOptInterface."
888
- ) # pragma: no cover
889
-
890
- if self.is_quadratic:
891
- return poi.ScalarQuadraticFunction(
892
- coefficients=self.data.get_column(COEF_KEY).to_numpy(),
893
- var1s=self.data.get_column(VAR_KEY).to_numpy(),
894
- var2s=self.data.get_column(QUAD_VAR_KEY).to_numpy(),
895
- )
896
- else:
897
- return poi.ScalarAffineFunction(
898
- coefficients=self.data.get_column(COEF_KEY).to_numpy(),
899
- variables=self.data.get_column(VAR_KEY).to_numpy(),
900
- )
901
-
902
- def to_str_table(self, include_const_term=True):
903
- data = self.data if include_const_term else self.variable_terms
904
- data = cast_coef_to_string(data)
905
-
906
- for var_col in self._variable_columns:
907
- temp_var_column = f"{var_col}_temp"
908
- if self._model is not None and self._model.var_map is not None:
909
- data = self._model.var_map.apply(
910
- data, to_col=temp_var_column, id_col=var_col
911
- )
912
- else:
913
- data = data.with_columns(
914
- pl.concat_str(pl.lit("x"), var_col).alias(temp_var_column)
915
- )
916
- data = data.with_columns(
917
- pl.when(pl.col(var_col) == CONST_TERM)
918
- .then(pl.lit(""))
919
- .otherwise(temp_var_column)
920
- .alias(var_col)
921
- ).drop(temp_var_column)
922
- if self.is_quadratic:
923
- data = data.with_columns(
924
- pl.when(pl.col(QUAD_VAR_KEY) == "")
925
- .then(pl.col(VAR_KEY))
926
- .otherwise(pl.concat_str(VAR_KEY, pl.lit(" * "), pl.col(QUAD_VAR_KEY)))
927
- .alias(VAR_KEY)
928
- ).drop(QUAD_VAR_KEY)
929
-
930
- dimensions = self.dimensions
931
-
932
- # Create a string for each term
933
- data = data.with_columns(
934
- expr=pl.concat_str(
935
- COEF_KEY,
936
- pl.lit(" "),
937
- VAR_KEY,
938
- )
939
- ).drop(COEF_KEY, VAR_KEY)
940
-
941
- if dimensions is not None:
942
- data = data.group_by(dimensions, maintain_order=True).agg(
943
- pl.col("expr").str.join(delimiter=" ")
944
- )
945
- else:
946
- data = data.select(pl.col("expr").str.join(delimiter=" "))
947
-
948
- # Remove leading +
949
- data = data.with_columns(pl.col("expr").str.strip_chars(characters=" +"))
950
-
951
- # TODO add vertical ... if too many rows, in the middle of the table
952
- if Config.print_max_lines:
953
- data = data.head(Config.print_max_lines)
954
-
955
- if Config.print_max_line_length:
956
- data = data.with_columns(
957
- pl.when(pl.col("expr").str.len_chars() > Config.print_max_line_length)
958
- .then(
959
- pl.concat_str(
960
- pl.col("expr").str.slice(0, Config.print_max_line_length),
961
- pl.lit("..."),
962
- )
963
- )
964
- .otherwise(pl.col("expr"))
965
- )
966
- return data
967
-
968
- def to_str_create_prefix(self, data):
969
- if self.name is None and self.dimensions is None:
970
- return data
971
-
972
- return (
973
- concat_dimensions(data, prefix=self.name, ignore_columns=["expr"])
974
- .with_columns(
975
- pl.concat_str("concated_dim", pl.lit(": "), "expr").alias("expr")
976
- )
977
- .drop("concated_dim")
978
- )
979
-
980
- def to_str(
981
- self,
982
- include_const_term=True,
983
- include_header=False,
984
- include_data=True,
985
- ):
986
- result = ""
987
- if include_header:
988
- result += get_obj_repr(
989
- self,
990
- size=len(self),
991
- dimensions=self.shape,
992
- terms=self.terms,
993
- degree=2 if self.degree() == 2 else None,
994
- )
995
- if include_header and include_data:
996
- result += "\n"
997
- if include_data:
998
- str_table = self.to_str_table(
999
- include_const_term=include_const_term,
1000
- )
1001
- str_table = self.to_str_create_prefix(str_table)
1002
- result += str_table.select(pl.col("expr").str.join(delimiter="\n")).item()
1003
-
1004
- return result
1005
-
1006
- def __repr__(self) -> str:
1007
- return self.to_str(include_header=True)
1008
-
1009
- def __str__(self) -> str:
1010
- return self.to_str()
1011
-
1012
- @property
1013
- def terms(self) -> int:
1014
- """
1015
- Number of terms across all subexpressions.
1016
-
1017
- Expressions equal to zero count as one term.
1018
-
1019
- Examples:
1020
- >>> import polars as pl
1021
- >>> m = pf.Model()
1022
- >>> m.v = pf.Variable({"t": [1, 2]})
1023
- >>> coef = pl.DataFrame({"t": [1, 2], "coef": [0, 1]})
1024
- >>> coef*(m.v+4)
1025
- <Expression size=2 dimensions={'t': 2} terms=3>
1026
- [1]: 0
1027
- [2]: 4 + v[2]
1028
- >>> (coef*(m.v+4)).terms
1029
- 3
1030
- """
1031
- return len(self.data)
1032
-
1033
-
1034
- @overload
1035
- def sum(over: Union[str, Sequence[str]], expr: SupportsToExpr) -> "Expression": ...
1036
-
1037
-
1038
- @overload
1039
- def sum(over: SupportsToExpr) -> "Expression": ...
1040
-
1041
-
1042
- def sum(
1043
- over: Union[str, Sequence[str], SupportsToExpr],
1044
- expr: Optional[SupportsToExpr] = None,
1045
- ) -> "Expression":
1046
- """
1047
- Sum an expression over specified dimensions.
1048
- If no dimensions are specified, the sum is taken over all of the expression's dimensions.
1049
-
1050
- Examples:
1051
- >>> expr = pl.DataFrame({
1052
- ... "time": ["mon", "tue", "wed", "mon", "tue"],
1053
- ... "place": ["Toronto", "Toronto", "Toronto", "Vancouver", "Vancouver"],
1054
- ... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6]
1055
- ... }).to_expr()
1056
- >>> expr
1057
- <Expression size=5 dimensions={'time': 3, 'place': 2} terms=5>
1058
- [mon,Toronto]: 1000000
1059
- [tue,Toronto]: 3000000
1060
- [wed,Toronto]: 2000000
1061
- [mon,Vancouver]: 1000000
1062
- [tue,Vancouver]: 2000000
1063
- >>> pf.sum("time", expr)
1064
- <Expression size=2 dimensions={'place': 2} terms=2>
1065
- [Toronto]: 6000000
1066
- [Vancouver]: 3000000
1067
- >>> pf.sum(expr)
1068
- <Expression size=1 dimensions={} terms=1>
1069
- 9000000
1070
- """
1071
- if expr is None:
1072
- assert isinstance(over, SupportsMath)
1073
- over = over.to_expr()
1074
- all_dims = over.dimensions
1075
- if all_dims is None:
1076
- raise ValueError(
1077
- "Cannot sum over dimensions with an expression with no dimensions."
1078
- )
1079
- return over.sum(all_dims)
1080
- else:
1081
- assert isinstance(over, (str, Sequence))
1082
- return expr.to_expr().sum(over)
1083
-
1084
-
1085
- def sum_by(by: Union[str, Sequence[str]], expr: SupportsToExpr) -> "Expression":
1086
- """
1087
- Like `pf.sum()`, but the sum is taken over all dimensions except those specified in `by` (just like a groupby operation).
1088
-
1089
- Examples:
1090
- >>> expr = pl.DataFrame({
1091
- ... "time": ["mon", "tue", "wed", "mon", "tue"],
1092
- ... "place": ["Toronto", "Toronto", "Toronto", "Vancouver", "Vancouver"],
1093
- ... "tiktok_posts": [1e6, 3e6, 2e6, 1e6, 2e6]
1094
- ... }).to_expr()
1095
- >>> expr
1096
- <Expression size=5 dimensions={'time': 3, 'place': 2} terms=5>
1097
- [mon,Toronto]: 1000000
1098
- [tue,Toronto]: 3000000
1099
- [wed,Toronto]: 2000000
1100
- [mon,Vancouver]: 1000000
1101
- [tue,Vancouver]: 2000000
1102
- >>> pf.sum_by("place", expr)
1103
- <Expression size=2 dimensions={'place': 2} terms=2>
1104
- [Toronto]: 6000000
1105
- [Vancouver]: 3000000
1106
- """
1107
- if isinstance(by, str):
1108
- by = [by]
1109
- expr = expr.to_expr()
1110
- dimensions = expr.dimensions
1111
- assert dimensions is not None, (
1112
- "Cannot sum by dimensions with an expression with no dimensions."
1113
- )
1114
- remaining_dims = [dim for dim in dimensions if dim not in by]
1115
- return sum(over=remaining_dims, expr=expr)
1116
-
1117
-
1118
- class Constraint(ModelElementWithId):
1119
- """A linear programming constraint."""
1120
-
1121
- def __init__(self, lhs: Expression, sense: ConstraintSense):
1122
- """Initialize a constraint.
1123
-
1124
- Parameters:
1125
- lhs:
1126
- The left hand side of the constraint.
1127
- sense:
1128
- The sense of the constraint.
1129
- """
1130
- self.lhs = lhs
1131
- self._model = lhs._model
1132
- self.sense = sense
1133
- self.to_relax: Optional[FuncArgs] = None
1134
- self.attr = Container(self._set_attribute, self._get_attribute)
1135
-
1136
- dims = self.lhs.dimensions
1137
- data = pl.DataFrame() if dims is None else self.lhs.data.select(dims).unique()
1138
-
1139
- super().__init__(data)
1140
-
1141
- def _set_attribute(self, name, value):
1142
- self._assert_has_ids()
1143
- col_name = name
1144
- try:
1145
- name = poi.ConstraintAttribute[name]
1146
- setter = self._model.poi.set_constraint_attribute
1147
- except KeyError:
1148
- setter = self._model.poi.set_constraint_raw_attribute
1149
-
1150
- constr_type = (
1151
- poi.ConstraintType.Quadratic
1152
- if self.lhs.is_quadratic
1153
- else poi.ConstraintType.Linear
1154
- )
1155
-
1156
- if self.dimensions is None:
1157
- for key in self.data.get_column(CONSTRAINT_KEY):
1158
- setter(poi.ConstraintIndex(constr_type, key), name, value)
1159
- else:
1160
- for key, value in (
1161
- self.data.join(value, on=self.dimensions, how="inner")
1162
- .select(pl.col(CONSTRAINT_KEY), pl.col(col_name))
1163
- .iter_rows()
1164
- ):
1165
- setter(poi.ConstraintIndex(constr_type, key), name, value)
1166
-
1167
- @unwrap_single_values
1168
- def _get_attribute(self, name):
1169
- self._assert_has_ids()
1170
- col_name = name
1171
- try:
1172
- name = poi.ConstraintAttribute[name]
1173
- getter = self._model.poi.get_constraint_attribute
1174
- except KeyError:
1175
- getter = self._model.poi.get_constraint_raw_attribute
1176
-
1177
- constr_type = (
1178
- poi.ConstraintType.Quadratic
1179
- if self.lhs.is_quadratic
1180
- else poi.ConstraintType.Linear
1181
- )
1182
-
1183
- ids = self.data.get_column(CONSTRAINT_KEY).to_list()
1184
- attr = [getter(poi.ConstraintIndex(constr_type, v_id), name) for v_id in ids]
1185
- data = self.data.with_columns(pl.Series(attr).alias(col_name))
1186
- return data.select(self.dimensions_unsafe + [col_name])
1187
-
1188
- def on_add_to_model(self, model: "Model", name: str):
1189
- super().on_add_to_model(model, name)
1190
- if self.to_relax is not None:
1191
- self.relax(*self.to_relax.args, **self.to_relax.kwargs)
1192
- self._assign_ids()
1193
-
1194
- def _assign_ids(self):
1195
- assert self._model is not None
1196
-
1197
- is_quadratic = self.lhs.is_quadratic
1198
- use_var_names = self._model.use_var_names
1199
- kwargs: Dict[str, Any] = dict(sense=self.sense.to_poi(), rhs=0)
1200
-
1201
- key_cols = [COEF_KEY] + self.lhs._variable_columns
1202
- key_cols_polars = [pl.col(c) for c in key_cols]
1203
-
1204
- add_constraint = (
1205
- self._model.poi.add_quadratic_constraint
1206
- if is_quadratic
1207
- else self._model.poi.add_linear_constraint
1208
- )
1209
- ScalarFunction = (
1210
- poi.ScalarQuadraticFunction if is_quadratic else poi.ScalarAffineFunction
1211
- )
1212
-
1213
- if self.dimensions is None:
1214
- if self._model.use_var_names:
1215
- kwargs["name"] = self.name
1216
- df = self.data.with_columns(
1217
- pl.lit(
1218
- add_constraint(
1219
- ScalarFunction(
1220
- *[self.lhs.data.get_column(c).to_numpy() for c in key_cols]
1221
- ),
1222
- **kwargs,
1223
- ).index
1224
- )
1225
- .alias(CONSTRAINT_KEY)
1226
- .cast(KEY_TYPE)
1227
- )
1228
- else:
1229
- df = self.lhs.data.group_by(self.dimensions, maintain_order=True).agg(
1230
- *key_cols_polars
1231
- )
1232
- if use_var_names:
1233
- df = (
1234
- concat_dimensions(df, prefix=self.name)
1235
- .with_columns(
1236
- pl.struct(*key_cols_polars, pl.col("concated_dim"))
1237
- .map_elements(
1238
- lambda x: add_constraint(
1239
- ScalarFunction(*[np.array(x[c]) for c in key_cols]),
1240
- name=x["concated_dim"],
1241
- **kwargs,
1242
- ).index,
1243
- return_dtype=KEY_TYPE,
1244
- )
1245
- .alias(CONSTRAINT_KEY)
1246
- )
1247
- .drop("concated_dim")
1248
- )
1249
- else:
1250
- df = df.with_columns(
1251
- pl.struct(*key_cols_polars)
1252
- .map_elements(
1253
- lambda x: add_constraint(
1254
- ScalarFunction(*[np.array(x[c]) for c in key_cols]),
1255
- **kwargs,
1256
- ).index,
1257
- return_dtype=KEY_TYPE,
1258
- )
1259
- .alias(CONSTRAINT_KEY)
1260
- )
1261
- df = df.drop(key_cols)
1262
-
1263
- self._data = df
1264
-
1265
- @property
1266
- @unwrap_single_values
1267
- def dual(self) -> Union[pl.DataFrame, float]:
1268
- dual = self.attr.Dual
1269
- if isinstance(dual, pl.DataFrame):
1270
- dual = dual.rename({"Dual": DUAL_KEY})
1271
- return dual
1272
-
1273
- @classmethod
1274
- def get_id_column_name(cls):
1275
- return CONSTRAINT_KEY
1276
-
1277
- def filter(self, *args, **kwargs) -> pl.DataFrame:
1278
- return self.lhs.data.filter(*args, **kwargs)
1279
-
1280
- def relax(
1281
- self, cost: SupportsToExpr, max: Optional[SupportsToExpr] = None
1282
- ) -> Constraint:
1283
- """
1284
- Relaxes the constraint by adding a variable to the constraint that can be non-zero at a cost.
1285
-
1286
- Parameters:
1287
- cost:
1288
- The cost of relaxing the constraint. Costs should be positives as they will automatically
1289
- become negative for maximization problems.
1290
- max:
1291
- The maximum value of the relaxation variable.
1292
-
1293
- Returns:
1294
- The same constraint
1295
-
1296
- Examples:
1297
- >>> m = pf.Model()
1298
- >>> m.hours_sleep = pf.Variable(lb=0)
1299
- >>> m.hours_day = pf.Variable(lb=0)
1300
- >>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
1301
- >>> m.maximize = m.hours_day
1302
- >>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
1303
- >>> m.optimize()
1304
- >>> m.hours_day.solution
1305
- 16.0
1306
- >>> m.maximize += 2 * m.hours_day
1307
- >>> m.optimize()
1308
- >>> m.hours_day.solution
1309
- 19.0
1310
-
1311
- Note: .relax() can only be called after the sense of the model has been defined.
1312
-
1313
- >>> m = pf.Model()
1314
- >>> m.hours_sleep = pf.Variable(lb=0)
1315
- >>> m.hours_day = pf.Variable(lb=0)
1316
- >>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
1317
- >>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
1318
- Traceback (most recent call last):
1319
- ...
1320
- ValueError: Cannot relax a constraint before the objective sense has been set. Try setting the objective first or using Model(sense=...).
1321
-
1322
- One way to solve this is by setting the sense directly on the model. See how this works fine:
1323
-
1324
- >>> m = pf.Model(sense="max")
1325
- >>> m.hours_sleep = pf.Variable(lb=0)
1326
- >>> m.hours_day = pf.Variable(lb=0)
1327
- >>> m.hours_in_day = m.hours_sleep + m.hours_day == 24
1328
- >>> m.must_sleep = (m.hours_sleep >= 8).relax(cost=2, max=3)
1329
-
1330
- And now an example with dimensions:
1331
-
1332
- >>> 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]})
1333
- >>> m.hours_spent = pf.Variable(homework_due_tomorrow[["project"]], lb=0)
1334
- >>> 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"]])
1335
- >>> m.only_one_day = sum("project", m.hours_spent) <= 24
1336
- >>> # Relaxing a constraint after it has already been assigned will give an error
1337
- >>> m.only_one_day.relax(1)
1338
- Traceback (most recent call last):
1339
- ...
1340
- ValueError: .relax() must be called before the Constraint is added to the model
1341
- >>> m.attr.Silent = True
1342
- >>> m.optimize()
1343
- >>> m.maximize.value
1344
- -50.0
1345
- >>> m.hours_spent.solution
1346
- shape: (3, 2)
1347
- ┌─────────┬──────────┐
1348
- │ project ┆ solution │
1349
- │ --- ┆ --- │
1350
- │ str ┆ f64 │
1351
- ╞═════════╪══════════╡
1352
- │ A ┆ 8.0 │
1353
- │ B ┆ 7.0 │
1354
- │ C ┆ 9.0 │
1355
- └─────────┴──────────┘
1356
- """
1357
- if self._has_ids:
1358
- raise ValueError(
1359
- ".relax() must be called before the Constraint is added to the model"
1360
- )
1361
-
1362
- m = self._model
1363
- if m is None or self.name is None:
1364
- self.to_relax = FuncArgs(args=[cost, max])
1365
- return self
1366
-
1367
- var_name = f"{self.name}_relaxation"
1368
- assert not hasattr(m, var_name), (
1369
- "Conflicting names, relaxation variable already exists on the model."
1370
- )
1371
- var = Variable(self, lb=0, ub=max)
1372
- setattr(m, var_name, var)
1373
-
1374
- if self.sense == ConstraintSense.LE:
1375
- self.lhs -= var
1376
- elif self.sense == ConstraintSense.GE:
1377
- self.lhs += var
1378
- else: # pragma: no cover
1379
- # TODO
1380
- raise NotImplementedError(
1381
- "Relaxation for equalities has not yet been implemented. Submit a pull request!"
1382
- )
1383
-
1384
- penalty = var * cost
1385
- if self.dimensions:
1386
- penalty = sum(self.dimensions, penalty)
1387
- if m.sense is None:
1388
- raise ValueError(
1389
- "Cannot relax a constraint before the objective sense has been set. Try setting the objective first or using Model(sense=...)."
1390
- )
1391
- elif m.sense == ObjSense.MAX:
1392
- penalty *= -1
1393
- if m.objective is None:
1394
- m.objective = penalty
1395
- else:
1396
- m.objective += penalty
1397
-
1398
- return self
1399
-
1400
- def to_str(self) -> str:
1401
- dims = self.dimensions
1402
- str_table = self.lhs.to_str_table(include_const_term=False)
1403
- str_table = self.lhs.to_str_create_prefix(str_table)
1404
- rhs = self.lhs.constant_terms.with_columns(pl.col(COEF_KEY) * -1)
1405
- rhs = cast_coef_to_string(rhs, drop_ones=False)
1406
- # Remove leading +
1407
- rhs = rhs.with_columns(pl.col(COEF_KEY).str.strip_chars(characters=" +"))
1408
- rhs = rhs.rename({COEF_KEY: "rhs"})
1409
- constr_str = pl.concat(
1410
- [str_table, rhs], how=("align" if dims else "horizontal")
1411
- )
1412
- constr_str = constr_str.select(
1413
- pl.concat_str("expr", pl.lit(f" {self.sense.value} "), "rhs").str.join(
1414
- delimiter="\n"
1415
- )
1416
- ).item()
1417
- return constr_str
1418
-
1419
- def __repr__(self) -> str:
1420
- return (
1421
- get_obj_repr(
1422
- self,
1423
- ("name",),
1424
- sense=f"'{self.sense.value}'",
1425
- size=len(self),
1426
- dimensions=self.shape,
1427
- terms=len(self.lhs.data),
1428
- )
1429
- + "\n"
1430
- + self.to_str()
1431
- )
1432
-
1433
-
1434
- class Variable(ModelElementWithId, SupportsMath, SupportPolarsMethodMixin):
1435
- """
1436
- Represents one or many decision variable in an optimization model.
1437
-
1438
- Parameters:
1439
- *indexing_sets:
1440
- If no indexing_sets are provided, a single variable with no dimensions is created.
1441
- Otherwise, a variable is created for each element in the Cartesian product of the indexing_sets (see Set for details on behaviour).
1442
- lb:
1443
- The lower bound for all variables.
1444
- ub:
1445
- The upper bound for all variables.
1446
- vtype:
1447
- The type of the variable. Can be either a VType enum or a string. Default is VType.CONTINUOUS.
1448
- equals:
1449
- When specified, a variable is created and a constraint is added to make the variable equal to the provided expression.
1450
-
1451
- Examples:
1452
- >>> import pandas as pd
1453
- >>> m = pf.Model()
1454
- >>> df = pd.DataFrame({"dim1": [1, 1, 2, 2, 3, 3], "dim2": ["a", "b", "a", "b", "a", "b"]})
1455
- >>> v = Variable(df)
1456
- >>> v
1457
- <Variable size=6 dimensions={'dim1': 3, 'dim2': 2} added_to_model=False>
1458
-
1459
- Variables cannot be used until they're added to the model.
1460
-
1461
- >>> m.constraint = v <= 3
1462
- Traceback (most recent call last):
1463
- ...
1464
- ValueError: Cannot use 'Variable' before it has beed added to a model.
1465
- >>> m.v = v
1466
- >>> m.constraint = m.v <= 3
1467
-
1468
- >>> m.v
1469
- <Variable name=v size=6 dimensions={'dim1': 3, 'dim2': 2}>
1470
- [1,a]: v[1,a]
1471
- [1,b]: v[1,b]
1472
- [2,a]: v[2,a]
1473
- [2,b]: v[2,b]
1474
- [3,a]: v[3,a]
1475
- [3,b]: v[3,b]
1476
- >>> m.v2 = Variable(df[["dim1"]])
1477
- Traceback (most recent call last):
1478
- ...
1479
- ValueError: Duplicate rows found in input data.
1480
- >>> m.v3 = Variable(df[["dim1"]].drop_duplicates())
1481
- >>> m.v3
1482
- <Variable name=v3 size=3 dimensions={'dim1': 3}>
1483
- [1]: v3[1]
1484
- [2]: v3[2]
1485
- [3]: v3[3]
1486
- """
1487
-
1488
- # TODO: Breaking change, remove support for Iterable[AcceptableSets]
1489
- def __init__(
1490
- self,
1491
- *indexing_sets: SetTypes | Iterable[SetTypes],
1492
- lb: float | int | SupportsToExpr | None = None,
1493
- ub: float | int | SupportsToExpr | None = None,
1494
- vtype: VType | VTypeValue = VType.CONTINUOUS,
1495
- equals: Optional[SupportsMath] = None,
1496
- ):
1497
- if equals is not None:
1498
- assert len(indexing_sets) == 0, (
1499
- "Cannot specify both 'equals' and 'indexing_sets'"
1500
- )
1501
- indexing_sets = (equals,)
1502
-
1503
- data = Set(*indexing_sets).data if len(indexing_sets) > 0 else pl.DataFrame()
1504
- super().__init__(data)
1505
-
1506
- self.vtype: VType = VType(vtype)
1507
- self.attr = Container(self._set_attribute, self._get_attribute)
1508
- self._equals = equals
1509
-
1510
- if lb is not None and not isinstance(lb, (float, int)):
1511
- self._lb_expr, self.lb = lb, None
1512
- else:
1513
- self._lb_expr, self.lb = None, lb
1514
- if ub is not None and not isinstance(ub, (float, int)):
1515
- self._ub_expr, self.ub = ub, None
1516
- else:
1517
- self._ub_expr, self.ub = None, ub
1518
-
1519
- def _set_attribute(self, name, value):
1520
- self._assert_has_ids()
1521
- col_name = name
1522
- try:
1523
- name = poi.VariableAttribute[name]
1524
- setter = self._model.poi.set_variable_attribute
1525
- except KeyError:
1526
- setter = self._model.poi.set_variable_raw_attribute
1527
-
1528
- if self.dimensions is None:
1529
- for key in self.data.get_column(VAR_KEY):
1530
- setter(poi.VariableIndex(key), name, value)
1531
- else:
1532
- for key, v in (
1533
- self.data.join(value, on=self.dimensions, how="inner")
1534
- .select(pl.col(VAR_KEY), pl.col(col_name))
1535
- .iter_rows()
1536
- ):
1537
- setter(poi.VariableIndex(key), name, v)
1538
-
1539
- @unwrap_single_values
1540
- def _get_attribute(self, name):
1541
- self._assert_has_ids()
1542
- col_name = name
1543
- try:
1544
- name = poi.VariableAttribute[name]
1545
- getter = self._model.poi.get_variable_attribute
1546
- except KeyError:
1547
- getter = self._model.poi.get_variable_raw_attribute
1548
-
1549
- ids = self.data.get_column(VAR_KEY).to_list()
1550
- attr = [getter(poi.VariableIndex(v_id), name) for v_id in ids]
1551
- data = self.data.with_columns(pl.Series(attr).alias(col_name))
1552
- return data.select(self.dimensions_unsafe + [col_name])
1553
-
1554
- def _assign_ids(self):
1555
- kwargs = dict(domain=self.vtype.to_poi())
1556
- if self.lb is not None:
1557
- kwargs["lb"] = self.lb
1558
- if self.ub is not None:
1559
- kwargs["ub"] = self.ub
1560
-
1561
- if self.dimensions is not None and self._model.use_var_names:
1562
- df = (
1563
- concat_dimensions(self.data, prefix=self.name)
1564
- .with_columns(
1565
- pl.col("concated_dim")
1566
- .map_elements(
1567
- lambda name: self._model.poi.add_variable(
1568
- name=name, **kwargs
1569
- ).index,
1570
- return_dtype=KEY_TYPE,
1571
- )
1572
- .alias(VAR_KEY)
1573
- )
1574
- .drop("concated_dim")
1575
- )
1576
- else:
1577
- if self._model.use_var_names:
1578
- kwargs["name"] = self.name
1579
-
1580
- df = self.data.with_columns(
1581
- pl.lit(0).alias(VAR_KEY).cast(KEY_TYPE)
1582
- ).with_columns(
1583
- pl.col(VAR_KEY).map_elements(
1584
- lambda _: self._model.poi.add_variable(**kwargs).index,
1585
- return_dtype=KEY_TYPE,
1586
- )
1587
- )
1588
-
1589
- self._data = df
1590
-
1591
- def on_add_to_model(self, model, name):
1592
- super().on_add_to_model(model, name)
1593
- self._assign_ids()
1594
- if self._lb_expr is not None:
1595
- setattr(model, f"{name}_lb", self._lb_expr <= self)
1596
-
1597
- if self._ub_expr is not None:
1598
- setattr(model, f"{name}_ub", self <= self._ub_expr)
1599
-
1600
- if self._equals is not None:
1601
- setattr(model, f"{name}_equals", self == self._equals)
1602
-
1603
- @classmethod
1604
- def get_id_column_name(cls):
1605
- return VAR_KEY
1606
-
1607
- @property
1608
- @unwrap_single_values
1609
- def solution(self):
1610
- """
1611
- Retrieve a variable's optimal value after the model has been solved.
1612
- Returned as a DataFrame if the variable has dimensions, otherwise as a single value.
1613
- Binary and integer variables are returned as integers.
1614
-
1615
- Examples:
1616
- >>> m = pf.Model()
1617
- >>> m.var_continuous = pf.Variable({"dim1": [1, 2, 3]}, lb=5, ub=5)
1618
- >>> m.var_integer = pf.Variable({"dim1": [1, 2, 3]}, lb=4.5, ub=5.5, vtype=VType.INTEGER)
1619
- >>> m.var_dimensionless = pf.Variable(lb=4.5, ub=5.5, vtype=VType.INTEGER)
1620
- >>> m.var_continuous.solution
1621
- Traceback (most recent call last):
1622
- ...
1623
- RuntimeError: Failed to retrieve solution for variable. Are you sure the model has been solved?
1624
- >>> m.optimize()
1625
- >>> m.var_continuous.solution
1626
- shape: (3, 2)
1627
- ┌──────┬──────────┐
1628
- │ dim1 ┆ solution │
1629
- │ --- ┆ --- │
1630
- │ i64 ┆ f64 │
1631
- ╞══════╪══════════╡
1632
- │ 1 ┆ 5.0 │
1633
- │ 2 ┆ 5.0 │
1634
- │ 3 ┆ 5.0 │
1635
- └──────┴──────────┘
1636
- >>> m.var_integer.solution
1637
- shape: (3, 2)
1638
- ┌──────┬──────────┐
1639
- │ dim1 ┆ solution │
1640
- │ --- ┆ --- │
1641
- │ i64 ┆ i64 │
1642
- ╞══════╪══════════╡
1643
- │ 1 ┆ 5 │
1644
- │ 2 ┆ 5 │
1645
- │ 3 ┆ 5 │
1646
- └──────┴──────────┘
1647
- >>> m.var_dimensionless.solution
1648
- 5
1649
- """
1650
- try:
1651
- solution = self.attr.Value
1652
- except RuntimeError as e:
1653
- raise RuntimeError(
1654
- "Failed to retrieve solution for variable. Are you sure the model has been solved?"
1655
- ) from e
1656
- if isinstance(solution, pl.DataFrame):
1657
- solution = solution.rename({"Value": SOLUTION_KEY})
1658
-
1659
- if self.vtype in [VType.BINARY, VType.INTEGER]:
1660
- if isinstance(solution, pl.DataFrame):
1661
- solution = solution.with_columns(
1662
- pl.col("solution").alias("solution_float"),
1663
- pl.col("solution").round().cast(pl.Int64),
1664
- )
1665
- if Config.integer_tolerance != 0:
1666
- df = solution.filter(
1667
- (pl.col("solution_float") - pl.col("solution")).abs()
1668
- > Config.integer_tolerance
1669
- )
1670
- assert df.is_empty(), (
1671
- f"Variable {self.name} has a non-integer value: {df}\nThis should not happen."
1672
- )
1673
- solution = solution.drop("solution_float")
1674
- else:
1675
- solution_float = solution
1676
- solution = int(round(solution))
1677
- if Config.integer_tolerance != 0:
1678
- assert abs(solution - solution_float) < Config.integer_tolerance, (
1679
- f"Value of variable {self.name} is not an integer: {solution}. This should not happen."
1680
- )
1681
-
1682
- return solution
1683
-
1684
- def __repr__(self):
1685
- if self._has_ids:
1686
- return (
1687
- get_obj_repr(
1688
- self,
1689
- ("name", "lb", "ub"),
1690
- size=self.data.height,
1691
- dimensions=self.shape,
1692
- )
1693
- + "\n"
1694
- + self.to_expr().to_str()
1695
- )
1696
- else:
1697
- return get_obj_repr(
1698
- self,
1699
- ("name", "lb", "ub"),
1700
- size=self.data.height,
1701
- dimensions=self.shape,
1702
- added_to_model=False,
1703
- )
1704
-
1705
- def to_expr(self) -> Expression:
1706
- self._assert_has_ids()
1707
- return self._new(self.data.drop(SOLUTION_KEY, strict=False))
1708
-
1709
- def _new(self, data: pl.DataFrame):
1710
- self._assert_has_ids()
1711
- e = Expression(data.with_columns(pl.lit(1.0).alias(COEF_KEY)))
1712
- e._model = self._model
1713
- # We propogate the unmatched strategy intentionally. Without this a .keep_unmatched() on a variable would always be lost.
1714
- e.unmatched_strategy = self.unmatched_strategy
1715
- e.allowed_new_dims = self.allowed_new_dims
1716
- return e
1717
-
1718
- def next(self, dim: str, wrap_around: bool = False) -> Expression:
1719
- """
1720
- Creates an expression where the variable at each index is the next variable in the specified dimension.
1721
-
1722
- Parameters:
1723
- dim:
1724
- The dimension over which to shift the variable.
1725
- wrap_around:
1726
- If True, the last index in the dimension is connected to the first index.
1727
-
1728
- Examples:
1729
- >>> import pandas as pd
1730
- >>> time_dim = pd.DataFrame({"time": ["00:00", "06:00", "12:00", "18:00"]})
1731
- >>> space_dim = pd.DataFrame({"city": ["Toronto", "Berlin"]})
1732
- >>> m = pf.Model()
1733
- >>> m.bat_charge = pf.Variable(time_dim, space_dim)
1734
- >>> m.bat_flow = pf.Variable(time_dim, space_dim)
1735
- >>> # Fails because the dimensions are not the same
1736
- >>> m.bat_charge + m.bat_flow == m.bat_charge.next("time")
1737
- Traceback (most recent call last):
1738
- ...
1739
- pyoframe.constants.PyoframeError: Failed to add expressions:
1740
- <Expression size=8 dimensions={'time': 4, 'city': 2} terms=16> + <Expression size=6 dimensions={'city': 2, 'time': 3} terms=6>
1741
- Due to error:
1742
- Dataframe has unmatched values. If this is intentional, use .drop_unmatched() or .keep_unmatched()
1743
- shape: (2, 4)
1744
- ┌───────┬─────────┬────────────┬────────────┐
1745
- │ time ┆ city ┆ time_right ┆ city_right │
1746
- │ --- ┆ --- ┆ --- ┆ --- │
1747
- │ str ┆ str ┆ str ┆ str │
1748
- ╞═══════╪═════════╪════════════╪════════════╡
1749
- │ 18:00 ┆ Toronto ┆ null ┆ null │
1750
- │ 18:00 ┆ Berlin ┆ null ┆ null │
1751
- └───────┴─────────┴────────────┴────────────┘
1752
-
1753
- >>> (m.bat_charge + m.bat_flow).drop_unmatched() == m.bat_charge.next("time")
1754
- <Constraint sense='=' size=6 dimensions={'time': 3, 'city': 2} terms=18>
1755
- [00:00,Berlin]: bat_charge[00:00,Berlin] + bat_flow[00:00,Berlin] - bat_charge[06:00,Berlin] = 0
1756
- [00:00,Toronto]: bat_charge[00:00,Toronto] + bat_flow[00:00,Toronto] - bat_charge[06:00,Toronto] = 0
1757
- [06:00,Berlin]: bat_charge[06:00,Berlin] + bat_flow[06:00,Berlin] - bat_charge[12:00,Berlin] = 0
1758
- [06:00,Toronto]: bat_charge[06:00,Toronto] + bat_flow[06:00,Toronto] - bat_charge[12:00,Toronto] = 0
1759
- [12:00,Berlin]: bat_charge[12:00,Berlin] + bat_flow[12:00,Berlin] - bat_charge[18:00,Berlin] = 0
1760
- [12:00,Toronto]: bat_charge[12:00,Toronto] + bat_flow[12:00,Toronto] - bat_charge[18:00,Toronto] = 0
1761
-
1762
- >>> (m.bat_charge + m.bat_flow) == m.bat_charge.next("time", wrap_around=True)
1763
- <Constraint sense='=' size=8 dimensions={'time': 4, 'city': 2} terms=24>
1764
- [00:00,Berlin]: bat_charge[00:00,Berlin] + bat_flow[00:00,Berlin] - bat_charge[06:00,Berlin] = 0
1765
- [00:00,Toronto]: bat_charge[00:00,Toronto] + bat_flow[00:00,Toronto] - bat_charge[06:00,Toronto] = 0
1766
- [06:00,Berlin]: bat_charge[06:00,Berlin] + bat_flow[06:00,Berlin] - bat_charge[12:00,Berlin] = 0
1767
- [06:00,Toronto]: bat_charge[06:00,Toronto] + bat_flow[06:00,Toronto] - bat_charge[12:00,Toronto] = 0
1768
- [12:00,Berlin]: bat_charge[12:00,Berlin] + bat_flow[12:00,Berlin] - bat_charge[18:00,Berlin] = 0
1769
- [12:00,Toronto]: bat_charge[12:00,Toronto] + bat_flow[12:00,Toronto] - bat_charge[18:00,Toronto] = 0
1770
- [18:00,Berlin]: bat_charge[18:00,Berlin] + bat_flow[18:00,Berlin] - bat_charge[00:00,Berlin] = 0
1771
- [18:00,Toronto]: bat_charge[18:00,Toronto] + bat_flow[18:00,Toronto] - bat_charge[00:00,Toronto] = 0
1772
- """
1773
-
1774
- wrapped = self.data.select(dim).unique(maintain_order=True).sort(by=dim)
1775
- wrapped = wrapped.with_columns(pl.col(dim).shift(-1).alias("__next"))
1776
- if wrap_around:
1777
- wrapped = wrapped.with_columns(pl.col("__next").fill_null(pl.first(dim)))
1778
- else:
1779
- wrapped = wrapped.drop_nulls(dim)
1780
-
1781
- expr = self.to_expr()
1782
- data = expr.data.rename({dim: "__prev"})
1783
-
1784
- data = data.join(
1785
- wrapped, left_on="__prev", right_on="__next", how="inner"
1786
- ).drop(["__prev", "__next"], strict=False)
1787
- return expr._new(data)