Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
58c12f8
WIP
tbittar Jun 21, 2024
0f285c6
Operator simplification on construction
tbittar Jun 21, 2024
ef9c8b6
Improve common operations on linear expr
tbittar Jun 25, 2024
4f0542c
Handle constraints
tbittar Jun 25, 2024
946c366
Equality comparator for linear expression
tbittar Jun 26, 2024
e1ebb29
WIP
tbittar Jun 26, 2024
10c2066
Sum shift
tbittar Jun 27, 2024
d13a649
WIP
tbittar Jul 11, 2024
75a5881
Shift and eval implementation in progress
tbittar Jul 12, 2024
2860242
TimeShift hashing
tbittar Jul 12, 2024
b640b22
Implement shift, eval and time sum of linear expressions
tbittar Jul 15, 2024
5f2823f
Test sum of linear expressions
tbittar Jul 15, 2024
e90254e
Fix printing term tests
tbittar Jul 16, 2024
c5214eb
More online simplifications, start handling constraints and ports
tbittar Jul 16, 2024
9ca614a
Make param and literal return expression node to later handle Express…
tbittar Jul 22, 2024
3d03d86
Fix model test
tbittar Jul 22, 2024
3251495
Fix test imports and port definition validation
tbittar Jul 23, 2024
6f85db0
Design problem between time operator / expression node
tbittar Jul 23, 2024
13ea7de
Fix circular imports
tbittar Jul 25, 2024
d092b1b
Fix syntax
tbittar Jul 26, 2024
48c96ff
Resolve linear expression
tbittar Jul 26, 2024
9de9cae
Fix circular imports
tbittar Aug 14, 2024
3a4004c
Parameter evaluation visitor implemented
tbittar Aug 14, 2024
8007306
Be able to create variables
tbittar Aug 14, 2024
c03705c
Merge branch 'main' into feature/better_expression_linearization
tbittar Aug 16, 2024
1e1ac18
Test resolve coefficients, update test evaluation context
tbittar Aug 16, 2024
1be9d84
Improve resolve coefficient API
tbittar Aug 16, 2024
b532a41
Start resolve variables, separate optimization context from optimizat…
tbittar Aug 19, 2024
b429782
Set objective
tbittar Aug 19, 2024
bda088c
Fix shift distribution and add component context for time operators
tbittar Aug 20, 2024
2c723cd
Temporary API for single shift over ExpressionNodeEfficient
tbittar Aug 21, 2024
27f8ad8
Fix variable get structure
tbittar Aug 21, 2024
bcafb95
Fix expectation computation
tbittar Aug 21, 2024
b0197da
Feature/update yaml parsing (#51)
tbittar Aug 21, 2024
1911e29
Fix interaction resolve ports / add component context
tbittar Aug 21, 2024
a904d9d
Uniformize imports
tbittar Aug 21, 2024
8b7a244
Fix some type checking issues, remove useless code
tbittar Aug 21, 2024
85d1628
Remove useless commented code
tbittar Aug 21, 2024
b5c3328
Remove useless commented code
tbittar Aug 21, 2024
344ac65
Improve type checking for constraint
tbittar Aug 22, 2024
600d1bc
Improve type checking for port field def
tbittar Aug 22, 2024
d3317ef
Improve type checking for **kwargs
tbittar Aug 22, 2024
7c9d427
Type checking and reformatting
tbittar Aug 22, 2024
6022781
Remove useless code
tbittar Aug 23, 2024
93eda7e
Rename files
tbittar Aug 27, 2024
952bce4
Rename file
tbittar Aug 27, 2024
85f3953
Remove 'efficient' suffix
tbittar Aug 27, 2024
f50f57f
Rename test files
tbittar Aug 27, 2024
abd1eed
Remove useless comments
tbittar Aug 27, 2024
f6a03c6
Comment and reformatting
tbittar Aug 27, 2024
9431fe7
WIP for parsing
tbittar Aug 27, 2024
02f8a39
WIP for yaml parsing
tbittar Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix shift distribution and add component context for time operators
  • Loading branch information
tbittar committed Aug 20, 2024
commit bda088c2f7b563723759dc9f95b08c7e4c8eb8fa
4 changes: 4 additions & 0 deletions src/andromede/expression/evaluate_parameters_efficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def time_aggregator(
for k in operand_dict.keys()
if k.scenario == scenario
)
# As the sum aggregates on time, time indices on which to evaluate parent expression collapses on row_id.time
self.time_scenario_indices.time_indices = [self.row_id.time]
return result
else:
return NotImplemented
Expand All @@ -185,6 +187,8 @@ def scenario_operator(
operand_dict[k] for k in operand_dict.keys() if k.time == time
)
)
# As the expectation aggregates on scenario, scenario indices on which to evaluate parent expression collapses on row_id.scenario
self.time_scenario_indices.scenario_indices = [self.row_id.scenario]
return result

