Skip to content

Instantly share code, notes, and snippets.

@dandavison
Last active November 26, 2017 04:21
Show Gist options
  • Save dandavison/c9c1fed11d407dc4ee4126647ea14fde to your computer and use it in GitHub Desktop.
Save dandavison/c9c1fed11d407dc4ee4126647ea14fde to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from scipy.optimize import bisect
def record_optimization(func):
def recorder(self, *args, **kwargs):
if not hasattr(self, '_recorder_inputs'):
self._recorder_inputs = []
if not hasattr(self, '_recorder_outputs'):
self._recorder_outputs = []
self._recorder_inputs.append(args)
output = func(self, *args, **kwargs)
self._recorder_outputs.append(output)
return output
return recorder
class Optimizer():
def __init__(self):
self.target = 5.
def optimize(self):
print 'finding target...\nerror:',
solution = bisect(self.error_function, -100, 100, xtol=0.1)
print '\nsolution:', solution
@record_optimization
def error_function(self, x):
print x - self.target,
return x - self.target
optimizer = Optimizer()
optimizer.optimize()
print optimizer._recorder_inputs
print optimizer._recorder_outputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment