-
Notifications
You must be signed in to change notification settings - Fork 2
feat: update examples with NPP plots, update bioma figure 4 logic and visualisation #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pFornagiel
wants to merge
7
commits into
main
Choose a base branch
from
@pForangiel/update_examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7bcdd3
feat: add scripts for number partitioning visualization and metrics a…
pFornagiel 40cc876
feat: refactor fig4.py to use matplotlib for 3D visualizations and me…
pFornagiel b93320f
refactor: simplify phase transition sweep script and update output fi…
pFornagiel 4fdb6e3
feat: update figures for optimization visualizations and add new imag…
pFornagiel eb88057
refactor: clean up comments and improve clarity in plot_metrics.py
pFornagiel 8e23d22
refactor: remove unused line in plot_metrics.py for cleaner code
pFornagiel e489155
refactor: update import statements and improve layout settings in num…
pFornagiel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| IMAGES_DIR = "images/number_partitioning" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| from pathlib import Path | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| from npp_paths import IMAGES_DIR | ||
|
|
||
| from lonkit import ( | ||
| CMLON, | ||
| ILSSampler, | ||
| ILSSamplerConfig, | ||
| LONConfig, | ||
| LONVisualizer, | ||
| NumberPartitioning, | ||
| ) | ||
|
|
||
| N = 20 | ||
| INSTANCE_SEED = 1 | ||
| N_RUNS = 100 | ||
| N_ITER = 500 | ||
| RANDOM_SEED = 42 | ||
|
|
||
| K_VALUES = [0.3, 0.7, 0.95] | ||
|
|
||
|
|
||
| def render_3d_lons(cmlon_by_k: dict[float, CMLON], output_dir: Path = Path(IMAGES_DIR)) -> None: | ||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| standard_camera = dict( | ||
| up=dict(x=0, y=0, z=1), | ||
| center=dict(x=0, y=0, z=0), | ||
| eye=dict(x=1.55, y=1.55, z=0.4), | ||
| ) | ||
|
|
||
| axis_config = dict( | ||
| visible=True, | ||
| showgrid=True, | ||
| gridcolor="lightgray", | ||
| showline=True, | ||
| linecolor="black", | ||
| showbackground=True, | ||
| backgroundcolor="rgb(250, 250, 250)", | ||
| zeroline=True, | ||
| zerolinecolor="gray", | ||
| showticklabels=True, | ||
| ) | ||
|
|
||
| for k, cmlon in cmlon_by_k.items(): | ||
| vis = LONVisualizer(min_edge_width=0.5, max_edge_width=1, min_node_size=2.5, arrow_size=0.1) | ||
| fig = vis.plot_3d(cmlon) | ||
| fig.update_layout( | ||
| scene=dict( | ||
| xaxis=dict(**axis_config, title="X"), | ||
| yaxis=dict(**axis_config, title="Y"), | ||
| zaxis=dict(**axis_config, title="Fitness"), | ||
| camera=dict(**standard_camera), | ||
| aspectmode="cube", | ||
| ), | ||
| showlegend=False, | ||
| width=900, | ||
| height=700, | ||
| margin=dict(l=20, r=20, t=40, b=20), | ||
| ) | ||
|
|
||
| fig.write_image(output_dir / f"NPP_{k}_3d.png", scale=2) | ||
|
|
||
|
|
||
| def render_merged_lon_grid(k_values: list[float], output_dir: Path = Path(IMAGES_DIR)) -> None: | ||
| fig, axes = plt.subplots(2, len(k_values), figsize=(5 * len(k_values), 10)) | ||
| if len(k_values) == 1: | ||
| axes = [[axes[0]], [axes[1]]] | ||
|
|
||
| for idx, k in enumerate(k_values): | ||
| img_2d = plt.imread(output_dir / f"NPP_{k}_2d.png") | ||
| img_3d = plt.imread(output_dir / f"NPP_{k}_3d.png") | ||
|
|
||
| ax_top = axes[0][idx] | ||
| ax_top.imshow(img_2d) | ||
| ax_top.set_title(f"2D CMLON (k={k})", fontsize=12) | ||
| ax_top.axis("off") | ||
|
|
||
| ax_bottom = axes[1][idx] | ||
| ax_bottom.imshow(img_3d) | ||
| ax_bottom.set_title(f"3D CMLON (k={k})", fontsize=12) | ||
| ax_bottom.axis("off") | ||
|
|
||
| fig.suptitle("Number Partitioning CMLON Views", fontsize=16, y=0.98) | ||
| fig.tight_layout(rect=(0, 0, 1, 0.96)) | ||
| fig.savefig(output_dir / "NPP_merged_cmlon_views.png", dpi=200) | ||
| plt.close(fig) | ||
|
|
||
|
|
||
| def main(): | ||
| Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sampler_config = ILSSamplerConfig(n_runs=N_RUNS, n_iter_no_change=N_ITER, seed=RANDOM_SEED) | ||
|
|
||
| lon_config = LONConfig(eq_atol=1e-8) | ||
| cmlon_by_k = {} | ||
|
|
||
| for k in K_VALUES: | ||
| problem = NumberPartitioning(n=N, k=k, instance_seed=INSTANCE_SEED) | ||
| sampler = ILSSampler(sampler_config) | ||
| result = sampler.sample(problem) | ||
|
|
||
| lon = sampler.sample_to_lon(result, lon_config) | ||
| cmlon = lon.to_cmlon() | ||
| cmlon_by_k[k] = cmlon | ||
|
|
||
| vis = LONVisualizer(0.5, 1, arrow_size=0.1) | ||
| vis.plot_2d(cmlon, f"{IMAGES_DIR}/NPP_{k}_2d.png") | ||
|
|
||
| render_3d_lons(cmlon_by_k) | ||
| render_merged_lon_grid(K_VALUES) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can’t we move this variable directly to
examples/number_partitioning/plot_lons.py?