simoptlib 1.2.4__py3-none-any.whl → 1.3.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. simopt/__main__.py +1 -7
  2. simopt/_markers.py +15 -0
  3. simopt/analysis/__init__.py +1 -0
  4. simopt/analysis/auc.py +233 -0
  5. simopt/analysis/bootstrap.py +300 -0
  6. simopt/analysis/common.py +375 -0
  7. simopt/analysis/deterministic_feasibility_progress.py +190 -0
  8. simopt/analysis/deterministic_terminal_feasibility.py +258 -0
  9. simopt/analysis/diff.py +266 -0
  10. simopt/analysis/feasibility_progress.py +264 -0
  11. simopt/analysis/merit.py +118 -0
  12. simopt/analysis/progress_curve.py +229 -0
  13. simopt/analysis/solvability.py +157 -0
  14. simopt/analysis/solvability_cdf.py +193 -0
  15. simopt/analysis/solvability_profile.py +354 -0
  16. simopt/analysis/terminal_feasibility.py +333 -0
  17. simopt/analysis/terminal_progress.py +124 -0
  18. simopt/analysis/terminal_scatterplot.py +133 -0
  19. simopt/cli.py +59 -0
  20. simopt/data_farming/data_farming_core.py +1 -2
  21. simopt/{experiment_base.py → data_farming/design.py} +111 -174
  22. simopt/data_farming/nolhs.py +1 -2
  23. simopt/data_farming_base.py +13 -25
  24. simopt/deterministic_feasibility.py +37 -0
  25. simopt/directory.py +11 -21
  26. simopt/dsl.py +15 -0
  27. simopt/experiment/__init__.py +13 -4
  28. simopt/experiment/api.py +466 -0
  29. simopt/experiment/data.py +35 -0
  30. simopt/experiment/experiment.py +146 -0
  31. simopt/experiment/plot.py +278 -0
  32. simopt/experiment/post_normalize.py +243 -269
  33. simopt/experiment/post_normalize_merit.py +175 -0
  34. simopt/experiment/post_replicate.py +207 -0
  35. simopt/experiment/run_solver.py +9 -48
  36. simopt/feasibility.py +1 -3
  37. simopt/input_models.py +45 -45
  38. simopt/logging.py +28 -0
  39. simopt/model.py +12 -20
  40. simopt/models/ambulance.py +26 -328
  41. simopt/models/amusementpark.py +58 -426
  42. simopt/models/chessmm.py +33 -166
  43. simopt/models/cntnv.py +27 -180
  44. simopt/models/contam.py +88 -248
  45. simopt/models/dualsourcing.py +37 -244
  46. simopt/models/dynamnews.py +32 -229
  47. simopt/models/ermexample.py +20 -81
  48. simopt/models/example.py +46 -105
  49. simopt/models/facilitysizing.py +51 -185
  50. simopt/models/fixedsan.py +39 -159
  51. simopt/models/hotel.py +29 -316
  52. simopt/models/ironore.py +55 -290
  53. simopt/models/mm1queue.py +33 -196
  54. simopt/models/network.py +31 -312
  55. simopt/models/paramesti.py +27 -98
  56. simopt/models/rmitd.py +48 -222
  57. simopt/models/san.py +307 -260
  58. simopt/models/sscont.py +42 -235
  59. simopt/models/tableallocation.py +43 -246
  60. simopt/options.py +39 -0
  61. simopt/plot_type.py +3 -2
  62. simopt/problem.py +112 -471
  63. simopt/simulations/__init__.py +1 -0
  64. simopt/simulations/ambulance.py +206 -0
  65. simopt/simulations/amusementpark.py +353 -0
  66. simopt/simulations/chessmm.py +158 -0
  67. simopt/simulations/cntnv.py +156 -0
  68. simopt/simulations/contam.py +158 -0
  69. simopt/simulations/dualsourcing.py +217 -0
  70. simopt/simulations/dynamnews.py +208 -0
  71. simopt/simulations/ermexample.py +61 -0
  72. simopt/simulations/example.py +69 -0
  73. simopt/simulations/facilitysizing.py +132 -0
  74. simopt/simulations/fixedsan.py +130 -0
  75. simopt/simulations/hotel.py +281 -0
  76. simopt/simulations/ironore.py +240 -0
  77. simopt/simulations/mm1queue.py +195 -0
  78. simopt/simulations/network.py +265 -0
  79. simopt/simulations/paramesti.py +80 -0
  80. simopt/simulations/rmitd.py +181 -0
  81. simopt/simulations/san.py +164 -0
  82. simopt/simulations/sscont.py +218 -0
  83. simopt/simulations/tableallocation.py +206 -0
  84. simopt/test.py +150 -0
  85. simopt/utils.py +179 -259
  86. simopt/web/__init__.py +1 -0
  87. simopt/web/dist/assets/index-BsdjTlAU.js +14 -0
  88. simopt/web/dist/assets/index-CdXZ_qpx.css +1 -0
  89. simopt/web/dist/index.html +2 -2
  90. simopt/web/plots.py +572 -0
  91. simopt/web/server.py +1811 -0
  92. simopt-dsl/src/simopt_dsl/__init__.py +8 -0
  93. simopt-dsl/src/simopt_dsl/expressions.py +273 -0
  94. simopt-dsl/src/simopt_dsl/model.py +723 -0
  95. simopt-dsl/src/simopt_dsl/py.typed +0 -0
  96. simopt-dsl/src/simopt_dsl/simulation.py +218 -0
  97. simopt-dsl/src/simopt_dsl/variables.py +77 -0
  98. simopt-dsl/tests/test_model.py +184 -0
  99. simopt-solvers/src/simopt_solvers/__init__.py +75 -0
  100. simopt-solvers/src/simopt_solvers/_utils.py +75 -0
  101. simopt-solvers/src/simopt_solvers/base.py +9 -0
  102. simopt-solvers/src/simopt_solvers/py.typed +0 -0
  103. simopt-solvers/src/simopt_solvers/solution.py +282 -0
  104. simopt-solvers/src/simopt_solvers/solver.py +315 -0
  105. simopt-solvers/src/simopt_solvers/solvers/__init__.py +1 -0
  106. {simopt → simopt-solvers/src/simopt_solvers}/solvers/_dasso.py +8 -5
  107. {simopt → simopt-solvers/src/simopt_solvers}/solvers/adam.py +34 -42
  108. {simopt → simopt-solvers/src/simopt_solvers}/solvers/aloe.py +47 -44
  109. {simopt → simopt-solvers/src/simopt_solvers}/solvers/astrodf.py +251 -272
  110. simopt-solvers/src/simopt_solvers/solvers/castrodf.py +2080 -0
  111. {simopt → simopt-solvers/src/simopt_solvers}/solvers/dasso.py +490 -528
  112. {simopt → simopt-solvers/src/simopt_solvers}/solvers/fcsa.py +47 -61
  113. {simopt → simopt-solvers/src/simopt_solvers}/solvers/neldmd.py +53 -132
  114. {simopt → simopt-solvers/src/simopt_solvers}/solvers/randomsearch.py +15 -21
  115. {simopt → simopt-solvers/src/simopt_solvers}/solvers/spsa.py +34 -48
  116. {simopt → simopt-solvers/src/simopt_solvers}/solvers/strong.py +77 -131
  117. {simopt → simopt-solvers/src/simopt_solvers}/solvers/utils.py +115 -53
  118. simopt-solvers/tests/test_solve.py +86 -0
  119. simopt-solvers/tests/test_specifications.py +55 -0
  120. simopt-web/node_modules/flatted/python/flatted.py +144 -0
  121. {simoptlib-1.2.4.dist-info → simoptlib-1.3.0.dev0.dist-info}/METADATA +39 -121
  122. simoptlib-1.3.0.dev0.dist-info/RECORD +134 -0
  123. {simoptlib-1.2.4.dist-info → simoptlib-1.3.0.dev0.dist-info}/WHEEL +1 -1
  124. simoptlib-1.3.0.dev0.dist-info/entry_points.txt +2 -0
  125. simoptlib-1.3.0.dev0.dist-info/top_level.txt +4 -0
  126. simopt/GUI.py +0 -50
  127. simopt/base.py +0 -17
  128. simopt/bootstrap.py +0 -437
  129. simopt/curve.py +0 -231
  130. simopt/curve_utils.py +0 -242
  131. simopt/experiment/multiple.py +0 -815
  132. simopt/experiment/single.py +0 -878
  133. simopt/experimental.py +0 -109
  134. simopt/gui/data_farming_window.py +0 -1369
  135. simopt/gui/df_object.py +0 -703
  136. simopt/gui/main_menu.py +0 -111
  137. simopt/gui/new_experiment_window.py +0 -6479
  138. simopt/gui/plot_window.py +0 -1057
  139. simopt/gui/toplevel_custom.py +0 -106
  140. simopt/plots/__init__.py +0 -21
  141. simopt/plots/area_scatterplot.py +0 -292
  142. simopt/plots/feasibility_progress.py +0 -301
  143. simopt/plots/progress_curve.py +0 -267
  144. simopt/plots/solvability_cdf.py +0 -208
  145. simopt/plots/solvability_profile.py +0 -578
  146. simopt/plots/terminal_feasibility.py +0 -478
  147. simopt/plots/terminal_progress.py +0 -175
  148. simopt/plots/terminal_scatterplot.py +0 -131
  149. simopt/plots/utils.py +0 -507
  150. simopt/solver.py +0 -248
  151. simopt/solvers/__init__.py +0 -1
  152. simopt/web/dist/assets/index-BlZMNzom.js +0 -2
  153. simopt/web/dist/assets/index-DJP_mVbX.css +0 -1
  154. simoptlib-1.2.4.dist-info/RECORD +0 -88
  155. simoptlib-1.2.4.dist-info/top_level.txt +0 -1
  156. {simopt → simopt-solvers/src/simopt_solvers}/problem_types.py +0 -0
  157. {simoptlib-1.2.4.dist-info → simoptlib-1.3.0.dev0.dist-info}/licenses/LICENSE +0 -0
