Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions grammar/Expr.g4
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ expr
| expr op=('+' | '-') expr # addsub
| expr COMPARISON expr # comparison
| IDENTIFIER '(' expr ')' # function
| IDENTIFIER '[' shift (',' shift)* ']' # timeShift
| IDENTIFIER '[' expr (',' expr )* ']' # timeIndex
| IDENTIFIER '[' shift1=shift '..' shift2=shift ']' # timeShiftRange
| IDENTIFIER '[' expr '..' expr ']' # timeRange
| IDENTIFIER '[' shift ']' # timeShift
| IDENTIFIER '[' expr ']' # timeIndex
| TIME_SUM '(' ((expr | shift | timeShiftRange | timeRange) ',')? IDENTIFIER ')' #timeSum
;

atom
: NUMBER # number
| IDENTIFIER # identifier
;

timeShiftRange: shift1=shift '..' shift2=shift;
timeRange: expr1=expr '..' expr2=expr;

// a shift is required to be either "t" or "t + ..." or "t - ..."
// Note: simply defining it as "shift: TIME ('+' | '-') expr" won't work
// because the minus sign will not have the expected precedence:
Expand Down Expand Up @@ -74,5 +76,6 @@ NUMBER : DIGIT+ ('.' DIGIT+)?;
TIME : 't';
IDENTIFIER : CHAR CHAR_OR_DIGIT*;
COMPARISON : ( '=' | '>=' | '<=' );
TIME_SUM : 'sum';

WS: (' ' | '\t' | '\r'| '\n') -> skip;
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ types-PyYAML~=6.0.12.12
antlr4-tools~=0.2.1
pandas~=2.0.3
pandas-stubs<=2.0.3
types-PyYAML~=6.0.12
25 changes: 12 additions & 13 deletions src/andromede/expression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@
# This file is part of the Antares project.

from .copy import CopyVisitor, copy_expression
from .degree import ExpressionDegreeVisitor, compute_degree
from .evaluate import EvaluationContext, EvaluationVisitor, ValueProvider, evaluate
from .evaluate_parameters import (
ParameterResolver,
ParameterValueProvider,
resolve_parameters,
)
from .evaluate_parameters import ValueProvider
from .expression import (
AdditionNode,
Comparator,
ComparisonNode,
ComponentParameterNode,
DivisionNode,
ExpressionNode,
ExpressionRange,
InstancesTimeIndex,
LiteralNode,
MultiplicationNode,
NegationNode,
ParameterNode,
PortFieldAggregatorNode,
PortFieldNode,
ScenarioOperatorName,
ScenarioOperatorNode,
SubstractionNode,
VariableNode,
literal,
param,
sum_expressions,
var,
TimeAggregatorName,
TimeAggregatorNode,
TimeOperatorName,
TimeOperatorNode,
)
from .print import PrinterVisitor, print_expr
from .visitor import ExpressionVisitor, visit
21 changes: 2 additions & 19 deletions src/andromede/expression/context_adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
from dataclasses import dataclass

from . import CopyVisitor
from .expression import (
ComponentParameterNode,
ComponentVariableNode,
ExpressionNode,
ParameterNode,
VariableNode,
)
from .expression import ComponentParameterNode, ExpressionNode, ParameterNode
from .visitor import visit


Expand All @@ -32,21 +26,10 @@ class ContextAdder(CopyVisitor):

component_id: str

def variable(self, node: VariableNode) -> ExpressionNode:
return ComponentVariableNode(self.component_id, node.name)

def parameter(self, node: ParameterNode) -> ExpressionNode:
return ComponentParameterNode(self.component_id, node.name)

def comp_variable(self, node: ComponentVariableNode) -> ExpressionNode:
raise ValueError(
"This expression has already been associated to another component."
)

def comp_parameter(self, node: ComponentParameterNode) -> ExpressionNode:
raise ValueError(
"This expression has already been associated to another component."
)
# Nothing is done is a component parameter node is encountered as it may have been generated from port resolution


def add_component_context(id: str, expression: ExpressionNode) -> ExpressionNode:
Expand Down
25 changes: 7 additions & 18 deletions src/andromede/expression/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,23 @@
# This file is part of the Antares project.

from dataclasses import dataclass
from typing import List, Union, cast
from typing import List, cast

