sho-lesson/sho/func.py
nojhan 3928be07a0 Add experimental stuff
- add ecdf module
- add expe module
- add func module
- use Dump wrapper around obj. func. instead of iters.
- add random solvers in snp options.
- add no-plot option in snp.
2019-01-20 22:03:45 +01:00

26 lines
943 B
Python

########################################################################
# Wrappers around objective functions
########################################################################
class Dump:
"""A wrapper around an objective function that
dumps a line in a file every time the objective function is called."""
def __init__(self, func, filename="run.csv", fmt="{it} ; {val} ; {sol}\n", sepsol=" , "):
self.func = func
self.filename = filename
self.fmt = fmt
self.sepsol = sepsol
self.counter = 0
# Erase previous file.
with open(self.filename, 'w') as fd:
fd.write("")
def __call__(self, sol):
val = self.func(sol)
self.counter += 1
with open(self.filename, 'a') as fd:
fmtsol = self.sepsol.join([str(i) for i in sol])
fd.write( self.fmt.format(it=self.counter, val=val, sol=fmtsol) )
return val