else:
Expand Down
66 changes: 52 additions & 14 deletions src/andromede/expression/linear_expression_efficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Sequence,
TypeVar,
Union,
cast,
overload,
)

Expand Down Expand Up @@ -231,26 +232,25 @@ def sum(
if shift is not None and eval is not None:
raise ValueError("Only shift or eval arguments should specified, not both.")

# The shift or eval operators distribute over the coefficients whereas the sum only applies to the whole as (param("a") * var("x")).shift([1,5]) represents: a[t+1]x[t+1] + ... + a[t+5]x[t+5]
# And (param("a") * var("x")).eval([1,5]) represents: a[1]x[1] + ... + a[5]x[5]
# The shift or eval operators applies on the variable, then it will define at which time step the term coefficient * variable will be evaluated

if shift is not None:
return dataclasses.replace(
self,
coefficient=TimeOperatorNode(
self.coefficient, TimeOperatorName.SHIFT, InstancesTimeIndex(shift)
),
# coefficient=TimeOperatorNode(
# self.coefficient, TimeOperatorName.SHIFT, InstancesTimeIndex(shift)
# ),
time_operator=TimeShift(InstancesTimeIndex(shift)),
time_aggregator=TimeSum(stay_roll=True),
)
elif eval is not None:
return dataclasses.replace(
self,
coefficient=TimeOperatorNode(
self.coefficient,
TimeOperatorName.EVALUATION,
InstancesTimeIndex(eval),
),
# coefficient=TimeOperatorNode(
# self.coefficient,
# TimeOperatorName.EVALUATION,
# InstancesTimeIndex(eval),
# ),
time_operator=TimeEvaluation(InstancesTimeIndex(eval)),
time_aggregator=TimeSum(stay_roll=True),
)
Expand Down Expand Up @@ -380,8 +380,7 @@ def _merge_dicts(
rhs: Dict[TermKeyEfficient, TermEfficient],
merge_func: Callable[[TermEfficient, TermEfficient], TermEfficient],
neutral: float,
) -> Dict[TermKeyEfficient, TermEfficient]:
...
) -> Dict[TermKeyEfficient, TermEfficient]: ...


@overload
Expand All @@ -390,8 +389,7 @@ def _merge_dicts(
rhs: Dict[PortFieldId, PortFieldTerm],
merge_func: Callable[[PortFieldTerm, PortFieldTerm], PortFieldTerm],
neutral: float,
) -> Dict[PortFieldId, PortFieldTerm]:
...
) -> Dict[PortFieldId, PortFieldTerm]: ...


def _get_neutral_term(term: T_val, neutral: float) -> T_val:
Expand Down Expand Up @@ -959,10 +957,21 @@ def add_component_context(self, component_id: str) -> "LinearExpressionEfficient
raise ValueError(
"This expression has already been associated to another component."
)

result_term = dataclasses.replace(
term,
component_id=component_id,
coefficient=add_component_context(component_id, term.coefficient),
time_operator=(
dataclasses.replace(
term.time_operator,
time_ids=_add_component_context_to_instances_index(
component_id, term.time_operator.time_ids
),
)
if term.time_operator
else None
),
)
result_terms[generate_key(result_term)] = result_term
result_constant = add_component_context(component_id, self.constant)
Expand All @@ -971,6 +980,35 @@ def add_component_context(self, component_id: str) -> "LinearExpressionEfficient
)


def _add_component_context_to_expression_range(
component_id: str, expression_range: ExpressionRange
) -> ExpressionRange:
return ExpressionRange(
start=add_component_context(component_id, expression_range.start),
stop=add_component_context(component_id, expression_range.stop),
step=(
add_component_context(component_id, expression_range.step)
if expression_range.step is not None
else None
),
)


def _add_component_context_to_instances_index(
component_id: str, instances_index: InstancesTimeIndex
) -> InstancesTimeIndex:
expressions = instances_index.expressions
if isinstance(expressions, ExpressionRange):
return InstancesTimeIndex(
_add_component_context_to_expression_range(component_id, expressions)
)
if isinstance(expressions, list):
expressions_list = cast(List[ExpressionNodeEfficient], expressions)
copy = [add_component_context(component_id, e) for e in expressions_list]
return InstancesTimeIndex(copy)
raise ValueError("Unexpected type in instances index")


