From 5f5d86e0638b46482f60418231a4db0377d97562 Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Sun, 7 Dec 2025 10:32:38 -0500 Subject: [PATCH 1/5] orig --- notebooks/demo_fcsa2.ipynb | 345 +++++++++++++++++++++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 notebooks/demo_fcsa2.ipynb diff --git a/notebooks/demo_fcsa2.ipynb b/notebooks/demo_fcsa2.ipynb new file mode 100644 index 00000000..cf95fc20 --- /dev/null +++ b/notebooks/demo_fcsa2.ipynb @@ -0,0 +1,345 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "024a8b08-d0bb-4f84-b0ef-9da506ccc7f2", + "metadata": {}, + "source": [ + "# Demo for an experiment with FCSA on the SAN problem\n", + "This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. " + ] + }, + { + "cell_type": "markdown", + "id": "b7be033d-e71b-4735-acdb-dcdf96553c15", + "metadata": {}, + "source": [ + "## Append SimOpt Path\n", + "\n", + "Since the notebook is stored in simopt/notebooks, we need to append the\n", + "parent simopt directory to the system path to import the necessary modules\n", + "later on." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ab813968-e1d0-467a-b34e-5289a66e7a83", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "\n", + "sys.path.append(\"..\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e5fdc1e7-d902-4d5d-a1ee-ab67e13f68ae", + "metadata": {}, + "outputs": [], + "source": [ + "from simopt.experiment_base import (\n", + " PlotType,\n", + " ProblemSolver,\n", + " plot_feasibility_progress,\n", + " plot_progress_curves,\n", + " plot_terminal_feasibility,\n", + " plot_terminal_progress,\n", + " post_normalize,\n", + ")\n", + "from simopt.models.san import SANLongestPathStochastic\n", + "from simopt.solvers.fcsa import FCSA" + ] + }, + { + "cell_type": "markdown", + "id": "1983eb52-2b02-4f9c-8b6c-ef34b43e5ad7", + "metadata": {}, + "source": [ + "## Experiment Configuration Parameters\n", + "\n", + "Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2ab33f02-0070-49c0-ab1f-907d26af1080", + "metadata": {}, + "outputs": [], + "source": [ + "fixed_factors = {\n", + " \"constraint_nodes\": [6, 8], # nodes with stochastic constraints\n", + " \"length_to_node_constraint\": [5, 5], # max expected length to each constraint node\n", + " \"initial_solution\": (5,) * 13,\n", + " \"budget\": 10000,\n", + "}\n", + "problem = SANLongestPathStochastic(fixed_factors=fixed_factors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c9c8d14c-2712-4290-a6f9-e3e360db71bd", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "csa = FCSA(\n", + " fixed_factors={\n", + " \"search_direction\": \"CSA\",\n", + " \"normalize_grads\": False,\n", + " \"report_all_solns\": True,\n", + " \"crn_across_solns\": False,\n", + " },\n", + " name=\"CSA\",\n", + ")\n", + "csa_n = FCSA(\n", + " fixed_factors={\n", + " \"search_direction\": \"CSA\",\n", + " \"normalize_grads\": True,\n", + " \"report_all_solns\": True,\n", + " \"crn_across_solns\": False,\n", + " },\n", + " name=\"CSA-N\",\n", + ")\n", + "fcsa = FCSA(\n", + " fixed_factors={\n", + " \"search_direction\": \"FCSA\",\n", + " \"normalize_grads\": True,\n", + " \"report_all_solns\": True,\n", + " \"crn_across_solns\": False,\n", + " },\n", + " name=\"FCSA\",\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3795eb6b-be3f-4192-a218-624f26eb5514", + "metadata": {}, + "outputs": [], + "source": [ + "def run_experiment(solver, problem, n_macroreps, n_postreps):\n", + " experiment = ProblemSolver(solver=solver, problem=problem)\n", + " experiment.run(n_macroreps=n_macroreps)\n", + " experiment.post_replicate(n_postreps=n_postreps)\n", + " return experiment" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30a2d9ec-785e-44d8-90a6-c68f4759e8e3", + "metadata": {}, + "outputs": [], + "source": [ + "n_macroreps = 10\n", + "n_postreps = 100\n", + "experiments = [\n", + " run_experiment(solver, problem, n_macroreps, n_postreps)\n", + " for solver in [csa, csa_n, fcsa]\n", + "]\n", + "experiment1, experiment2, experiment3 = experiments\n", + "post_normalize(experiments, n_postreps)" + ] + }, + { + "cell_type": "markdown", + "id": "4b647488-a992-4d2e-8986-b6db5e70949b", + "metadata": {}, + "source": [ + "## Plotting Settings\n", + "\n", + "Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f699a65-c580-4ef4-8a0e-09f60d0898ff", + "metadata": {}, + "outputs": [], + "source": [ + "plot_terminal_progress([experiment1], PlotType.VIOLIN, normalize=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66b0254e-1adb-4b97-93e8-3f0b334af5e6", + "metadata": {}, + "outputs": [], + "source": [ + "plot_terminal_feasibility(\n", + " [[experiment1]], PlotType.FEASIBILITY_VIOLIN, all_in_one=True, two_sided=True\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df22deb8-97c8-4fc6-8eb3-54f42147110f", + "metadata": {}, + "outputs": [], + "source": [ + "plot_progress_curves([experiment1], PlotType.ALL, normalize=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5bbd05c7-8e8a-441d-8187-525202ed49eb", + "metadata": {}, + "outputs": [], + "source": [ + "plot_feasibility_progress(\n", + " [[experiment1]], PlotType.ALL_FEASIBILITY_PROGRESS, print_max_hw=False\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e4c7849-4011-421f-87f6-5cdc5f9505dd", + "metadata": {}, + "outputs": [], + "source": [ + "plot_progress_curves([experiment2, experiment3], PlotType.ALL, normalize=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "964b0b59-0fc5-4bd7-bbab-f1556267ebc7", + "metadata": {}, + "outputs": [], + "source": [ + "plot_feasibility_progress(\n", + " [[experiment2], [experiment3]],\n", + " PlotType.ALL_FEASIBILITY_PROGRESS,\n", + " print_max_hw=False,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "787a06e0-c5cc-4cb7-be1c-752bf81c8ad9", + "metadata": {}, + "source": [ + "## Experiment Configuration Parameters\n", + "\n", + "Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0b9d9594-f550-4e96-8a06-3e21659f7b8c", + "metadata": {}, + "outputs": [], + "source": [ + "csa_n = FCSA(\n", + " fixed_factors={\n", + " \"search_direction\": \"CSA\",\n", + " \"normalize_grads\": True,\n", + " \"report_all_solns\": False,\n", + " \"crn_across_solns\": False,\n", + " },\n", + " name=\"CSA-N\",\n", + ")\n", + "fcsa = FCSA(\n", + " fixed_factors={\n", + " \"search_direction\": \"FCSA\",\n", + " \"normalize_grads\": True,\n", + " \"report_all_solns\": False,\n", + " \"crn_across_solns\": False,\n", + " },\n", + " name=\"FCSA\",\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3d28bd7-04b4-49f2-b240-26f4b9f544e6", + "metadata": {}, + "outputs": [], + "source": [ + "experiments = [\n", + " run_experiment(solver, problem, n_macroreps, n_postreps) for solver in [csa_n, fcsa]\n", + "]\n", + "experiment2, experiment3 = experiments\n", + "post_normalize([experiment2, experiment3], 100)" + ] + }, + { + "cell_type": "markdown", + "id": "172f65b6-32be-4dac-a80a-a29ec15a5e8a", + "metadata": {}, + "source": [ + "## Plotting Settings\n", + "\n", + "Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0780c94c-25f8-47e6-866c-0fd64d9bd508", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'plot_terminal_feasibility' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m plot_terminal_feasibility([[experiment2], [experiment3]], PlotType\u001b[38;5;241m.\u001b[39mFEASIBILITY_SCATTER)\n", + "\u001b[1;31mNameError\u001b[0m: name 'plot_terminal_feasibility' is not defined" + ] + } + ], + "source": [ + "plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74180e66-6800-468e-a1e0-2e0e8e2685e9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "jupytext": { + "formats": "ipynb,py:percent" + }, + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 456ca4c7f6f8d662a6a7aa7db2a3a582d433a17f Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Sun, 7 Dec 2025 10:33:48 -0500 Subject: [PATCH 2/5] pair --- notebooks/demo_fcsa2.py | 178 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 notebooks/demo_fcsa2.py diff --git a/notebooks/demo_fcsa2.py b/notebooks/demo_fcsa2.py new file mode 100644 index 00000000..3b5d05eb --- /dev/null +++ b/notebooks/demo_fcsa2.py @@ -0,0 +1,178 @@ +# --- +# jupyter: +# jupytext: +# formats: ipynb,py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# jupytext_version: 1.18.1 +# kernelspec: +# display_name: Python [conda env:base] * +# language: python +# name: conda-base-py +# --- + +# %% [markdown] +# # Demo for an experiment with FCSA on the SAN problem +# This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. + +# %% [markdown] +# ## Append SimOpt Path +# +# Since the notebook is stored in simopt/notebooks, we need to append the +# parent simopt directory to the system path to import the necessary modules +# later on. + +# %% +import sys + +sys.path.append("..") + +# %% +from simopt.experiment_base import ( + PlotType, + ProblemSolver, + plot_feasibility_progress, + plot_progress_curves, + plot_terminal_feasibility, + plot_terminal_progress, + post_normalize, +) +from simopt.models.san import SANLongestPathStochastic +from simopt.solvers.fcsa import FCSA + +# %% [markdown] +# ## Experiment Configuration Parameters +# +# Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. + +# %% +fixed_factors = { + "constraint_nodes": [6, 8], # nodes with stochastic constraints + "length_to_node_constraint": [5, 5], # max expected length to each constraint node + "initial_solution": (5,) * 13, + "budget": 10000, +} +problem = SANLongestPathStochastic(fixed_factors=fixed_factors) + +# %% +csa = FCSA( + fixed_factors={ + "search_direction": "CSA", + "normalize_grads": False, + "report_all_solns": True, + "crn_across_solns": False, + }, + name="CSA", +) +csa_n = FCSA( + fixed_factors={ + "search_direction": "CSA", + "normalize_grads": True, + "report_all_solns": True, + "crn_across_solns": False, + }, + name="CSA-N", +) +fcsa = FCSA( + fixed_factors={ + "search_direction": "FCSA", + "normalize_grads": True, + "report_all_solns": True, + "crn_across_solns": False, + }, + name="FCSA", +) + + +# %% +def run_experiment(solver, problem, n_macroreps, n_postreps): + experiment = ProblemSolver(solver=solver, problem=problem) + experiment.run(n_macroreps=n_macroreps) + experiment.post_replicate(n_postreps=n_postreps) + return experiment + + +# %% +n_macroreps = 10 +n_postreps = 100 +experiments = [ + run_experiment(solver, problem, n_macroreps, n_postreps) + for solver in [csa, csa_n, fcsa] +] +experiment1, experiment2, experiment3 = experiments +post_normalize(experiments, n_postreps) + +# %% [markdown] +# ## Plotting Settings +# +# Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions. + +# %% +plot_terminal_progress([experiment1], PlotType.VIOLIN, normalize=False) + +# %% +plot_terminal_feasibility( + [[experiment1]], PlotType.FEASIBILITY_VIOLIN, all_in_one=True, two_sided=True +) + +# %% +plot_progress_curves([experiment1], PlotType.ALL, normalize=False) + +# %% +plot_feasibility_progress( + [[experiment1]], PlotType.ALL_FEASIBILITY_PROGRESS, print_max_hw=False +) + +# %% +plot_progress_curves([experiment2, experiment3], PlotType.ALL, normalize=False) + +# %% +plot_feasibility_progress( + [[experiment2], [experiment3]], + PlotType.ALL_FEASIBILITY_PROGRESS, + print_max_hw=False, +) + +# %% [markdown] +# ## Experiment Configuration Parameters +# +# Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. + +# %% +csa_n = FCSA( + fixed_factors={ + "search_direction": "CSA", + "normalize_grads": True, + "report_all_solns": False, + "crn_across_solns": False, + }, + name="CSA-N", +) +fcsa = FCSA( + fixed_factors={ + "search_direction": "FCSA", + "normalize_grads": True, + "report_all_solns": False, + "crn_across_solns": False, + }, + name="FCSA", +) + +# %% +experiments = [ + run_experiment(solver, problem, n_macroreps, n_postreps) for solver in [csa_n, fcsa] +] +experiment2, experiment3 = experiments +post_normalize([experiment2, experiment3], 100) + +# %% [markdown] +# ## Plotting Settings +# +# Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. + +# %% +plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER) + +# %% From 40766a64699971a443aef9ed0644fd254b81c34a Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Sun, 7 Dec 2025 10:36:00 -0500 Subject: [PATCH 3/5] clean --- notebooks/demo_fcsa2.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/notebooks/demo_fcsa2.py b/notebooks/demo_fcsa2.py index 3b5d05eb..e3e30865 100644 --- a/notebooks/demo_fcsa2.py +++ b/notebooks/demo_fcsa2.py @@ -6,11 +6,11 @@ # extension: .py # format_name: percent # format_version: '1.3' -# jupytext_version: 1.18.1 +# jupytext_version: 1.17.3 # kernelspec: -# display_name: Python [conda env:base] * +# display_name: Python 3 (ipykernel) # language: python -# name: conda-base-py +# name: python3 # --- # %% [markdown] @@ -174,5 +174,3 @@ def run_experiment(solver, problem, n_macroreps, n_postreps): # %% plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER) - -# %% From cad617b9abd87f6e1a44e9d13b697a008e3fcbe0 Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Sun, 7 Dec 2025 10:37:11 -0500 Subject: [PATCH 4/5] upd --- notebooks/demo_fcsa.py | 31 ++++ notebooks/demo_fcsa2.ipynb | 345 ------------------------------------- notebooks/demo_fcsa2.py | 176 ------------------- 3 files changed, 31 insertions(+), 521 deletions(-) delete mode 100644 notebooks/demo_fcsa2.ipynb delete mode 100644 notebooks/demo_fcsa2.py diff --git a/notebooks/demo_fcsa.py b/notebooks/demo_fcsa.py index 2a878ac2..e3e30865 100644 --- a/notebooks/demo_fcsa.py +++ b/notebooks/demo_fcsa.py @@ -13,6 +13,17 @@ # name: python3 # --- +# %% [markdown] +# # Demo for an experiment with FCSA on the SAN problem +# This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. + +# %% [markdown] +# ## Append SimOpt Path +# +# Since the notebook is stored in simopt/notebooks, we need to append the +# parent simopt directory to the system path to import the necessary modules +# later on. + # %% import sys @@ -31,6 +42,11 @@ from simopt.models.san import SANLongestPathStochastic from simopt.solvers.fcsa import FCSA +# %% [markdown] +# ## Experiment Configuration Parameters +# +# Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. + # %% fixed_factors = { "constraint_nodes": [6, 8], # nodes with stochastic constraints @@ -88,6 +104,11 @@ def run_experiment(solver, problem, n_macroreps, n_postreps): experiment1, experiment2, experiment3 = experiments post_normalize(experiments, n_postreps) +# %% [markdown] +# ## Plotting Settings +# +# Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions. + # %% plot_terminal_progress([experiment1], PlotType.VIOLIN, normalize=False) @@ -114,6 +135,11 @@ def run_experiment(solver, problem, n_macroreps, n_postreps): print_max_hw=False, ) +# %% [markdown] +# ## Experiment Configuration Parameters +# +# Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. + # %% csa_n = FCSA( fixed_factors={ @@ -141,5 +167,10 @@ def run_experiment(solver, problem, n_macroreps, n_postreps): experiment2, experiment3 = experiments post_normalize([experiment2, experiment3], 100) +# %% [markdown] +# ## Plotting Settings +# +# Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. + # %% plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER) diff --git a/notebooks/demo_fcsa2.ipynb b/notebooks/demo_fcsa2.ipynb deleted file mode 100644 index cf95fc20..00000000 --- a/notebooks/demo_fcsa2.ipynb +++ /dev/null @@ -1,345 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "024a8b08-d0bb-4f84-b0ef-9da506ccc7f2", - "metadata": {}, - "source": [ - "# Demo for an experiment with FCSA on the SAN problem\n", - "This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. " - ] - }, - { - "cell_type": "markdown", - "id": "b7be033d-e71b-4735-acdb-dcdf96553c15", - "metadata": {}, - "source": [ - "## Append SimOpt Path\n", - "\n", - "Since the notebook is stored in simopt/notebooks, we need to append the\n", - "parent simopt directory to the system path to import the necessary modules\n", - "later on." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "ab813968-e1d0-467a-b34e-5289a66e7a83", - "metadata": {}, - "outputs": [], - "source": [ - "import sys\n", - "\n", - "sys.path.append(\"..\")" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "e5fdc1e7-d902-4d5d-a1ee-ab67e13f68ae", - "metadata": {}, - "outputs": [], - "source": [ - "from simopt.experiment_base import (\n", - " PlotType,\n", - " ProblemSolver,\n", - " plot_feasibility_progress,\n", - " plot_progress_curves,\n", - " plot_terminal_feasibility,\n", - " plot_terminal_progress,\n", - " post_normalize,\n", - ")\n", - "from simopt.models.san import SANLongestPathStochastic\n", - "from simopt.solvers.fcsa import FCSA" - ] - }, - { - "cell_type": "markdown", - "id": "1983eb52-2b02-4f9c-8b6c-ef34b43e5ad7", - "metadata": {}, - "source": [ - "## Experiment Configuration Parameters\n", - "\n", - "Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. " - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "2ab33f02-0070-49c0-ab1f-907d26af1080", - "metadata": {}, - "outputs": [], - "source": [ - "fixed_factors = {\n", - " \"constraint_nodes\": [6, 8], # nodes with stochastic constraints\n", - " \"length_to_node_constraint\": [5, 5], # max expected length to each constraint node\n", - " \"initial_solution\": (5,) * 13,\n", - " \"budget\": 10000,\n", - "}\n", - "problem = SANLongestPathStochastic(fixed_factors=fixed_factors)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c9c8d14c-2712-4290-a6f9-e3e360db71bd", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "csa = FCSA(\n", - " fixed_factors={\n", - " \"search_direction\": \"CSA\",\n", - " \"normalize_grads\": False,\n", - " \"report_all_solns\": True,\n", - " \"crn_across_solns\": False,\n", - " },\n", - " name=\"CSA\",\n", - ")\n", - "csa_n = FCSA(\n", - " fixed_factors={\n", - " \"search_direction\": \"CSA\",\n", - " \"normalize_grads\": True,\n", - " \"report_all_solns\": True,\n", - " \"crn_across_solns\": False,\n", - " },\n", - " name=\"CSA-N\",\n", - ")\n", - "fcsa = FCSA(\n", - " fixed_factors={\n", - " \"search_direction\": \"FCSA\",\n", - " \"normalize_grads\": True,\n", - " \"report_all_solns\": True,\n", - " \"crn_across_solns\": False,\n", - " },\n", - " name=\"FCSA\",\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3795eb6b-be3f-4192-a218-624f26eb5514", - "metadata": {}, - "outputs": [], - "source": [ - "def run_experiment(solver, problem, n_macroreps, n_postreps):\n", - " experiment = ProblemSolver(solver=solver, problem=problem)\n", - " experiment.run(n_macroreps=n_macroreps)\n", - " experiment.post_replicate(n_postreps=n_postreps)\n", - " return experiment" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "30a2d9ec-785e-44d8-90a6-c68f4759e8e3", - "metadata": {}, - "outputs": [], - "source": [ - "n_macroreps = 10\n", - "n_postreps = 100\n", - "experiments = [\n", - " run_experiment(solver, problem, n_macroreps, n_postreps)\n", - " for solver in [csa, csa_n, fcsa]\n", - "]\n", - "experiment1, experiment2, experiment3 = experiments\n", - "post_normalize(experiments, n_postreps)" - ] - }, - { - "cell_type": "markdown", - "id": "4b647488-a992-4d2e-8986-b6db5e70949b", - "metadata": {}, - "source": [ - "## Plotting Settings\n", - "\n", - "Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5f699a65-c580-4ef4-8a0e-09f60d0898ff", - "metadata": {}, - "outputs": [], - "source": [ - "plot_terminal_progress([experiment1], PlotType.VIOLIN, normalize=False)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "66b0254e-1adb-4b97-93e8-3f0b334af5e6", - "metadata": {}, - "outputs": [], - "source": [ - "plot_terminal_feasibility(\n", - " [[experiment1]], PlotType.FEASIBILITY_VIOLIN, all_in_one=True, two_sided=True\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "df22deb8-97c8-4fc6-8eb3-54f42147110f", - "metadata": {}, - "outputs": [], - "source": [ - "plot_progress_curves([experiment1], PlotType.ALL, normalize=False)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5bbd05c7-8e8a-441d-8187-525202ed49eb", - "metadata": {}, - "outputs": [], - "source": [ - "plot_feasibility_progress(\n", - " [[experiment1]], PlotType.ALL_FEASIBILITY_PROGRESS, print_max_hw=False\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4e4c7849-4011-421f-87f6-5cdc5f9505dd", - "metadata": {}, - "outputs": [], - "source": [ - "plot_progress_curves([experiment2, experiment3], PlotType.ALL, normalize=False)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "964b0b59-0fc5-4bd7-bbab-f1556267ebc7", - "metadata": {}, - "outputs": [], - "source": [ - "plot_feasibility_progress(\n", - " [[experiment2], [experiment3]],\n", - " PlotType.ALL_FEASIBILITY_PROGRESS,\n", - " print_max_hw=False,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "787a06e0-c5cc-4cb7-be1c-752bf81c8ad9", - "metadata": {}, - "source": [ - "## Experiment Configuration Parameters\n", - "\n", - "Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0b9d9594-f550-4e96-8a06-3e21659f7b8c", - "metadata": {}, - "outputs": [], - "source": [ - "csa_n = FCSA(\n", - " fixed_factors={\n", - " \"search_direction\": \"CSA\",\n", - " \"normalize_grads\": True,\n", - " \"report_all_solns\": False,\n", - " \"crn_across_solns\": False,\n", - " },\n", - " name=\"CSA-N\",\n", - ")\n", - "fcsa = FCSA(\n", - " fixed_factors={\n", - " \"search_direction\": \"FCSA\",\n", - " \"normalize_grads\": True,\n", - " \"report_all_solns\": False,\n", - " \"crn_across_solns\": False,\n", - " },\n", - " name=\"FCSA\",\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c3d28bd7-04b4-49f2-b240-26f4b9f544e6", - "metadata": {}, - "outputs": [], - "source": [ - "experiments = [\n", - " run_experiment(solver, problem, n_macroreps, n_postreps) for solver in [csa_n, fcsa]\n", - "]\n", - "experiment2, experiment3 = experiments\n", - "post_normalize([experiment2, experiment3], 100)" - ] - }, - { - "cell_type": "markdown", - "id": "172f65b6-32be-4dac-a80a-a29ec15a5e8a", - "metadata": {}, - "source": [ - "## Plotting Settings\n", - "\n", - "Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. " - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "0780c94c-25f8-47e6-866c-0fd64d9bd508", - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'plot_terminal_feasibility' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m plot_terminal_feasibility([[experiment2], [experiment3]], PlotType\u001b[38;5;241m.\u001b[39mFEASIBILITY_SCATTER)\n", - "\u001b[1;31mNameError\u001b[0m: name 'plot_terminal_feasibility' is not defined" - ] - } - ], - "source": [ - "plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "74180e66-6800-468e-a1e0-2e0e8e2685e9", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "jupytext": { - "formats": "ipynb,py:percent" - }, - "kernelspec": { - "display_name": "Python [conda env:base] *", - "language": "python", - "name": "conda-base-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.13.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/demo_fcsa2.py b/notebooks/demo_fcsa2.py deleted file mode 100644 index e3e30865..00000000 --- a/notebooks/demo_fcsa2.py +++ /dev/null @@ -1,176 +0,0 @@ -# --- -# jupyter: -# jupytext: -# formats: ipynb,py:percent -# text_representation: -# extension: .py -# format_name: percent -# format_version: '1.3' -# jupytext_version: 1.17.3 -# kernelspec: -# display_name: Python 3 (ipykernel) -# language: python -# name: python3 -# --- - -# %% [markdown] -# # Demo for an experiment with FCSA on the SAN problem -# This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. - -# %% [markdown] -# ## Append SimOpt Path -# -# Since the notebook is stored in simopt/notebooks, we need to append the -# parent simopt directory to the system path to import the necessary modules -# later on. - -# %% -import sys - -sys.path.append("..") - -# %% -from simopt.experiment_base import ( - PlotType, - ProblemSolver, - plot_feasibility_progress, - plot_progress_curves, - plot_terminal_feasibility, - plot_terminal_progress, - post_normalize, -) -from simopt.models.san import SANLongestPathStochastic -from simopt.solvers.fcsa import FCSA - -# %% [markdown] -# ## Experiment Configuration Parameters -# -# Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. - -# %% -fixed_factors = { - "constraint_nodes": [6, 8], # nodes with stochastic constraints - "length_to_node_constraint": [5, 5], # max expected length to each constraint node - "initial_solution": (5,) * 13, - "budget": 10000, -} -problem = SANLongestPathStochastic(fixed_factors=fixed_factors) - -# %% -csa = FCSA( - fixed_factors={ - "search_direction": "CSA", - "normalize_grads": False, - "report_all_solns": True, - "crn_across_solns": False, - }, - name="CSA", -) -csa_n = FCSA( - fixed_factors={ - "search_direction": "CSA", - "normalize_grads": True, - "report_all_solns": True, - "crn_across_solns": False, - }, - name="CSA-N", -) -fcsa = FCSA( - fixed_factors={ - "search_direction": "FCSA", - "normalize_grads": True, - "report_all_solns": True, - "crn_across_solns": False, - }, - name="FCSA", -) - - -# %% -def run_experiment(solver, problem, n_macroreps, n_postreps): - experiment = ProblemSolver(solver=solver, problem=problem) - experiment.run(n_macroreps=n_macroreps) - experiment.post_replicate(n_postreps=n_postreps) - return experiment - - -# %% -n_macroreps = 10 -n_postreps = 100 -experiments = [ - run_experiment(solver, problem, n_macroreps, n_postreps) - for solver in [csa, csa_n, fcsa] -] -experiment1, experiment2, experiment3 = experiments -post_normalize(experiments, n_postreps) - -# %% [markdown] -# ## Plotting Settings -# -# Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions. - -# %% -plot_terminal_progress([experiment1], PlotType.VIOLIN, normalize=False) - -# %% -plot_terminal_feasibility( - [[experiment1]], PlotType.FEASIBILITY_VIOLIN, all_in_one=True, two_sided=True -) - -# %% -plot_progress_curves([experiment1], PlotType.ALL, normalize=False) - -# %% -plot_feasibility_progress( - [[experiment1]], PlotType.ALL_FEASIBILITY_PROGRESS, print_max_hw=False -) - -# %% -plot_progress_curves([experiment2, experiment3], PlotType.ALL, normalize=False) - -# %% -plot_feasibility_progress( - [[experiment2], [experiment3]], - PlotType.ALL_FEASIBILITY_PROGRESS, - print_max_hw=False, -) - -# %% [markdown] -# ## Experiment Configuration Parameters -# -# Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. - -# %% -csa_n = FCSA( - fixed_factors={ - "search_direction": "CSA", - "normalize_grads": True, - "report_all_solns": False, - "crn_across_solns": False, - }, - name="CSA-N", -) -fcsa = FCSA( - fixed_factors={ - "search_direction": "FCSA", - "normalize_grads": True, - "report_all_solns": False, - "crn_across_solns": False, - }, - name="FCSA", -) - -# %% -experiments = [ - run_experiment(solver, problem, n_macroreps, n_postreps) for solver in [csa_n, fcsa] -] -experiment2, experiment3 = experiments -post_normalize([experiment2, experiment3], 100) - -# %% [markdown] -# ## Plotting Settings -# -# Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. - -# %% -plot_terminal_feasibility([[experiment2], [experiment3]], PlotType.FEASIBILITY_SCATTER) From 1dd561026917ee86f9ffd67324f7362cefa85ebe Mon Sep 17 00:00:00 2001 From: Cen Wang Date: Sun, 7 Dec 2025 10:38:01 -0500 Subject: [PATCH 5/5] sync --- notebooks/demo_fcsa.ipynb | 61 +++++++++++++++++++++++++++++++++++++++ notebooks/demo_fcsa.py | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/notebooks/demo_fcsa.ipynb b/notebooks/demo_fcsa.ipynb index 26455cd4..e1643b2d 100644 --- a/notebooks/demo_fcsa.ipynb +++ b/notebooks/demo_fcsa.ipynb @@ -1,5 +1,26 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "04c470f6", + "metadata": {}, + "source": [ + "# Demo for an experiment with FCSA on the SAN problem\n", + "This script is intented to demonstrate an experiment with three different versions of the FCSA solver on the SAN problem. " + ] + }, + { + "cell_type": "markdown", + "id": "8dffeae6", + "metadata": {}, + "source": [ + "## Append SimOpt Path\n", + "\n", + "Since the notebook is stored in simopt/notebooks, we need to append the\n", + "parent simopt directory to the system path to import the necessary modules\n", + "later on." + ] + }, { "cell_type": "code", "execution_count": null, @@ -32,6 +53,16 @@ "from simopt.solvers.fcsa import FCSA" ] }, + { + "cell_type": "markdown", + "id": "dc0e1dfe", + "metadata": {}, + "source": [ + "## Experiment Configuration Parameters\n", + "\n", + "Configure 3 versions of the solver: CSA, CSA-N, and FCSA and set problem configuration. Set report_all_solutions = True meaning all incumbent solutions will be reported. " + ] + }, { "cell_type": "code", "execution_count": null, @@ -117,6 +148,16 @@ "post_normalize(experiments, n_postreps)" ] }, + { + "cell_type": "markdown", + "id": "6d42598f", + "metadata": {}, + "source": [ + "## Plotting Settings\n", + "\n", + "Define the plotting settings for the experiments. Plot terminal objective progress, terminal feasibility progress, objective progress curve, and feasiblity progress curve for all incumbent solutions." + ] + }, { "cell_type": "code", "execution_count": null, @@ -185,6 +226,16 @@ ")" ] }, + { + "cell_type": "markdown", + "id": "418eefec", + "metadata": {}, + "source": [ + "## Experiment Configuration Parameters\n", + "\n", + "Configure 2 versions of the solver: CSA-N, and FCSA and set problem configuration. Set report_all_solutions = False meaning only recommended solutions will be reported. " + ] + }, { "cell_type": "code", "execution_count": null, @@ -226,6 +277,16 @@ "post_normalize([experiment2, experiment3], 100)" ] }, + { + "cell_type": "markdown", + "id": "5c9de038", + "metadata": {}, + "source": [ + "## Plotting Settings\n", + "\n", + "Define the plotting settings for the experiments. Plot terminal objective vs feasibility scatter plot for recommended solutions. " + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/notebooks/demo_fcsa.py b/notebooks/demo_fcsa.py index e3e30865..0f7d29db 100644 --- a/notebooks/demo_fcsa.py +++ b/notebooks/demo_fcsa.py @@ -6,7 +6,7 @@ # extension: .py # format_name: percent # format_version: '1.3' -# jupytext_version: 1.17.3 +# jupytext_version: 1.18.1 # kernelspec: # display_name: Python 3 (ipykernel) # language: python