From f48f710df507a5feea25d36751c38ed4be4dbc63 Mon Sep 17 00:00:00 2001 From: Ian Menezes Date: Mon, 22 Jul 2024 17:05:20 +0200 Subject: [PATCH] Added check for upper-lower variable bounds --- src/andromede/simulation/optimization.py | 8 +++++++- tests/functional/test_andromede.py | 8 ++++++++ tests/functional/test_performance.py | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/andromede/simulation/optimization.py b/src/andromede/simulation/optimization.py index a4c642c7..43d121f5 100644 --- a/src/andromede/simulation/optimization.py +++ b/src/andromede/simulation/optimization.py @@ -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 @@ -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, diff --git a/tests/functional/test_andromede.py b/tests/functional/test_andromede.py index 6e87b01a..a0abbf20 100644 --- a/tests/functional/test_andromede.py +++ b/tests/functional/test_andromede.py @@ -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 diff --git a/tests/functional/test_performance.py b/tests/functional/test_performance.py index d8b2321c..1c50af1c 100644 --- a/tests/functional/test_performance.py +++ b/tests/functional/test_performance.py @@ -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), ), ],