Skip to content
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

Replace ballistic by mode #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/simulated_bifurcation/core/ising.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

"""

from typing import Optional, TypeVar, Union
from typing import Literal, Optional, TypeVar, Union

import torch
from numpy import ndarray
Expand Down Expand Up @@ -241,7 +241,7 @@ def minimize(
self,
agents: int = 128,
max_steps: int = 10000,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
*,
Expand All @@ -263,7 +263,7 @@ def minimize(
max_steps : int, default=10_000
Number of iterations after which the algorithm is stopped
regardless of whether convergence has been achieved.
ballistic : bool, default=False
mode : "ballistic" | "discrete", optional, default = "ballistic"
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
Expand Down Expand Up @@ -390,12 +390,13 @@ def minimize(
https://doi.org/10.1038/s42005-022-00929-9

"""
engine = SimulatedBifurcationEngine.get_engine(ballistic, heated)
engine = SimulatedBifurcationEngine.get_engine(mode)
optimizer = SimulatedBifurcationOptimizer(
agents,
max_steps,
timeout,
engine,
heated,
verbose,
sampling_period,
convergence_threshold,
Expand Down
35 changes: 19 additions & 16 deletions src/simulated_bifurcation/core/quadratic_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""

import re
from typing import Optional, Tuple, Union
from typing import Literal, Optional, Tuple, Union

import numpy as np
import torch
Expand Down Expand Up @@ -313,7 +313,7 @@ def optimize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
minimize: bool = True,
verbose: bool = True,
Expand Down Expand Up @@ -379,9 +379,10 @@ def optimize(
timeout : float | None, default=None
Time in seconds after which the simulation is stopped.
None means no timeout.
ballistic : bool, optional
if True, the ballistic SB will be used, else it will be the
discrete SB (default is True)
mode : "ballistic" | "discrete", optional, default = "ballistic"
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
heated : bool, optional
if True, the heated SB will be used, else it will be the non-heated
SB (default is True)
Expand All @@ -407,7 +408,7 @@ def optimize(
ising_equivalent.minimize(
agents,
max_steps,
ballistic,
mode,
heated,
verbose,
use_window=use_window,
Expand All @@ -430,7 +431,7 @@ def minimize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
*,
Expand Down Expand Up @@ -495,9 +496,10 @@ def minimize(
timeout : float | None, default=None
Time in seconds after which the simulation is stopped.
None means no timeout.
ballistic : bool, optional
if True, the ballistic SB will be used, else it will be the
discrete SB (default is True)
mode : "ballistic" | "discrete", optional, default = "ballistic"
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
heated : bool, optional
if True, the heated SB will be used, else it will be the non-heated
SB (default is True)
Expand All @@ -518,7 +520,7 @@ def minimize(
agents,
max_steps,
best_only,
ballistic,
mode,
heated,
True,
verbose,
Expand All @@ -534,7 +536,7 @@ def maximize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
*,
Expand Down Expand Up @@ -598,9 +600,10 @@ def maximize(
timeout : float | None, default=None
Time in seconds after which the simulation is stopped.
None means no timeout.
ballistic : bool, optional
if True, the ballistic SB will be used, else it will be the
discrete SB (default is True)
mode : "ballistic" | "discrete", optional, default = "ballistic"
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
heated : bool, optional
if True, the heated SB will be used, else it will be the non-heated
SB (default is True)
Expand All @@ -621,7 +624,7 @@ def maximize(
agents,
max_steps,
best_only,
ballistic,
mode,
heated,
False,
verbose,
Expand Down
14 changes: 7 additions & 7 deletions src/simulated_bifurcation/models/abc_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC
from typing import Optional, Tuple
from typing import Literal, Optional, Tuple

import torch

Expand All @@ -26,7 +26,7 @@ def optimize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
minimize: bool = True,
verbose: bool = True,
Expand All @@ -41,7 +41,7 @@ def optimize(
agents,
max_steps,
best_only,
ballistic,
mode,
heated,
minimize,
verbose,
Expand All @@ -56,7 +56,7 @@ def minimize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
*,
Expand All @@ -69,7 +69,7 @@ def minimize(
agents,
max_steps,
best_only,
ballistic,
mode,
heated,
True,
verbose,
Expand All @@ -84,7 +84,7 @@ def maximize(
agents: int = 128,
max_steps: int = 10000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
*,
Expand All @@ -97,7 +97,7 @@ def maximize(
agents,
max_steps,
best_only,
ballistic,
mode,
heated,
False,
verbose,
Expand Down
39 changes: 18 additions & 21 deletions src/simulated_bifurcation/optimizer/simulated_bifurcation_engine.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
from enum import Enum
from typing import Callable, Literal

import torch


class SimulatedBifurcationEngine(Enum):
"""
Enum class that gathers the 4 variants of the Simulated Bifurcation
algorithm:

1. Ballistic SB (bSB)
2. Discrete SB (dSB)
3. Heated ballistic SB (HbSB)
4. Heated discrete SB (HdSB)
Variants of the Simulated Bifurcation algorithm.
"""

bSB = torch.nn.Identity(), False
dSB = torch.sign, False
HbSB = torch.nn.Identity(), True
HdSB = torch.sign, True
bSB = torch.nn.Identity()
dSB = torch.sign

def __init__(
self, activation_function: Callable[[torch.Tensor], torch.Tensor]
) -> None:
self.__activation_function = activation_function

def __init__(self, activation_function, heated: bool) -> None:
self.activation_function = activation_function
self.heated = heated
@property
def activation_function(self) -> Callable[[torch.Tensor], torch.Tensor]:
return self.__activation_function

@staticmethod
def get_engine(ballistic: bool, heated: bool):
if ballistic:
if heated:
return SimulatedBifurcationEngine.HbSB
def get_engine(engine_name: Literal["ballistic", "discrete"]):
if engine_name == "ballistic":
return SimulatedBifurcationEngine.bSB
if heated:
return SimulatedBifurcationEngine.HdSB
return SimulatedBifurcationEngine.dSB
elif engine_name == "discrete":
return SimulatedBifurcationEngine.dSB
else:
raise ValueError(f"Unknwown Simulated Bifurcation engine: {engine_name}.")
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
max_steps: Optional[int],
timeout: Optional[float],
engine: SimulatedBifurcationEngine,
heated: bool,
verbose: bool,
sampling_period: int,
convergence_threshold: int,
Expand All @@ -78,7 +79,7 @@ def __init__(
self.window = None
self.symplectic_integrator = None
self.heat_coefficient = ENVIRONMENT.heat_coefficient
self.heated = engine.heated
self.heated = heated
self.verbose = verbose
self.start_time = None
self.simulation_time = None
Expand Down
20 changes: 10 additions & 10 deletions src/simulated_bifurcation/simulated_bifurcation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

"""

from typing import Optional, Tuple, Union
from typing import Literal, Optional, Tuple, Union

import torch

Expand Down Expand Up @@ -164,7 +164,7 @@ def optimize(
agents: int = 128,
max_steps: int = 10_000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
minimize: bool = True,
verbose: bool = True,
Expand Down Expand Up @@ -228,7 +228,7 @@ def optimize(
polynomial at this vector. Otherwise, returns all the vectors
found by the SB algorithm and the values of polynomial at these
points.
ballistic : bool, default=False, keyword-only
mode : "ballistic" | "discrete", default="ballistic, keyword-only
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
Expand Down Expand Up @@ -422,7 +422,7 @@ def optimize(
agents=agents,
max_steps=max_steps,
best_only=best_only,
ballistic=ballistic,
mode=mode,
heated=heated,
minimize=minimize,
verbose=verbose,
Expand All @@ -442,7 +442,7 @@ def minimize(
agents: int = 128,
max_steps: int = 10_000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
use_window: bool = True,
Expand Down Expand Up @@ -505,7 +505,7 @@ def minimize(
polynomial at this vector. Otherwise, returns all the vectors
found by the SB algorithm and the values of polynomial at these
points.
ballistic : bool, default=False, keyword-only
mode : "ballistic" | "discrete", default="ballistic, keyword-only
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
Expand Down Expand Up @@ -686,7 +686,7 @@ def minimize(
agents=agents,
max_steps=max_steps,
best_only=best_only,
ballistic=ballistic,
mode=mode,
heated=heated,
minimize=True,
verbose=verbose,
Expand All @@ -705,7 +705,7 @@ def maximize(
agents: int = 128,
max_steps: int = 10_000,
best_only: bool = True,
ballistic: bool = False,
mode: Literal["ballistic", "discrete"] = "ballistic",
heated: bool = False,
verbose: bool = True,
use_window: bool = True,
Expand Down Expand Up @@ -768,7 +768,7 @@ def maximize(
polynomial at this vector. Otherwise, returns all the vectors
found by the SB algorithm and the values of polynomial at these
points.
ballistic : bool, default=False, keyword-only
mode : "ballistic" | "discrete", default="ballistic, keyword-only
Whether to use the ballistic or the discrete SB algorithm.
See Notes for further information about the variants of the SB
algorithm.
Expand Down Expand Up @@ -949,7 +949,7 @@ def maximize(
agents=agents,
max_steps=max_steps,
best_only=best_only,
ballistic=ballistic,
mode=mode,
heated=heated,
minimize=False,
verbose=verbose,
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_ising_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_ising():
agents=10,
verbose=False,
best_only=True,
ballistic=True,
mode="ballistic",
)
assert torch.equal(
torch.tensor([-1.0, 1.0, -1.0], dtype=torch.float32), spin_vector
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_knapsack():
"status": "not optimized",
}

model.minimize(ballistic=True, verbose=False, agents=1000)
model.minimize(mode="ballistic", verbose=False, agents=1000)
assert model.summary["items"] == [1, 2, 3, 4]
assert model.summary["total_cost"] == 15
assert model.summary["total_weight"] == 8
Expand Down
Loading
Loading