|
| 1 | +# ruff: noqa |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | +from types import ModuleType, SimpleNamespace |
| 7 | + |
| 8 | + |
| 9 | +class _GenericAliasMixin: |
| 10 | + @classmethod |
| 11 | + def __class_getitem__(cls, _item: object) -> type[_GenericAliasMixin]: |
| 12 | + return cls |
| 13 | + |
| 14 | + |
| 15 | +class _Constraint(_GenericAliasMixin): |
| 16 | + def OnlyEnforceIf(self, *_args: object, **_kwargs: object) -> _Constraint: |
| 17 | + return self |
| 18 | + |
| 19 | + |
| 20 | +class _IntVar(_GenericAliasMixin): |
| 21 | + def Proto(self) -> SimpleNamespace: |
| 22 | + return SimpleNamespace(domain=[]) |
| 23 | + |
| 24 | + |
| 25 | +class _LinearExpr(_GenericAliasMixin): |
| 26 | + @staticmethod |
| 27 | + def WeightedSum(*_args: object, **_kwargs: object) -> int: |
| 28 | + return 0 |
| 29 | + |
| 30 | + |
| 31 | +class _Domain(_GenericAliasMixin): |
| 32 | + def __init__(self, *_args: object, **_kwargs: object) -> None: |
| 33 | + self._intervals: list[int] = [] |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def FromValues(cls, values: list[int]) -> _Domain: |
| 37 | + domain = cls() |
| 38 | + domain._intervals = list(values) |
| 39 | + return domain |
| 40 | + |
| 41 | + def FlattenedIntervals(self) -> list[int]: |
| 42 | + return list(self._intervals) |
| 43 | + |
| 44 | + |
| 45 | +class _PBResult: |
| 46 | + clauses: list[list[int]] = [] |
| 47 | + |
| 48 | + |
| 49 | +def _ensure_module(name: str) -> ModuleType: |
| 50 | + module = sys.modules.get(name) |
| 51 | + if module is None: |
| 52 | + module = ModuleType(name) |
| 53 | + sys.modules[name] = module |
| 54 | + return module |
| 55 | + |
| 56 | + |
| 57 | +def _install_gurobi_stub() -> None: |
| 58 | + try: |
| 59 | + import gurobipy # noqa: F401 |
| 60 | + except Exception: |
| 61 | + module = _ensure_module("gurobipy") |
| 62 | + |
| 63 | + class Env(_GenericAliasMixin): |
| 64 | + pass |
| 65 | + |
| 66 | + class Var(_GenericAliasMixin): |
| 67 | + pass |
| 68 | + |
| 69 | + class Constr(_GenericAliasMixin): |
| 70 | + pass |
| 71 | + |
| 72 | + class LinExpr(_GenericAliasMixin): |
| 73 | + pass |
| 74 | + |
| 75 | + class QuadExpr(_GenericAliasMixin): |
| 76 | + pass |
| 77 | + |
| 78 | + class MVar(_GenericAliasMixin): |
| 79 | + pass |
| 80 | + |
| 81 | + class MLinExpr(_GenericAliasMixin): |
| 82 | + @staticmethod |
| 83 | + def zeros(*_args: object, **_kwargs: object) -> int: |
| 84 | + return 0 |
| 85 | + |
| 86 | + class tupledict(dict, _GenericAliasMixin): |
| 87 | + pass |
| 88 | + |
| 89 | + class Model(_GenericAliasMixin): |
| 90 | + def __init__(self, *_args: object, **_kwargs: object) -> None: |
| 91 | + pass |
| 92 | + |
| 93 | + class _Callback: |
| 94 | + MIPSOL = 1 |
| 95 | + MIPSOL_OBJ = 2 |
| 96 | + |
| 97 | + class GRB: |
| 98 | + BINARY = "B" |
| 99 | + CONTINUOUS = "C" |
| 100 | + INFINITY = float("inf") |
| 101 | + MINIMIZE = 1 |
| 102 | + Callback = _Callback |
| 103 | + |
| 104 | + module.Env = Env |
| 105 | + module.Var = Var |
| 106 | + module.Constr = Constr |
| 107 | + module.LinExpr = LinExpr |
| 108 | + module.QuadExpr = QuadExpr |
| 109 | + module.MVar = MVar |
| 110 | + module.MLinExpr = MLinExpr |
| 111 | + module.Model = Model |
| 112 | + module.tupledict = tupledict |
| 113 | + module.GRB = GRB |
| 114 | + |
| 115 | + |
| 116 | +def _install_ortools_stub() -> None: |
| 117 | + try: |
| 118 | + from ortools.sat.python import cp_model |
| 119 | + except Exception: |
| 120 | + ortools = _ensure_module("ortools") |
| 121 | + sat = _ensure_module("ortools.sat") |
| 122 | + python = _ensure_module("ortools.sat.python") |
| 123 | + cp_model = _ensure_module("ortools.sat.python.cp_model") |
| 124 | + |
| 125 | + class CpModel(_GenericAliasMixin): |
| 126 | + def Add(self, *_args: object, **_kwargs: object) -> _Constraint: |
| 127 | + return _Constraint() |
| 128 | + |
| 129 | + def AddAbsEquality( |
| 130 | + self, |
| 131 | + *_args: object, |
| 132 | + **_kwargs: object, |
| 133 | + ) -> _Constraint: |
| 134 | + return _Constraint() |
| 135 | + |
| 136 | + def AddElement( |
| 137 | + self, |
| 138 | + *_args: object, |
| 139 | + **_kwargs: object, |
| 140 | + ) -> _Constraint: |
| 141 | + return _Constraint() |
| 142 | + |
| 143 | + def AddExactlyOne( |
| 144 | + self, |
| 145 | + *_args: object, |
| 146 | + **_kwargs: object, |
| 147 | + ) -> _Constraint: |
| 148 | + return _Constraint() |
| 149 | + |
| 150 | + def Minimize(self, *_args: object, **_kwargs: object) -> None: |
| 151 | + return None |
| 152 | + |
| 153 | + def NewBoolVar(self, *_args: object, **_kwargs: object) -> _IntVar: |
| 154 | + return _IntVar() |
| 155 | + |
| 156 | + def NewIntVar(self, *_args: object, **_kwargs: object) -> _IntVar: |
| 157 | + return _IntVar() |
| 158 | + |
| 159 | + def NewIntVarFromDomain( |
| 160 | + self, |
| 161 | + *_args: object, |
| 162 | + **_kwargs: object, |
| 163 | + ) -> _IntVar: |
| 164 | + return _IntVar() |
| 165 | + |
| 166 | + class CpSolver(_GenericAliasMixin): |
| 167 | + def __init__(self) -> None: |
| 168 | + self.parameters = SimpleNamespace( |
| 169 | + log_search_progress=False, |
| 170 | + max_time_in_seconds=0, |
| 171 | + random_seed=0, |
| 172 | + num_workers=0, |
| 173 | + ) |
| 174 | + |
| 175 | + def BestObjectiveBound(self) -> int: |
| 176 | + return 0 |
| 177 | + |
| 178 | + def ObjectiveValue(self) -> int: |
| 179 | + return 0 |
| 180 | + |
| 181 | + def Solve(self, *_args: object, **_kwargs: object) -> int: |
| 182 | + return 0 |
| 183 | + |
| 184 | + def Value(self, *_args: object, **_kwargs: object) -> int: |
| 185 | + return 0 |
| 186 | + |
| 187 | + def status_name(self) -> str: |
| 188 | + return "UNKNOWN" |
| 189 | + |
| 190 | + class CpSolverSolutionCallback(_GenericAliasMixin): |
| 191 | + def __init__(self) -> None: |
| 192 | + pass |
| 193 | + |
| 194 | + def ObjectiveValue(self) -> int: |
| 195 | + return 0 |
| 196 | + |
| 197 | + cp_model.Constraint = _Constraint |
| 198 | + cp_model.CpModel = CpModel |
| 199 | + cp_model.CpSolver = CpSolver |
| 200 | + cp_model.CpSolverSolutionCallback = CpSolverSolutionCallback |
| 201 | + cp_model.Domain = _Domain |
| 202 | + cp_model.IntVar = _IntVar |
| 203 | + cp_model.LinearExpr = _LinearExpr |
| 204 | + cp_model.ObjLinearExprT = int |
| 205 | + |
| 206 | + ortools.sat = sat |
| 207 | + sat.python = python |
| 208 | + python.cp_model = cp_model |
| 209 | + |
| 210 | + |
| 211 | +def _install_pysat_stub() -> None: |
| 212 | + try: |
| 213 | + import pysat.examples.rc2 |
| 214 | + import pysat.formula |
| 215 | + import pysat.pb |
| 216 | + except Exception: |
| 217 | + pysat = _ensure_module("pysat") |
| 218 | + formula = _ensure_module("pysat.formula") |
| 219 | + pb = _ensure_module("pysat.pb") |
| 220 | + examples = _ensure_module("pysat.examples") |
| 221 | + rc2 = _ensure_module("pysat.examples.rc2") |
| 222 | + |
| 223 | + class WCNF(_GenericAliasMixin): |
| 224 | + def __init__(self) -> None: |
| 225 | + self.hard: list[list[int]] = [] |
| 226 | + self.soft: list[list[int]] = [] |
| 227 | + self.wght: list[int] = [] |
| 228 | + self.topw = 1 |
| 229 | + |
| 230 | + def append( |
| 231 | + self, |
| 232 | + lits: list[int], |
| 233 | + weight: int | None = None, |
| 234 | + ) -> None: |
| 235 | + if weight is None: |
| 236 | + self.hard.append(list(lits)) |
| 237 | + else: |
| 238 | + self.soft.append(list(lits)) |
| 239 | + self.wght.append(weight) |
| 240 | + |
| 241 | + class IDPool(_GenericAliasMixin): |
| 242 | + def __init__(self) -> None: |
| 243 | + self.obj2id: dict[str, int] = {} |
| 244 | + |
| 245 | + def id(self, name: str) -> int: |
| 246 | + if name not in self.obj2id: |
| 247 | + self.obj2id[name] = len(self.obj2id) + 1 |
| 248 | + return self.obj2id[name] |
| 249 | + |
| 250 | + class PBEnc: |
| 251 | + @staticmethod |
| 252 | + def atleast(*_args: object, **_kwargs: object) -> _PBResult: |
| 253 | + return _PBResult() |
| 254 | + |
| 255 | + class RC2(_GenericAliasMixin): |
| 256 | + def __init__(self, *_args: object, **_kwargs: object) -> None: |
| 257 | + self.cost = 0 |
| 258 | + |
| 259 | + def __enter__(self) -> RC2: |
| 260 | + return self |
| 261 | + |
| 262 | + def __exit__( |
| 263 | + self, |
| 264 | + *_args: object, |
| 265 | + **_kwargs: object, |
| 266 | + ) -> bool: |
| 267 | + return False |
| 268 | + |
| 269 | + def compute(self) -> list[int]: |
| 270 | + return [] |
| 271 | + |
| 272 | + formula.WCNF = WCNF |
| 273 | + formula.IDPool = IDPool |
| 274 | + pb.PBEnc = PBEnc |
| 275 | + rc2.RC2 = RC2 |
| 276 | + |
| 277 | + pysat.formula = formula |
| 278 | + pysat.pb = pb |
| 279 | + pysat.examples = examples |
| 280 | + examples.rc2 = rc2 |
| 281 | + |
| 282 | + |
| 283 | +def install() -> None: |
| 284 | + _install_gurobi_stub() |
| 285 | + _install_ortools_stub() |
| 286 | + _install_pysat_stub() |
0 commit comments