From d7a60cce2b52c13164d03953ed490dc53a7f5201 Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Thu, 20 Nov 2025 12:46:06 -0500 Subject: [PATCH 1/3] Fix ty warnings --- simopt/input_models.py | 6 +++--- simopt/models/amusementpark.py | 3 ++- simopt/models/chessmm.py | 4 ++-- simopt/models/cntnv.py | 4 ++-- simopt/models/dualsourcing.py | 4 ++-- simopt/models/dynamnews.py | 4 ++-- simopt/models/facilitysizing.py | 4 ++-- simopt/models/ironore.py | 4 ++-- simopt/models/network.py | 4 ++-- simopt/models/tableallocation.py | 11 +++++++---- 10 files changed, 26 insertions(+), 22 deletions(-) diff --git a/simopt/input_models.py b/simopt/input_models.py index b640f751b..066f2adcd 100644 --- a/simopt/input_models.py +++ b/simopt/input_models.py @@ -3,9 +3,9 @@ import bisect import itertools import math -import random from abc import abstractmethod from collections.abc import Sequence +from random import Random from typing import ParamSpec, Protocol, TypeVar P = ParamSpec("P") @@ -15,9 +15,9 @@ class InputModel(Protocol[P, R]): """Abstract base for input models used by simulations.""" - rng: random.Random | None = None + rng: Random | None = None - def set_rng(self, rng: random.Random) -> None: + def set_rng(self, rng: Random) -> None: """Attach a Python RNG to the input model. Args: diff --git a/simopt/models/amusementpark.py b/simopt/models/amusementpark.py index f27180373..1edc85031 100644 --- a/simopt/models/amusementpark.py +++ b/simopt/models/amusementpark.py @@ -478,9 +478,10 @@ def set_completion(i: int, new_time: float) -> None: cumulative_util = [cumulative_util[i] / time_open for i in attraction_range] # Calculate responses from simulation data. + percent_departed = total_departed / total_visitors if total_visitors else 0 responses = { "total_departed": total_departed, - "percent_departed": total_departed / total_visitors, + "percent_departed": percent_departed, "average_number_in_system": time_average / time_open, "attraction_utilization_percentages": cumulative_util, } diff --git a/simopt/models/chessmm.py b/simopt/models/chessmm.py index a08f55138..faadc3bd9 100644 --- a/simopt/models/chessmm.py +++ b/simopt/models/chessmm.py @@ -2,7 +2,7 @@ from __future__ import annotations -import random +from random import Random from typing import Annotated, ClassVar, Final import numpy as np @@ -111,7 +111,7 @@ class ChessAvgDifferenceConfig(BaseModel): class EloInputModel(InputModel): """Input model for player Elo ratings.""" - rng: random.Random | None = None + rng: Random | None = None def random( self, mean: float, std: float, min_rating: float, max_rating: float diff --git a/simopt/models/cntnv.py b/simopt/models/cntnv.py index 4f17a519c..898193139 100644 --- a/simopt/models/cntnv.py +++ b/simopt/models/cntnv.py @@ -2,7 +2,7 @@ from __future__ import annotations -import random +from random import Random from typing import Annotated, ClassVar, Self import numpy as np @@ -128,7 +128,7 @@ class CntNVMaxProfitConfig(BaseModel): class DemandInputModel(InputModel): """Input model for Burr Type XII demand.""" - rng: random.Random | None = None + rng: Random | None = None def random(self, burr_c: float, burr_k: float) -> float: # noqa: D102 # Generate random demand according to Burr Type XII distribution. diff --git a/simopt/models/dualsourcing.py b/simopt/models/dualsourcing.py index 5e4e14010..784e8e1f6 100644 --- a/simopt/models/dualsourcing.py +++ b/simopt/models/dualsourcing.py @@ -2,7 +2,7 @@ from __future__ import annotations -import random +from random import Random from typing import Annotated, ClassVar, Self import numpy as np @@ -166,7 +166,7 @@ class DualSourcingMinCostConfig(BaseModel): class DemandInputModel(InputModel): """Input model for daily demand.""" - rng: random.Random | None = None + rng: Random | None = None def random(self, mu: float, sigma: float) -> int: # noqa: D102 def round_and_clamp_non_neg(x: float | int) -> int: diff --git a/simopt/models/dynamnews.py b/simopt/models/dynamnews.py index b6612204e..ccedf04dc 100644 --- a/simopt/models/dynamnews.py +++ b/simopt/models/dynamnews.py @@ -3,7 +3,7 @@ from __future__ import annotations import math -import random +from random import Random from typing import Annotated, ClassVar, Final, Self import numpy as np @@ -155,7 +155,7 @@ class DynamNewsMaxProfitConfig(BaseModel): class Utility(InputModel): """Input model for customer utility sampling.""" - rng: random.Random | None = None + rng: Random | None = None def _gumbelvariate(self, mu: float, beta: float) -> float: assert self.rng is not None diff --git a/simopt/models/facilitysizing.py b/simopt/models/facilitysizing.py index 276245b57..488349213 100644 --- a/simopt/models/facilitysizing.py +++ b/simopt/models/facilitysizing.py @@ -2,7 +2,7 @@ from __future__ import annotations -import random +from random import Random from typing import Annotated, ClassVar, Final, Self import numpy as np @@ -204,7 +204,7 @@ def _validate_problem(self) -> Self: class DemandInputModel(InputModel): """Input model for multivariate normal demand at facilities.""" - rng: random.Random | None = None + rng: Random | None = None def _mvnormalvariate( self, diff --git a/simopt/models/ironore.py b/simopt/models/ironore.py index 3a73b8cd7..8fd0d141b 100644 --- a/simopt/models/ironore.py +++ b/simopt/models/ironore.py @@ -6,8 +6,8 @@ from __future__ import annotations -import random from math import copysign, sqrt +from random import Random from typing import Annotated, ClassVar, Self import numpy as np @@ -202,7 +202,7 @@ class IronOreMaxRevConfig(BaseModel): class MovementInputModel(InputModel): """Input model for mining movement and price shocks.""" - rng: random.Random | None = None + rng: Random | None = None def random(self, mean: float, std: float) -> float: # noqa: D102 assert self.rng is not None diff --git a/simopt/models/network.py b/simopt/models/network.py index 4d56a24f6..25425cfba 100644 --- a/simopt/models/network.py +++ b/simopt/models/network.py @@ -2,8 +2,8 @@ from __future__ import annotations -import random from enum import IntEnum +from random import Random from typing import Annotated, ClassVar, Final, Self import numpy as np @@ -215,7 +215,7 @@ class NetworkMinTotalCostConfig(BaseModel): class RouteInputModel(InputModel): """Input model for routing choices in the network.""" - rng: random.Random | None = None + rng: Random | None = None def random(self, choices: list[int], weights: list[float], k: int) -> list[int]: # noqa: D102 assert self.rng is not None diff --git a/simopt/models/tableallocation.py b/simopt/models/tableallocation.py index 5d6fb5c1f..7f9c6336d 100644 --- a/simopt/models/tableallocation.py +++ b/simopt/models/tableallocation.py @@ -5,7 +5,7 @@ import bisect import itertools from collections.abc import Sequence -from typing import Annotated, ClassVar, Self +from typing import Annotated, ClassVar, Self, cast import numpy as np from pydantic import BaseModel, Field, model_validator @@ -256,9 +256,12 @@ def fast_weighted_choice( # Pass through all arrivals of groups to the restaurants. for n in range(n_arrivals): # Determine group size. - group_size = self.group_size_model.random( - population=group_size_options, - weights=f_lambda, + group_size = cast( + int, + self.group_size_model.random( + population=group_size_options, + weights=f_lambda, + ), ) # Find smallest table size to start search. From 485a73b5fa6c01acbc99d1c0d29e6b8e203e09d0 Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Thu, 20 Nov 2025 12:58:39 -0500 Subject: [PATCH 2/3] Fix pyrefly warning --- simopt/models/hotel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simopt/models/hotel.py b/simopt/models/hotel.py index 6f0fda6d2..69150382a 100644 --- a/simopt/models/hotel.py +++ b/simopt/models/hotel.py @@ -307,7 +307,7 @@ def replicate(self) -> tuple[dict, dict]: total_revenue = 0 # Generate interarrival times - arr_bound = 10 * round(168 * np.sum(f_lambda)) + arr_bound = 10 * round(168 * sum(f_lambda)) arr_time = np.array( [ [self.arrival_model.random(f_lambda[i]) for _ in range(arr_bound)] From a52144692ca79e82ea69a8196a14c970b5fc2e4d Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Thu, 20 Nov 2025 12:43:39 -0500 Subject: [PATCH 3/3] Remove excluded files in `simopt/models` --- ty.toml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ty.toml b/ty.toml index 44f2c20f3..b3261959c 100644 --- a/ty.toml +++ b/ty.toml @@ -19,15 +19,6 @@ exclude = [ "simopt/gui/new_experiment_window.py", "simopt/gui/plot_window.py", "simopt/model.py", - "simopt/models/amusementpark.py", - "simopt/models/chessmm.py", - "simopt/models/cntnv.py", - "simopt/models/dualsourcing.py", - "simopt/models/dynamnews.py", - "simopt/models/facilitysizing.py", - "simopt/models/ironore.py", - "simopt/models/network.py", - "simopt/models/tableallocation.py", "simopt/plot_type.py", "simopt/plots", "simopt/problem.py",