From d3c5371fa5928a36bdda163211890d6a593e4f92 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Tue, 2 Feb 2021 09:10:49 -0600 Subject: [PATCH] VarIndex: Use tuples instead of lists --- miplearn/solvers/gurobi.py | 9 ++++++--- miplearn/types.py | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/miplearn/solvers/gurobi.py b/miplearn/solvers/gurobi.py index 665ed91..2cced0f 100644 --- a/miplearn/solvers/gurobi.py +++ b/miplearn/solvers/gurobi.py @@ -6,7 +6,7 @@ import re import sys from io import StringIO from random import randint -from typing import List, Any, Dict, Optional +from typing import List, Any, Dict, Optional, cast, Tuple, Union from miplearn.instance import Instance from miplearn.solvers import _RedirectOutput @@ -92,11 +92,14 @@ class GurobiSolver(InternalSolver): m = re.search(r"([^[]*)\[(.*)]", var.varName) if m is None: name = var.varName - idx = [0] + idx = (0,) else: name = m.group(1) parts = m.group(2).split(",") - idx = tuple(int(k) if k.isdecimal() else k for k in parts) + idx = cast( + Tuple[Union[str, int]], + tuple(int(k) if k.isdecimal() else str(k) for k in parts), + ) if len(idx) == 1: idx = idx[0] if name not in self._all_vars: diff --git a/miplearn/types.py b/miplearn/types.py index 5cb98e9..b89e649 100644 --- a/miplearn/types.py +++ b/miplearn/types.py @@ -2,11 +2,11 @@ # Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. # Released under the modified BSD license. See COPYING.md for more details. -from typing import Optional, Dict, Callable, Any, Union, List +from typing import Optional, Dict, Callable, Any, Union, Tuple from mypy_extensions import TypedDict -VarIndex = Union[str, int, List[Union[str, int]]] +VarIndex = Union[str, int, Tuple[Union[str, int]]] Solution = Dict[str, Dict[VarIndex, Optional[float]]]