def linear_expressions_equal(
lhs: LinearExpressionEfficient, rhs: LinearExpressionEfficient
) -> bool:
Expand Down
26 changes: 15 additions & 11 deletions src/andromede/simulation/linear_expression_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ def resolve(
# a_t * sum(x_t')
# a_t * x_t
# TODO: Next line is to be moved inside the for loop once we have figured out how to represent sum(a_t * x_t)
resolved_coeff = resolve_coefficient(
term.coefficient, self.value_provider, row_id
)

for ts_id, lp_variable in resolved_variables.items():
# TODO: Could we check in which case coeff resolution leads to the same result for each element in the for loop ? When there is only a literal, etc, etc ?
resolved_coeff = resolve_coefficient(
term.coefficient,
self.value_provider,
RowIndex(ts_id.time, ts_id.scenario),
)
resolved_terms.append(ResolvedTerm(resolved_coeff, lp_variable))

resolved_constant = resolve_coefficient(
Expand All @@ -80,14 +84,14 @@ def resolve_variables(
operator_ts_ids = self._row_id_to_term_time_scenario_id(term, row_id)
for time in operator_ts_ids.time_indices:
for scenario in operator_ts_ids.scenario_indices:
solver_vars[
TimeScenarioIndex(time, scenario)
] = self.context.get_component_variable(
time,
scenario,
term.component_id,
term.variable_name,
term.structure,
solver_vars[TimeScenarioIndex(time, scenario)] = (
self.context.get_component_variable(
time,
scenario,
term.component_id,
term.variable_name,
term.structure,
)
)
return solver_vars

Expand Down
20 changes: 4 additions & 16 deletions tests/unittests/expressions/test_expressions_efficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,7 @@ def test_comparison() -> None:
), # The internal representation of shift(1) is sum(shift=1)
scenario_aggregator=None,
): TermEfficient(
TimeOperatorNode(
LiteralNode(1), TimeOperatorName.SHIFT, InstancesTimeIndex(1)
),
LiteralNode(1),
"",
"x",
time_operator=TimeShift(
Expand All @@ -432,9 +430,7 @@ def test_comparison() -> None:
time_aggregator=TimeSum(stay_roll=True),
scenario_aggregator=None,
): TermEfficient(
TimeOperatorNode(
LiteralNode(1), TimeOperatorName.SHIFT, InstancesTimeIndex(1)
),
LiteralNode(1),
"",
"y",
time_operator=TimeShift(InstancesTimeIndex(1)),
Expand All @@ -461,11 +457,7 @@ def test_comparison() -> None:
), # The internal representation of eval(1) is sum(eval=1)
scenario_aggregator=None,
): TermEfficient(
TimeOperatorNode(
LiteralNode(1),
TimeOperatorName.EVALUATION,
InstancesTimeIndex(1),
),
LiteralNode(1),
"",
"x",
time_operator=TimeEvaluation(
Expand All @@ -482,11 +474,7 @@ def test_comparison() -> None:
time_aggregator=TimeSum(stay_roll=True),
scenario_aggregator=None,
): TermEfficient(
TimeOperatorNode(
LiteralNode(1),
TimeOperatorName.EVALUATION,
InstancesTimeIndex(1),
),
LiteralNode(1),
"",
"y",
time_operator=TimeEvaluation(InstancesTimeIndex(1)),
Expand Down
21 changes: 21 additions & 0 deletions tests/unittests/expressions/test_resolve_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
TimeOperatorName,
TimeOperatorNode,
comp_param,
literal,
param,
)
from andromede.expression.indexing_structure import IndexingStructure, RowIndex
Expand Down Expand Up @@ -275,6 +276,7 @@ def test_resolve_coefficient_on_elementary_operations(
[
(param("p").shift(2).sum(), RowIndex(0, 0), 3.0),
(param("p").shift(-1).sum(), RowIndex(2, 1), 5.0),
(literal(0).shift(-1).sum(), RowIndex(0, 0), 0.0),
(param("p").eval(2).sum(), RowIndex(0, 0), 3.0),
(param("p").eval(2).sum(), RowIndex(2, 0), 3.0),
(param("p").shift(ExpressionRange(0, 3)).sum(), RowIndex(0, 0), 13.0),
Expand Down Expand Up @@ -311,3 +313,22 @@ def test_resolve_coefficient_on_expectation(
provider: CustomValueProvider,
) -> None:
assert math.isclose(resolve_coefficient(expr, provider, row_id), expected)


@pytest.mark.parametrize(
"expr, row_id, expected",
[
(param("p").expec().sum(), RowIndex(0, 0), 18.0),
(param("p").sum().expec(), RowIndex(0, 0), 18.0),
(param("p").shift(comp_param("c", "q")).sum().expec(), RowIndex(1, 0), 6.5),
(param("p").expec().shift(comp_param("c", "q")).sum(), RowIndex(1, 0), 7.5),
(param("p").shift(comp_param("c", "q")).expec().sum(), RowIndex(1, 0), 6.5),
],
)
def test_resolve_coefficient_on_sum_and_expectation(
expr: ExpressionNodeEfficient,
row_id: RowIndex,
expected: float,
provider: CustomValueProvider,
) -> None:
assert math.isclose(resolve_coefficient(expr, provider, row_id), expected)