Skip to content

Commit

Permalink
Added check for upper-lower variable bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmnz committed Jul 22, 2024
1 parent daa62b9 commit f48f710
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/andromede/simulation/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
into a mathematical optimization problem.
"""

import math
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Dict, Iterable, List, Optional, Type
from typing import Dict, Iterable, List, Optional

import ortools.linear_solver.pywraplp as lp

Expand Down Expand Up @@ -769,6 +770,11 @@ def _create_variables(self) -> None:
solver_var = None
solver_var_name = f"{component_prefix}{var_name}{block_suffix}{scenario_suffix}"

if math.isclose(lower_bound, upper_bound):
raise ValueError(
f"Upper and lower bounds of variable {solver_var_name} have the same value: {lower_bound}"
)

if model_var.data_type == ValueType.BOOL:
solver_var = self.solver.BoolVar(
solver_var_name,
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/test_andromede.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ def test_variable_bound() -> None:
status = problem.solver.Solve()
assert status == problem.solver.INFEASIBLE # Infeasible

network = create_one_node_network(generator_model)
database = create_simple_database(max_generation=0) # Equal upper and lower bounds
with pytest.raises(
ValueError,
match="Upper and lower bounds of variable G_generation have the same value: 0",
):
problem = build_problem(network, database, TimeBlock(1, [0]), 1)


def generate_data(
efficiency: float, horizon: int, scenarios: int
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_large_sum_inside_model_with_sum_operator() -> None:
float_variable(
"var",
lower_bound=literal(1),
upper_bound=literal(1),
upper_bound=literal(2),
structure=IndexingStructure(True, False),
),
],
Expand Down

0 comments on commit f48f710

Please sign in to comment.