Skip to main content

doc License: GPL v3

Status

pytests push-pypi push-doc

maintained issues pr

Compatibilities

ubuntu unix

python

Contact

linkedin website mail

AdAnnealing

A package doing simulated annealing

Installation

git clone https://github.com/pcotteadvestis/adannealing
cd adannealing
pip install .

Usage

Simple usage :

from adannealing import Annealer

class LossFunc2D:
    def __init__(self):
        self.constraints = None

    def __call__(self, w) -> float:
        """
        A __call__ method must be present. It will be called to evaluate the loss. The argument passed is the 
        parameter value at which the loss has to be computed.
        """
        x = w[0]
        y = w[1]
        return (x - 5) * (x - 2) * (x - 1) * x + 10 * y ** 2

    def on_fit_start(self, val):
        """
        This method is called by the fitter before optimisation. The argument passed is either the starting point of the
        optimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used
        """
        pass

    def on_fit_end(self, val):
        """
        This method is called by the fitter after optimisation. The argument passed is either the result of the 
        optimiser (for single annealer) or the list of results if more than one annealer reache the end of fit.
        """
        pass

init_states, bounds, acceptance = (3.0, 0.5), np.array([[0, 5], [-1, 1]]), 0.01

ann = Annealer(
    loss=LossFunc2D(),
    weights_step_size=0.1,
    init_states=init_states,  # Optional
    bounds=bounds,
    verbose=True
)

# Weights of local minimum, and loss at local minimum
w0, lmin, _, _, _, _ = ann.fit(stopping_limit=acceptance)

Use multiple initial states in parallel runs and get one output per init states :

from adannealing import Annealer

Annealer.set_parallel()


class LossFunc2D:
    def __init__(self):
        self.constraints = None

    def __call__(self, w) -> float:
        """
        A __call__ method must be present. It will be called to evaluate the loss. The argument passed is the 
        parameter value at which the loss has to be computed.
        """
        x = w[0]
        y = w[1]
        return (x - 5) * (x - 2) * (x - 1) * x + 10 * y ** 2

    def on_fit_start(self, val):
        """
        This method is called by the fitter before optimisation. The argument passed is either the starting point of the
        optimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used
        """
        pass

    def on_fit_end(self, val):
        """
        This method is called by the fitter after optimisation. The argument passed is either the result of the 
        optimiser (for single annealer) or the list of results if more than one annealer reache the end of fit.
        """
        pass

bounds, acceptance, n =  np.array([[0, 5], [-1, 1]]), 0.01, 5

ann = Annealer(
    loss=LossFunc2D(),
    weights_step_size=0.1,
    bounds=bounds,
    verbose=True
)

# Iterable of n weights of local minimum and loss at local minimum
results = ann.fit(npoints=n, stopping_limit=acceptance)
for w0, lmin, _, _, _, _ in results:
    """do something"""

Use multiple initial states in parallel runs and get the result with the smallest loss :

from adannealing import Annealer

Annealer.set_parallel()


class LossFunc2D:
    def __init__(self):
        self.constraints = None

class LossFunc2D:
    def __init__(self):
        self.constraints = None

    def __call__(self, w) -> float:
        """
        A __call__ method must be present. It will be called to evaluate the loss. The argument passed is the 
        parameter value at which the loss has to be computed.
        """
        x = w[0]
        y = w[1]
        return (x - 5) * (x - 2) * (x - 1) * x + 10 * y ** 2

    def on_fit_start(self, val):
        """
        This method is called by the fitter before optimisation. The argument passed is either the starting point of the
        optimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used
        """
        pass

    def on_fit_end(self, val):
        """
        This method is called by the fitter after optimisation. The argument passed is either the result of the 
        optimiser (for single annealer) or the list of results if more than one annealer reache the end of fit.
        """
        pass

bounds, acceptance, n = np.array([[0, 5], [-1, 1]]), 0.01, 5

ann = Annealer(
    loss=LossFunc2D(),
    weights_step_size=0.1,
    bounds=bounds,
    verbose=True
)

# Weights of the best local minimum and loss at the best local minimum
w0, lmin, _, _, _, _ = ann.fit(npoints=n, stopping_limit=acceptance, stop_at_first_found=True)

One can save the history of the learning by giving a path :

from adannealing import Annealer

Annealer.set_parallel()

class LossFunc2D:
    def __init__(self):
        self.constraints = None

    def __call__(self, w) -> float:
        """
        A __call__ method must be present. It will be called to evaluate the loss. The argument passed is the 
        parameter value at which the loss has to be computed.
        """
        x = w[0]
        y = w[1]
        return (x - 5) * (x - 2) * (x - 1) * x + 10 * y ** 2

    def on_fit_start(self, val):
        """
        This method is called by the fitter before optimisation. The argument passed is either the starting point of the
        optimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used
        """
        pass

    def on_fit_end(self, val):
        """
        This method is called by the fitter after optimisation. The argument passed is either the result of the 
        optimiser (for single annealer) or the list of results if more than one annealer reache the end of fit.
        """
        pass


bounds, acceptance, n = np.array([[0, 5], [-1, 1]]), 0.01, 5

ann = Annealer(
    loss=LossFunc2D(),
    weights_step_size=0.1,
    bounds=bounds,
    verbose=True
)

# Weights of the best local minimum and loss at the best local minimum
w0, lmin, _, _, _, _ = ann.fit(
    npoints=n,
    stopping_limit=acceptance,
    history_path="logs"
)

In this example, calling fit will produce n directories in logs, each containing 2 files: history.csv and returns.csv. The first is the entier history of the fit, the second is only the iteration that found the local minimum. If only one point is asked (either by using npoints=1 or stop_at_first_found=True), will produce history.csv and returns.csv directly in logs, and will delete the subfolders of the runs that did not produce the local minimum.

One can plot the result of a fit by doing

# figure will be saved in logs/annealing.pdf
fig = ann.plot("logs", nweights=2, weights_names=["A", "B", "C"], do_3d=True)

If the argument do_3d=True, then 3-dimensional dynamical figures are produced to inspect the phase space marginalised over different couples of components.

Metadata

Release files for adannealing 0.1.78

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adannealing 0.1.78
File Size Uploaded
adannealing-0.1.78.tar.gz 23.1 MB Details

Release files / adannealing-0.1.78.tar.gz

Download URL adannealing-0.1.78.tar.gz
Size 23.1 MB
Tags Source
SHA-256 checksum
How to use checksums
f674646d6a209970a0747dcbe25242b8ce694314332907b030b1d31cb1b74545
BLAKE2b-256 checksum
How to use checksums
76b4c1eb337f27e7feb92794b5aad8c7095d05f92edd4e762555b578226d8920
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.1.78 This release

1 release file

0.1.74

1 release file

0.1.32

1 release file

0.1.31

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.4

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page