from .expression import (
AdditionNode,
ComparisonNode,
ComponentParameterNode,
ComponentVariableNode,
DivisionNode,
ExpressionNode,
ExpressionRange,
InstancesTimeIndex,
LiteralNode,
MultiplicationNode,
NegationNode,
ParameterNode,
PortFieldAggregatorNode,
PortFieldNode,
ScenarioOperatorNode,
SubstractionNode,
TimeAggregatorNode,
TimeOperatorNode,
VariableNode,
)
from .visitor import ExpressionVisitor, ExpressionVisitorOperations, T, visit
from .visitor import ExpressionVisitorOperations, visit


@dataclass(frozen=True)
Expand All @@ -51,15 +44,9 @@ def comparison(self, node: ComparisonNode) -> ExpressionNode:
visit(node.left, self), visit(node.right, self), node.comparator
)

def variable(self, node: VariableNode) -> ExpressionNode:
return VariableNode(node.name)

def parameter(self, node: ParameterNode) -> ExpressionNode:
return ParameterNode(node.name)

def comp_variable(self, node: ComponentVariableNode) -> ExpressionNode:
return ComponentVariableNode(node.component_id, node.name)

def comp_parameter(self, node: ComponentParameterNode) -> ExpressionNode:
return ComponentParameterNode(node.component_id, node.name)

Expand All @@ -69,9 +56,11 @@ def copy_expression_range(
return ExpressionRange(
start=visit(expression_range.start, self),
stop=visit(expression_range.stop, self),
step=visit(expression_range.step, self)
if expression_range.step is not None
else None,
step=(
visit(expression_range.step, self)
if expression_range.step is not None
else None
),
)

def copy_instances_index(
Expand Down
122 changes: 0 additions & 122 deletions src/andromede/expression/degree.py

This file was deleted.

33 changes: 19 additions & 14 deletions src/andromede/expression/equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
from dataclasses import dataclass
from typing import Optional

from andromede.expression import (
from andromede.expression.expression import (
AdditionNode,
BinaryOperatorNode,
ComparisonNode,
ComponentParameterNode,
DivisionNode,
ExpressionNode,
ExpressionRange,
InstancesTimeIndex,
LiteralNode,
MultiplicationNode,
NegationNode,
ParameterNode,
SubstractionNode,
VariableNode,
)
from andromede.expression.expression import (
BinaryOperatorNode,
ExpressionRange,
InstancesTimeIndex,
PortFieldAggregatorNode,
PortFieldNode,
ScenarioOperatorNode,
SubstractionNode,
TimeAggregatorNode,
TimeOperatorNode,
)
Expand Down Expand Up @@ -72,10 +70,12 @@ def visit(self, left: ExpressionNode, right: ExpressionNode) -> bool:
return self.multiplication(left, right)
if isinstance(left, ComparisonNode) and isinstance(right, ComparisonNode):
return self.comparison(left, right)
if isinstance(left, VariableNode) and isinstance(right, VariableNode):
return self.variable(left, right)
if isinstance(left, ParameterNode) and isinstance(right, ParameterNode):
return self.parameter(left, right)
if isinstance(left, ComponentParameterNode) and isinstance(
right, ComponentParameterNode
):
return self.comp_parameter(left, right)
if isinstance(left, TimeOperatorNode) and isinstance(right, TimeOperatorNode):
return self.time_operator(left, right)
if isinstance(left, TimeAggregatorNode) and isinstance(
Expand Down Expand Up @@ -124,12 +124,14 @@ def division(self, left: DivisionNode, right: DivisionNode) -> bool:
def comparison(self, left: ComparisonNode, right: ComparisonNode) -> bool:
return left.comparator == right.comparator and self._visit_operands(left, right)

def variable(self, left: VariableNode, right: VariableNode) -> bool:
return left.name == right.name

def parameter(self, left: ParameterNode, right: ParameterNode) -> bool:
return left.name == right.name

def comp_parameter(
self, left: ComponentParameterNode, right: ComponentParameterNode
) -> bool:
return left.component_id == right.component_id and left.name == right.name

def expression_range(self, left: ExpressionRange, right: ExpressionRange) -> bool:
if not self.visit(left.start, right.start):
return False
Expand Down Expand Up @@ -183,7 +185,10 @@ def port_field_aggregator(


def expressions_equal(
left: ExpressionNode, right: ExpressionNode, abs_tol: float = 0, rel_tol: float = 0
left: ExpressionNode,
right: ExpressionNode,
abs_tol: float = 0,
rel_tol: float = 0,
) -> bool:
"""
True if both expression nodes are equal. Literal values may be compared with absolute or relative tolerance.
Expand Down
Loading