simanneal
simanneal copied to clipboard
updates not printed out
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
The frequency of printing is determined by the updates
and steps
properties. What does oa.steps
and oa.updates
give you?
Somehow, the end="\r"
in the print statement of the default update function seemed to prevent it from printing on my system (macOS mojave). Removing the end="\r'
fixed the problem, at least for me.
@br1000
There is a known issue with PyCharm where it won't print properly with end='\r'
but will print strings containing '\r' just fine.
@perrygeo
Commit 2e79630 added end='\r'
but I don't think it is necessary since the string begins with '\r' anyway. Perhaps it could be changed to end=''
to help out PyCharm users?
Also, the annealing process seems to be run twice (once by auto and then again, for real). This leads to output like:
Temperature Energy Accept Improve Elapsed Remaining Temperature Energy Accept Improve Elapsed Remaining 7.30000 6801.84 5.60% 0.27% 0:00:11 0:00:00
If you were to change
print(' Temperature Energy Accept Improve Elapsed Remaining',
file=sys.stderr)
to
print('\n Temperature Energy Accept Improve Elapsed Remaining',
file=sys.stderr)
then the output would be:
empty line Temperature Energy Accept Improve Elapsed Remaining 4.90000 6801.84 4.60% 0.00% 0:00:03 -1:59:59 Temperature Energy Accept Improve Elapsed Remaining 4.90000 6845.62 6.81% 0.14% 0:00:09 0:00:00 which I think is a bit neater.
@Iain-S sounds like a good solution. Would you mind creating a PR for it?