Skip to content

Commit

Permalink
Merge branch 'main' into feature/investment_pathway
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmnz committed Nov 28, 2024
2 parents 95e07ec + b1649dc commit b914bc1
Show file tree
Hide file tree
Showing 86 changed files with 3,895 additions and 2,609 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ venv
coverage.xml
bin
outputs
build
src/andromede_modeling_prototype.egg-info/
8 changes: 4 additions & 4 deletions grammar/Expr.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ expr
| expr op=('/' | '*') expr # muldiv
| expr op=('+' | '-') expr # addsub
| expr COMPARISON expr # comparison
| 'sum' '(' expr ')' # allTimeSum
| 'sum' '(' from=shift '..' to=shift ',' expr ')' # timeSum
| 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
;

atom
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ dependencies = [
"ortools"
]

[project.scripts]
andromede-simulator = "andromede.main.main:main_cli"

[tool.setuptools.packages.find]
# All the following settings are optional:
where = ["src"]
Expand Down
1 change: 0 additions & 1 deletion src/andromede/expression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
MultiplicationNode,
NegationNode,
ParameterNode,
SubstractionNode,
VariableNode,
literal,
param,
Expand Down
12 changes: 12 additions & 0 deletions src/andromede/expression/context_adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ComponentVariableNode,
ExpressionNode,
ParameterNode,
ProblemParameterNode,
ProblemVariableNode,
VariableNode,
)
from .visitor import visit
Expand Down Expand Up @@ -48,6 +50,16 @@ def comp_parameter(self, node: ComponentParameterNode) -> ExpressionNode:
"This expression has already been associated to another component."
)

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

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


def add_component_context(id: str, expression: ExpressionNode) -> ExpressionNode:
return visit(expression, ContextAdder(id))
66 changes: 28 additions & 38 deletions src/andromede/expression/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@
# 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,
AllTimeSumNode,
ComparisonNode,
ComponentParameterNode,
ComponentVariableNode,
DivisionNode,
ExpressionNode,
ExpressionRange,
InstancesTimeIndex,
LiteralNode,
MultiplicationNode,
NegationNode,
ParameterNode,
PortFieldAggregatorNode,
PortFieldNode,
ProblemParameterNode,
ProblemVariableNode,
ScenarioOperatorNode,
SubstractionNode,
TimeAggregatorNode,
TimeOperatorNode,
TimeEvalNode,
TimeShiftNode,
TimeSumNode,
VariableNode,
)
from .visitor import ExpressionVisitor, ExpressionVisitorOperations, T, visit
from .visitor import ExpressionVisitorOperations, visit


@dataclass(frozen=True)
Expand Down Expand Up @@ -63,38 +60,31 @@ def comp_variable(self, node: ComponentVariableNode) -> ExpressionNode:
def comp_parameter(self, node: ComponentParameterNode) -> ExpressionNode:
return ComponentParameterNode(node.component_id, node.name)

def copy_expression_range(
self, expression_range: ExpressionRange
) -> ExpressionRange:
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,
def pb_variable(self, node: ProblemVariableNode) -> ExpressionNode:
return ProblemVariableNode(
node.component_id, node.name, node.time_index, node.scenario_index
)

def copy_instances_index(
self, instances_index: InstancesTimeIndex
) -> InstancesTimeIndex:
expressions = instances_index.expressions
if isinstance(expressions, ExpressionRange):
return InstancesTimeIndex(self.copy_expression_range(expressions))
if isinstance(expressions, list):
expressions_list = cast(List[ExpressionNode], expressions)
copy = [visit(e, self) for e in expressions_list]
return InstancesTimeIndex(copy)
raise ValueError("Unexpected type in instances index")

def time_operator(self, node: TimeOperatorNode) -> ExpressionNode:
return TimeOperatorNode(
def pb_parameter(self, node: ProblemParameterNode) -> ExpressionNode:
return ProblemParameterNode(
node.component_id, node.name, node.time_index, node.scenario_index
)

def time_shift(self, node: TimeShiftNode) -> ExpressionNode:
return TimeShiftNode(visit(node.operand, self), visit(node.time_shift, self))

def time_eval(self, node: TimeEvalNode) -> ExpressionNode:
return TimeShiftNode(visit(node.operand, self), visit(node.eval_time, self))

def time_sum(self, node: TimeSumNode) -> ExpressionNode:
return TimeSumNode(
visit(node.operand, self),
node.name,
self.copy_instances_index(node.instances_index),
visit(node.from_time, self),
visit(node.to_time, self),
)

def time_aggregator(self, node: TimeAggregatorNode) -> ExpressionNode:
return TimeAggregatorNode(visit(node.operand, self), node.name, node.stay_roll)
def all_time_sum(self, node: AllTimeSumNode) -> ExpressionNode:
return AllTimeSumNode(visit(node.operand, self))

def scenario_operator(self, node: ScenarioOperatorNode) -> ExpressionNode:
return ScenarioOperatorNode(visit(node.operand, self), node.name)
Expand Down
43 changes: 25 additions & 18 deletions src/andromede/expression/degree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

import andromede.expression.scenario_operator
from andromede.expression.expression import (
AllTimeSumNode,
ComponentParameterNode,
ComponentVariableNode,
PortFieldAggregatorNode,
PortFieldNode,
TimeOperatorNode,
ProblemParameterNode,
ProblemVariableNode,
TimeEvalNode,
TimeShiftNode,
TimeSumNode,
)

from .expression import (
Expand All @@ -29,8 +34,6 @@
NegationNode,
ParameterNode,
ScenarioOperatorNode,
SubstractionNode,
TimeAggregatorNode,
VariableNode,
)
from .visitor import ExpressionVisitor, T, visit
Expand All @@ -49,10 +52,8 @@ def negation(self, node: NegationNode) -> int:

# TODO: Take into account simplification that can occur with literal coefficient for add, sub, mult, div
def addition(self, node: AdditionNode) -> int:
return max(visit(node.left, self), visit(node.right, self))

def substraction(self, node: SubstractionNode) -> int:
return max(visit(node.left, self), visit(node.right, self))
degrees = [visit(o, self) for o in node.operands]
return max(degrees)

def multiplication(self, node: MultiplicationNode) -> int:
return visit(node.left, self) + visit(node.right, self)
Expand All @@ -78,17 +79,23 @@ def comp_variable(self, node: ComponentVariableNode) -> int:
def comp_parameter(self, node: ComponentParameterNode) -> int:
return 0

def time_operator(self, node: TimeOperatorNode) -> int:
if node.name in ["TimeShift", "TimeEvaluation"]:
return visit(node.operand, self)
else:
return NotImplemented

def time_aggregator(self, node: TimeAggregatorNode) -> int:
if node.name in ["TimeSum"]:
return visit(node.operand, self)
else:
return NotImplemented
def pb_variable(self, node: ProblemVariableNode) -> int:
return 1

def pb_parameter(self, node: ProblemParameterNode) -> int:
return 0

def time_shift(self, node: TimeShiftNode) -> int:
return visit(node.operand, self)

def time_eval(self, node: TimeEvalNode) -> int:
return visit(node.operand, self)

def time_sum(self, node: TimeSumNode) -> int:
return visit(node.operand, self)

def all_time_sum(self, node: AllTimeSumNode) -> int:
return visit(node.operand, self)

def scenario_operator(self, node: ScenarioOperatorNode) -> int:
scenario_operator_cls = getattr(
Expand Down
Loading

0 comments on commit b914bc1

Please sign in to comment.