perrygeo / simanneal Goto Github PK
View Code? Open in Web Editor NEWPython module for Simulated Annealing optimization
License: ISC License
Python module for Simulated Annealing optimization
License: ISC License
self.move() in the the tsp example returns self.energy() or E, but should return dE shouldn't it?
it took me a while to track this down, but anneal() blows up when self.move() returns E instead of dE.
Hi!
When I run the optimization algorithm, the table gets stuck like this:
I've seen a similar issue, but it was related to PyCharm and I use Emacs.
Changing the update method so that it simply prints *args and **kwargs simply prints 0 0.0 0.0 None None
once. So it seems the code is stuck at the first iteration. The code keeps running even after half an hour or more, even though I used auto(minutes=0.2)
.
Further, adding some prints to my energy function shows that the energy function is being called over and over. Its value does change, and so do the state parameters.
Thanks!
Why is the cooling process written in the internal cycle?
Currently, if your objective function relies on any external data it must be defined in the enclosing scope. It would be nice to allow required data to be specified with __init__
. Maybe that just means testing and documenting this technique...
class Optimizer(Annealer):
def __init__(self, state, moredata, extradata):
self.moredata = moredata
self.extradata = extradata
super(Optimizer, self).__init__(state)
hello,
I'm trying out simanneal
, and found something that I don't understand:
here's my code:
oa = OptimizerAnnealer(strategy, ranges, self.get_data())
configuration, points = oa.anneal()
where my annealer is:
def __init__(self, strategy, ranges, data):
self.strategy = strategy
self.ranges = ranges
self.data = data
# initial state with first value of each range
self.state = {}
for key, value in self.ranges.items():
self.state[key] = value[0]
super(OptimizerAnnealer, self).__init__(self.state) # important!
def move(self):
key = pick(list(self.state))
new_value = following(list(self.ranges[key]), self.state[key])
self.state[key] = new_value
def energy(self):
# energy function calculates points related to current state, obtaining a `result` float
# that depends on state.
return result
the output of simanneal
is:
Temperature Energy Accept Improve Elapsed Remaining
25000.00000 616.99 0:00:02
and that's it, it doesn't show any updates at all. even though there are new solutions that have lower energy level.
anything I'm doing wrong?
Thanks
Thank you so much for providing an amazing package.
I want to know which literature you have referenced to implement the simulated annealing algorithm.
Because I checked the original related literature of this algorithm, it usually contains two loops: an inner loop and an outer loop.
I want to know more about this literature .
E.g: Gendreau, Michel, Jean-Yves Potvin, and others. Handbook of Metaheuristics. Vol. 2. Springer, 2010.
Hi. I'm using the simulated annealing part several times in my scripts. The console output is too long for my task, so I changed the print statements in lines 86 et seqq. to the following:
sys.stdout.write('\r%12.2f %12.2f %s ' % \
(T, E, time_string(elapsed)))
sys.stdout.flush()
and
sys.stdout.write('\r%12.2f %12.2f %7.2f%% %7.2f%% %s %s' % \
(T, E, 100.0 * acceptance, 100.0 * improvement,
time_string(elapsed), time_string(remain))),
sys.stdout.flush()
By doing so, previous outputs are replaced by the most recent output.
I could use type numpy.ndarray or pandas.DataFrame for state variable from #12.
Unfortunately, the patch of #12 was canceled by ed97524.
There seems to be a little confusion.
I have a suggestion about this code.
class Annealer(object):
def __init__(self, initial_state=None, load_state=None):
if initial_state:
self.state = self.copy_state(initial_state)
elif load_state:
with open(load_state, 'rb') as fh:
self.state = pickle.load(fh)
else:
raise ValueError('No valid values supplied for neither \
initial_state nor load_state')
To start annealing, I think only two things are required in __init__()
.
First, either initial_state
or load_state
is given.
Second, the given object is copyable. The definition of "copyable" can be custmized by overriding copy_state()
.
Therefore, all we have to do in __init__()
is to check whether initial_state
is given or not.
Other conditions about state can be handled by move()
and energey()
, so it doesn't matter if initial_state
itself is evaluated as false like an empty list.
I'm wondering how can we do to leverage multiple CPUs when doing the annealing algorithm? I try to use GPU for quicker calculation, but it seems does not offer any help :(.
I notice that when I run this code, it only takes up 1 CPU, but my other learning algorithm like simple neural network can take up multiple CPUs to accelerate the calculation.
Thanks for any reply.
Cheers.
Hi
I think it would be a good thing to have access to the current temperature in the move function, either by passing it as a parameter or by making it a class member.
It would allow for smaller changes in the state when the temperature is small for example
This algorithm can be quite time consuming, and it could benefit greatly from multithreading,
I'm not sure how this could be achieved, in sight of the fact that the original algorithm considers just one solution per time, maybe, because the next moves are independent from the next temperature, one could calculate some of the next moves, so that the results might come much faster.
of course there might be some wasted computation, but there would be a good improvement on running speed regardless.
your thoughts?
Thanks
Hi,
Does this code support multi-objective simulated annealing when objectives are multiple different independent functions? Is there any sample or hint on which parts of anneal.py should be modified? I would appreciate your guidance on this and thanks for sharing this repository.
First thank you for this implementation of SA! Its flexibility allowed me to get really good results in a few hours on the Quadratic Knapsack Problem by implementing two simple moves.
I was wondering if the auto scheduling from below is something you came up on your own or something which can be found in the literature? If it is from the literature, could you give me the references?
Line 238 in 951e7d8
Thank you for your time.
class Optimizer(Annealer, AnnealGraph):
...
Would override the .update
method to generate a matplotlib plot to graphically show the progress of the simulated annealing.
I'm not sure how this can happen, but I'm looking at it in my terminal output, so I can at least say that it did happen!
File "/home/terry/s/net/simanneal/simanneal/anneal.py", line 211, in anneal
if dE > 0.0 and math.exp(-dE / T) < random.random():
ZeroDivisionError: float division by zero
So it seems T=0
, which, from code about 10 lines above, would mean that self.Tmax
would need to be zero. I'm running using the auto
method and set_schedule
. So maybe when auto
sets Tmax
to be the result of running round_figures
the value is zero?
copy_state()
returns None
when self.copy_strategy
is mistakenly set up.
Then the error message is not easy to understand.
For example, when I set tsp.copy_strategy = "copy"
instead of "slice"
in example/salesman.py,
the output was like this.
$ python examples/salesman.py
Temperature Energy Accept Improve Elapsed Remaining
Traceback (most recent call last): 0:00:00
File "examples/salesman.py", line 88, in <module>
state, e = tsp.anneal()
File "../simanneal/simanneal/anneal.py", line 192, in anneal
self.move()
File "examples/salesman.py", line 31, in move
a = random.randint(0, len(self.state) - 1)
TypeError: object of type 'NoneType' has no len()
It took time to know where the 'NoneType' came from.
It would be useful if we can see the cause is tsp.copy_strategy
.
How do you unit test a stochastic process? ... need to do some research.
In method, there is a problem:
def update(self, *args, **kwargs):
self.default_update(self,*args, **kwargs)
It should be:
def update(self, *args, **kwargs):
self.default_update(*args, **kwargs)
Running .auto() in a script output the following
Temperature Energy Accept Improve Elapsed Remaining
0.21 21.00 100.00% 0.00% 0:00:10 -1:59:50
{u'tmax': 0.21, u'tmin': 0.21, u'steps': 310000000.0}
I'm new to all of this .. Just wondering if this is a bug, or if it can make sense sometimes ?
I'm a very beginner in Python and want to know if it's possible to do an optimization for n-dimensions Rosenbrock's function with this package
It's sad that the part of "class TravellingSalesmanProblem" in example is too difficult for me to understand and therefore couldn't edit it as a suitable form for continuous problem
Here is an example I wrote to define 3D-Rosenbrock's function and the initial solution, initial objective value for SA:
`def rosen(x):
"""The Rosenbrock function"""
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
X0 = np.array([2, 1, 1.5])
of_int = rosen(X0)`
Hope someone could help me with an easy example!
Thanks in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.