simopt/__main__.py CHANGED
@@ -1,12 +1,6 @@
1
1
  """Main module for the simopt package."""
2
2
 
3
- from simopt import GUI
4
-
5
-
6
- def main() -> None:
7
- """Launch the GUI."""
8
- GUI.main()
9
-
3
+ from simopt.cli import main
10
4
 
11
5
  if __name__ == "__main__":
12
6
  main()
simopt/_markers.py ADDED
@@ -0,0 +1,15 @@
1
+ """Identity markers for simulations and input models."""
2
+
3
+ from typing import TypeVar
4
+
5
+ T = TypeVar("T")
6
+
7
+
8
+ def simulation(function: T) -> T:
9
+ """Mark a simulation without changing its Python behavior."""
10
+ return function
11
+
12
+
13
+ def input_model(cls: type[T]) -> type[T]:
14
+ """Mark an input model without changing its Python behavior."""
15
+ return cls
@@ -0,0 +1 @@
1
+ """Analysis and plotting functions."""
simopt/analysis/auc.py ADDED
@@ -0,0 +1,233 @@
1
+ """Analysis and plotting of area under the curve (AUC)."""
2
+
3
+ from dataclasses import dataclass
4
+
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+ from matplotlib.collections import PathCollection
8
+ from matplotlib.container import ErrorbarContainer
9
+
10
+ from simopt.experiment.api import AnalysisInput, PlotConfig
11
+ from simopt.experiment.experiment import Experiment
12
+ from simopt.logging import Logger, null_logger
13
+ from simopt.options import (
14
+ DEFAULT_CONFIDENCE_INTERVAL_OPTIONS,
15
+ DEFAULT_CRN_OPTIONS,
16
+ ConfidenceIntervalOptions,
17
+ CrnOptions,
18
+ )
19
+
20
+ from .bootstrap import compute_bootstrap_conf_int
21
+ from .common import compute_estimator_and_ci, styled_group_entries
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class AucResult:
26
+ """Result of an AUC analysis."""
27
+
28
+ mean: float
29
+ std: float
30
+ lb_mean: float | None = None
31
+ ub_mean: float | None = None
32
+ lb_std: float | None = None
33
+ ub_std: float | None = None
34
+
35
+ def to_numpy(self) -> np.ndarray:
36
+ """Convert the AUC result to a NumPy array."""
37
+ return np.array([self.mean, self.std, self.lb_mean, self.ub_mean, self.lb_std, self.ub_std])
38
+
39
+
40
+ def _auc(x: np.ndarray, y: np.ndarray) -> float:
41
+ dx = np.diff(x)
42
+ return float(np.sum(dx * y[:-1]))
43
+
44
+
45
+ def _estimator(analysis_input: AnalysisInput) -> tuple[float, float]:
46
+ aucs = analysis_input.mean_df.groupby("mrep")[["budget", "objective"]].apply(
47
+ lambda x: _auc(x["budget"], x["objective"])
48
+ )
49
+ return float(np.mean(aucs)), float(np.std(aucs, ddof=1))
50
+
51
+
52
+ def _ci(
53
+ bootstraps: list[tuple[float, float]],
54
+ estimator: tuple[float, float] | None,
55
+ options: ConfidenceIntervalOptions,
56
+ ) -> tuple[float, float, float, float]:
57
+ mean_estimator, std_estimator = estimator or (None, None)
58
+
59
+ means = [bootstrap[0] for bootstrap in bootstraps]
60
+ stds = [bootstrap[1] for bootstrap in bootstraps]
61
+
62
+ lb_mean, ub_mean = compute_bootstrap_conf_int(
63
+ means,
64
+ options.confidence_level,
65
+ options.bias_correction,
66
+ mean_estimator,
67
+ )
68
+ lb_std, ub_std = compute_bootstrap_conf_int(
69
+ stds,
70
+ options.confidence_level,
71
+ options.bias_correction,
72
+ std_estimator,
73
+ )
74
+
75
+ return lb_mean, ub_mean, lb_std, ub_std
76
+
77
+
78
+ def analyze(
79
+ analysis_input: AnalysisInput,
80
+ ci_options: ConfidenceIntervalOptions | None,
81
+ crn_options: CrnOptions,
82
+ /,
83
+ *,
84
+ normalize: bool = True,
85
+ ) -> AucResult:
86
+ """Analyze the AUC of progress curves.
87
+
88
+ Args:
89
+ analysis_input: Analysis input containing the experiment results and references.
90
+ ci_options: Options for computing confidence intervals.
91
+ crn_options: Options for handling common random numbers.
92
+ normalize: Whether to normalize the objectives and budget.
93
+
94
+ Returns:
95
+ AucResult: The results of the AUC analysis.
96
+ """
97
+ (mean, std), ci = compute_estimator_and_ci(
98
+ analysis_input, _estimator, _ci, ci_options, crn_options, normalize=normalize
99
+ )
100
+
101
+ if ci is None:
102
+ return AucResult(mean=mean, std=std)
103
+
104
+ lb_mean, ub_mean, lb_std, ub_std = ci
105
+ return AucResult(
106
+ mean=mean,
107
+ std=std,
108
+ lb_mean=lb_mean,
109
+ ub_mean=ub_mean,
110
+ lb_std=lb_std,
111
+ ub_std=ub_std,
112
+ )
113
+
114
+
115
+ def plot(
116
+ ax: plt.Axes,
117
+ result: AucResult,
118
+ color: str,
119
+ marker: str,
120
+ label: str | None = None,
121
+ logger: Logger = null_logger,
122
+ ) -> ErrorbarContainer | PathCollection:
123
+ """Plot the AUC result on the given axes.
124
+
125
+ Args:
126
+ ax: Matplotlib axes to plot on.
127
+ result: The AUC result to plot.
128
+ color: Color of the plot element.
129
+ marker: Marker style for the plot.
130
+ label: Legend label for the plot element.
131
+ logger: Logger for debugging.
132
+
133
+ Returns:
134
+ The created plot container or collection.
135
+ """
136
+ if result.lb_mean is None:
137
+ return ax.scatter(
138
+ x=result.mean,
139
+ y=result.std,
140
+ color=color,
141
+ marker=marker,
142
+ label=label,
143
+ )
144
+
145
+ logger.debug("data", data=result.to_numpy())
146
+
147
+ # If any error values are negative, set them to zero.
148
+ assert result.lb_mean is not None
149
+ assert result.ub_mean is not None
150
+ assert result.lb_std is not None
151
+ assert result.ub_std is not None
152
+ x_err_low = max(0, result.mean - result.lb_mean)
153
+ x_err_high = max(0, result.ub_mean - result.mean)
154
+ y_err_low = max(0, result.std - result.lb_std)
155
+ y_err_high = max(0, result.ub_std - result.std)
156
+
157
+ x_err = [[x_err_low], [x_err_high]]
158
+ y_err = [[y_err_low], [y_err_high]]
159
+
160
+ return ax.errorbar(
161
+ x=result.mean,
162
+ y=result.std,
163
+ xerr=x_err,
164
+ yerr=y_err,
165
+ color=color,
166
+ marker=marker,
167
+ elinewidth=1,
168
+ label=label,
169
+ )
170
+
171
+
172
+ def analyze_many(
173
+ analysis_inputs: list[AnalysisInput],
174
+ ci_options: ConfidenceIntervalOptions | None,
175
+ crn_options: CrnOptions,
176
+ /,
177
+ *,
178
+ normalize: bool = True,
179
+ ) -> list[AucResult]:
180
+ """Analyze AUC for multiple experiments on the same problem."""
181
+ return [
182
+ analyze(analysis_input, ci_options, crn_options, normalize=normalize)
183
+ for analysis_input in analysis_inputs
184
+ ]
185
+
186
+
187
+ def plot_many(
188
+ results: list[AucResult],
189
+ experiments: list[Experiment],
190
+ /,
191
+ *,
192
+ colors: list[str] | None = None,
193
+ markers: list[str] | None = None,
194
+ logger: Logger = null_logger,
195
+ ) -> tuple[plt.Figure, plt.Axes]:
196
+ """Plot multiple AUC results."""
197
+ if len(experiments) != len(results):
198
+ raise ValueError("experiments must be the same length as results.")
199
+
200
+ if colors is None:
201
+ colors = [f"C{i}" for i in range(len(results))]
202
+ if markers is None:
203
+ markers = ["o", "v", "s", "*", "P", "X", "D", "V", ">", "<"]
204
+
205
+ group = list(zip(experiments, results, strict=True))
206
+ fig, ax = plt.subplots()
207
+ styled_entries, used_labels = styled_group_entries(group, colors, markers)
208
+ for _, result, color, marker, label in styled_entries:
209
+ plot(ax, result, color, marker, label=label, logger=logger)
210
+
211
+ ax.set_xlabel("Mean")
212
+ ax.set_ylabel("Standard Deviation")
213
+ if used_labels:
214
+ ax.legend()
215
+
216
+ return fig, ax
217
+
218
+
219
+ @dataclass(frozen=True)
220
+ class Auc(PlotConfig):
221
+ """Options for AUC analysis."""
222
+
223
+ ci_options: ConfidenceIntervalOptions | None = DEFAULT_CONFIDENCE_INTERVAL_OPTIONS
224
+ crn_options: CrnOptions = DEFAULT_CRN_OPTIONS
225
+
226
+ def plot(
227
+ self,
228
+ analysis_inputs: list[AnalysisInput],
229
+ experiments: list[Experiment],
230
+ ) -> tuple[plt.Figure, plt.Axes]:
231
+ """Analyze and plot AUC results."""
232
+ results = analyze_many(analysis_inputs, self.ci_options, self.crn_options)
233
+ return plot_many(results, experiments)
@@ -0,0 +1,300 @@
1
+ """Bootstrapping procedures."""
2
+
3
+ from collections.abc import Callable, Sequence
4
+ from typing import Any
5
+
6
+ import numpy as np
7
+ import pandas as pd
8
+
9
+ from mrg32k3a.mrg32k3a import MRG32k3a
10
+ from simopt.experiment.api import AnalysisInput
11
+
12
+
13
+ def _resample_macroreps(
14
+ rng: MRG32k3a, indices: list[int], disable_macrorep_bootstrap: bool
15
+ ) -> list[int]:
16
+ # Uniformly resample M macroreplications (with replacement) from 0, 1, ..., M - 1.
17
+ # Subsubstream 0: reserved for outer-level bootstrapping.
18
+ # Advance RNG subsubstream to prepare for inner-level bootstrapping.
19
+ if not disable_macrorep_bootstrap:
20
+ indices = rng.choices(indices, k=len(indices))
21
+ rng.advance_subsubstream()
22
+ return indices
23
+
24
+
25
+ def _bootstrap_resample(rng: MRG32k3a, sample: np.ndarray) -> np.ndarray:
26
+ n_samples = len(sample)
27
+ index = np.array(rng.choices(range(n_samples), k=n_samples))
28
+ return sample[index]
29
+
30
+
31
+ def _bootstrap_common(
32
+ rng: MRG32k3a,
33
+ x0_sample: np.ndarray,
34
+ xstar_sample: np.ndarray,
35
+ crn_across_x0_xstar: bool,
36
+ xstar_merit_sample: np.ndarray | None = None,
37
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray | None]:
38
+ # Subsubstream 1: reserved for bootstrapping at x0 and x*.
39
+ # Bootstrap sample post-replicates at common x0.
40
+ # Reset subsubstream if using CRN across budgets.
41
+ # Bootstrap sample postreplicates at reference optimal solution x*.
42
+ # Compute initial optimality gap.
43
+ x0_sample = _bootstrap_resample(rng, x0_sample)
44
+ if crn_across_x0_xstar:
45
+ rng.reset_subsubstream()
46
+ xstar_sample = _bootstrap_resample(rng, xstar_sample)
47
+
48
+ # Bootstrap sample post-replicates at the best merit solution, mirroring x*.
49
+ if xstar_merit_sample is not None:
50
+ if crn_across_x0_xstar:
51
+ rng.reset_subsubstream()
52
+ xstar_merit_sample = _bootstrap_resample(rng, xstar_merit_sample)
53
+
54
+ # Advance RNG subsubstream to prepare for inner-level bootstrapping.
55
+ rng.advance_subsubstream()
56
+
57
+ return x0_sample, xstar_sample, xstar_merit_sample
58
+
59
+
60
+ def _bootstrap_mrep(
61
+ mrep_data: dict[str, np.ndarray],
62
+ rng: MRG32k3a,
63
+ crn_across_budget: bool,
64
+ crn_across_macroreps: bool,
65
+ ) -> tuple[np.ndarray, np.ndarray]:
66
+ """Bootstrap a single macroreplication."""
67
+ objectives = mrep_data["objective"]
68
+ stochastic_constraints = mrep_data["stochastic_constraints"]
69
+ n_steps = objectives.shape[0]
70
+ n_preps = objectives.shape[1]
71
+
72
+ use_special_indices = crn_across_budget and not crn_across_macroreps
73
+ special_indices = rng.choices(range(n_preps), k=n_preps) if use_special_indices else None
74
+
75
+ all_indices = []
76
+ for _ in range(n_steps):
77
+ indices = special_indices if use_special_indices else rng.choices(range(n_preps), k=n_preps)
78
+ all_indices.append(indices)
79
+
80
+ if crn_across_budget and not use_special_indices:
81
+ rng.reset_subsubstream()
82
+
83
+ if not use_special_indices:
84
+ if crn_across_macroreps:
85
+ rng.reset_subsubstream()
86
+ else:
87
+ rng.advance_subsubstream()
88
+
89
+ all_indices = np.array(all_indices)
90
+ steps = np.arange(n_steps)[:, None]
91
+ bootstrap_objective = objectives[steps, all_indices].mean(axis=1)
92
+ bootstrap_stochastic_constraints = stochastic_constraints[steps, all_indices].mean(axis=1)
93
+ return bootstrap_objective, bootstrap_stochastic_constraints
94
+
95
+
96
+ def _transform_bootstrap_data(
97
+ df: pd.DataFrame,
98
+ n_preps: int,
99
+ ) -> list[dict[str, np.ndarray]]:
100
+ """Precompute per-macrorep arrays used by bootstrap sampling."""
101
+ data: list[dict[str, np.ndarray]] = []
102
+ for mrep in df.index.get_level_values("mrep").unique().to_list():
103
+ df_mrep = df.xs(mrep, level="mrep")
104
+ n_steps = len(df_mrep) // n_preps
105
+ budgets = df_mrep["budget"].to_numpy().reshape(n_steps, n_preps)[:, 0]
106
+ solutions = df_mrep["solution"].to_numpy().reshape(n_steps, n_preps)[:, 0]
107
+ objectives = df_mrep["objective"].to_numpy().reshape(n_steps, n_preps)
108
+ stochastic_constraint_dim = len(df_mrep["stochastic_constraints"].iloc[0])
109
+ stochastic_constraints = np.vstack(df_mrep["stochastic_constraints"].to_numpy()).reshape(
110
+ n_steps, n_preps, stochastic_constraint_dim
111
+ )
112
+ mrep_data = {
113
+ "budget": budgets,
114
+ "solution": solutions,
115
+ "objective": objectives,
116
+ "stochastic_constraints": stochastic_constraints,
117
+ }
118
+ if "deterministic_violation" in df_mrep:
119
+ mrep_data["deterministic_violation"] = (
120
+ df_mrep["deterministic_violation"].to_numpy().reshape(n_steps, n_preps)[:, 0]
121
+ )
122
+ data.append(mrep_data)
123
+ return data
124
+
125
+
126
+ def _bootstrap_sample(
127
+ analysis_input: AnalysisInput,
128
+ data: list[dict[str, np.ndarray]],
129
+ rng: MRG32k3a,
130
+ crn_across_budget: bool,
131
+ crn_across_macroreps: bool,
132
+ crn_across_x0_xstar: bool,
133
+ disable_macrorep_bootstrap: bool,
134
+ ) -> AnalysisInput:
135
+ mreps = list(range(len(data)))
136
+ bootstrap_mreps = _resample_macroreps(rng, mreps, disable_macrorep_bootstrap)
137
+
138
+ x0_sample, xstar_sample, xstar_merit_sample = _bootstrap_common(
139
+ rng,
140
+ analysis_input.x0_sample,
141
+ analysis_input.xstar_sample,
142
+ crn_across_x0_xstar,
143
+ analysis_input.xstar_merit_sample,
144
+ )
145
+
146
+ objectives = []
147
+ stochastic_constraints = []
148
+ for mrep in bootstrap_mreps:
149
+ bootstrap_objective, bootstrap_stochastic_constraints = _bootstrap_mrep(
150
+ data[mrep],
151
+ rng,
152
+ crn_across_budget,
153
+ crn_across_macroreps,
154
+ )
155
+ objectives.append(bootstrap_objective)
156
+ stochastic_constraints.append(bootstrap_stochastic_constraints)
157
+
158
+ n_steps = [len(data[mrep]["budget"]) for mrep in bootstrap_mreps]
159
+ full_df = pd.DataFrame(
160
+ {
161
+ "step": np.concatenate([np.arange(n) for n in n_steps]),
162
+ "budget": np.concatenate([data[mrep]["budget"] for mrep in bootstrap_mreps]),
163
+ "solution": np.concatenate([data[mrep]["solution"] for mrep in bootstrap_mreps]),
164
+ "objective": np.concatenate(objectives),
165
+ "stochastic_constraints": list(np.concatenate(stochastic_constraints)),
166
+ "mrep": np.repeat(np.arange(len(bootstrap_mreps)), n_steps),
167
+ }
168
+ )
169
+ if "deterministic_violation" in data[0]:
170
+ full_df["deterministic_violation"] = np.concatenate(
171
+ [data[mrep]["deterministic_violation"] for mrep in bootstrap_mreps]
172
+ )
173
+ return AnalysisInput(
174
+ full_df=full_df,
175
+ x0=analysis_input.x0,
176
+ x0_sample=x0_sample,
177
+ xstar=analysis_input.xstar,
178
+ xstar_sample=xstar_sample,
179
+ skip_aggregation=True,
180
+ xstar_merit_sample=xstar_merit_sample,
181
+ merit_normalization=analysis_input.merit_normalization,
182
+ )
183
+
184
+
185
+ def _get_n_preps(df: pd.DataFrame) -> int:
186
+ values = df.reset_index().groupby(["mrep", "step"])["rep"].max().add(1).unique()
187
+ if len(values) > 1:
188
+ raise ValueError("number of post replication is not consistent across macroreps and steps.")
189
+ return int(values[0])
190
+
191
+
192
+ def bootstrap(
193
+ analysis_input: AnalysisInput,
194
+ n_bootstraps: int,
195
+ f: Callable[[AnalysisInput], Any],
196
+ crn_across_budget: bool,
197
+ crn_across_macroreps: bool,
198
+ crn_across_x0_xstar: bool,
199
+ disable_macrorep_bootstrap: bool = False,
200
+ ) -> list[Any]:
201
+ """Perform bootstrapping on post-normalization results.
202
+
203
+ Args:
204
+ analysis_input: Analysis input containing post-replication data and references.
205
+ n_bootstraps: Number of bootstrap samples to generate.
206
+ f: Function to apply to each bootstrap sample.
207
+ It takes the bootstrap AnalysisInput and returns any result.
208
+ crn_across_budget: Whether to use CRN across budgets.
209
+ crn_across_macroreps: Whether to use CRN across macroreplications.
210
+ crn_across_x0_xstar: Whether to use CRN across initial and optimal solutions.
211
+ disable_macrorep_bootstrap: Whether to disable macroreplication-level
212
+ bootstrapping.
213
+
214
+ Returns:
215
+ List of results from applying `f` to each bootstrap sample.
216
+ """
217
+ # Compute n_preps directly from df
218
+ df = analysis_input.full_df
219
+ n_preps = _get_n_preps(df)
220
+ data = _transform_bootstrap_data(df, n_preps)
221
+
222
+ results = []
223
+ for i in range(n_bootstraps):
224
+ # Create random number generator for bootstrap sampling.
225
+ # Stream 1 dedicated for bootstrapping.
226
+ rng = MRG32k3a(s_ss_sss_index=[1, i, 0])
227
+ bootstrap_input = _bootstrap_sample(
228
+ analysis_input,
229
+ data,
230
+ rng,
231
+ crn_across_budget,
232
+ crn_across_macroreps,
233
+ crn_across_x0_xstar,
234
+ disable_macrorep_bootstrap,
235
+ )
236
+ result = f(bootstrap_input)
237
+ results.append(result)
238
+
239
+ return results
240
+
241
+
242
+ def compute_bootstrap_conf_int(
243
+ observations: Sequence[float] | np.ndarray,
244
+ conf_level: float,
245
+ bias_correction: bool = True,
246
+ overall_estimator: float | None = None,
247
+ ) -> tuple[float, float]:
248
+ """Construct a bootstrap confidence interval for an estimator.
249
+
250
+ Args:
251
+ observations (Sequence[float] | np.ndarray): Estimators from all bootstrap instances.
252
+ conf_level (float): Confidence level for confidence intervals, i.e., 1 - gamma;
253
+ must be in (0, 1).
254
+ bias_correction (bool, optional): Whether to use bias-corrected bootstrap CIs
255
+ (via the percentile method). Defaults to True.
256
+ overall_estimator (float | None, optional): The estimator to compute the CI
257
+ around. Required if`bias_correction` is True.
258
+
259
+ Returns:
260
+ tuple[float, float]: A tuple containing the lower and upper bounds of
261
+ the bootstrap confidence interval.
262
+
263
+ Raises:
264
+ ValueError: If `conf_level` is not in (0, 1), or if `overall_estimator` is None
265
+ when `bias_correction` is True.
266
+ """
267
+ # Value checking
268
+ if not 0 < conf_level < 1:
269
+ error_msg = "Confidence level must be in (0, 1)."
270
+ raise ValueError(error_msg)
271
+ if bias_correction and overall_estimator is None:
272
+ error_msg = "Overall estimator must be provided for bias correction."
273
+ raise ValueError(error_msg)
274
+
275
+ # This assertion is used during refactoring to ensure that observations is a 1D
276
+ # array.
277
+ values = np.asarray(observations, dtype=float)
278
+ assert values.ndim == 1
279
+
280
+ # Compute bootstrapping confidence interval via percentile method.
281
+ # See Efron (1981) "Nonparameteric Standard Errors and Confidence Intervals."
282
+ if bias_correction:
283
+ if overall_estimator is None:
284
+ error_msg = "Overall estimator must be provided for bias correction."
285
+ raise ValueError(error_msg)
286
+ # Lazy imports
287
+ from scipy.stats import norm
288
+
289
+ # For biased-corrected CIs, see equation (4.4) on page 146.
290
+ z0 = norm.ppf(np.mean([obs < overall_estimator for obs in values]))
291
+ zconflvl = norm.ppf(conf_level)
292
+ q_lower = norm.cdf(2 * z0 - zconflvl)
293
+ q_upper = norm.cdf(2 * z0 + zconflvl)
294
+ else:
295
+ # For uncorrected CIs, see equation (4.3) on page 146.
296
+ q_lower = (1 - conf_level) / 2
297
+ q_upper = 1 - (1 - conf_level) / 2
298
+ bs_conf_int_lower_bound = np.quantile(values, q=q_lower)
299
+ bs_conf_int_upper_bound = np.quantile(values, q=q_upper)
300
+ return bs_conf_int_lower_bound, bs_conf_int_upper_bound