Example #1
0
def read_history(source):
    """read parameter history and cost history from the given source

source is a monitor, logfile, support file, solver restart file, dataset, etc
    """
    monitor = solver = data = arxiv = False
    from mystic.monitors import Monitor, Null
    from mystic.abstract_solver import AbstractSolver
    from mystic.math.legacydata import dataset
    from klepto.archives import archive, cache
    try:
        basestring
    except NameError:
        basestring = str
        import io
        file = io.IOBase
    if isinstance(source, file):
        return read_history(source.name)
    if isinstance(source, basestring):
        import re
        source = re.sub(r'\.py*.$', '', source)  # strip off .py* extension
    elif isinstance(source, Monitor):
        monitor = True
    elif isinstance(source, dataset):
        data = True
    elif isinstance(source, cache):
        source = source.__archive__
        arxiv = True
    elif isinstance(source, archive):
        arxiv = True
    elif isinstance(source, AbstractSolver):
        solver = True
    elif isinstance(source, Null):
        return [], [
        ]  #XXX: or source.x, source.y (e.g. Null(),Null())? or Error?
    else:
        raise IOError("a history filename or instance is required")
    try:  # read standard logfile (or monitor)
        if monitor:
            params, cost = read_monitor(source)
        elif data:
            params, cost = source.coords, source.values
        elif arxiv:
            params = list(list(k) for k in source.keys())
            cost = source.values()
            cost = cost if type(cost) is list else list(cost)
        elif solver:
            params, cost = source.solution_history, source.energy_history
        else:
            try:
                from mystic.solvers import LoadSolver
                return read_history(LoadSolver(source))
            except:  #KeyError
                _step, params, cost = logfile_reader(source)
                #FIXME: doesn't work for multi-id logfile; select id?
        params, cost = raw_to_support(params, cost)
    except:
        params, cost = read_import(source + '.py', 'params', 'cost')
    return params, cost
Example #2
0
def runme():
    # instantiate the solver
    _solver = NelderMeadSimplexSolver(3)
    lb, ub = [0., 0., 0.], [10., 10., 10.]
    _solver.SetRandomInitialPoints(lb, ub)
    _solver.SetEvaluationLimits(1000)
    # add a monitor stream
    stepmon = VerboseMonitor(1)
    _solver.SetGenerationMonitor(stepmon)
    # configure the bounds
    _solver.SetStrictRanges(lb, ub)
    # configure stop conditions
    term = Or(VTR(), ChangeOverGeneration())
    _solver.SetTermination(term)
    # add a periodic dump to an archive
    tmpfile = 'mysolver.pkl'
    _solver.SetSaveFrequency(10, tmpfile)
    # run the optimizer
    _solver.Solve(rosen)
    # get results
    x = _solver.bestSolution
    y = _solver.bestEnergy
    # load the saved solver
    solver = LoadSolver(tmpfile)
    #os.remove(tmpfile)
    # obligatory check that state is the same
    assert all(x == solver.bestSolution)
    assert y == solver.bestEnergy
    # modify the termination condition
    term = VTR(0.0001)
    solver.SetTermination(term)
    # run the optimizer
    solver.Solve(rosen)
    os.remove(tmpfile)
    # check the solver serializes
    _s = dill.dumps(solver)
    return dill.loads(_s)
Example #3
0
def read_history(source):
    """read parameter history and cost history from the given source

'source' can be a monitor, logfile, support file, or solver restart file
    """
    monitor = solver = False
    from mystic.monitors import Monitor, Null
    from mystic.abstract_solver import AbstractSolver
    try:
        basestring
    except NameError:
        basestring = str
        import io
        file = io.IOBase
    if isinstance(source, file):
        return read_history(source.name)
    if isinstance(source, basestring):
        import re
        source = re.sub('\.py*.$', '', source)  # strip off .py* extension
    elif isinstance(source, Monitor):
        monitor = True
    elif isinstance(source, AbstractSolver):
        solver = True
    elif isinstance(source, Null):
        return [], [
        ]  #XXX: or source.x, source.y (e.g. Null(),Null())? or Error?
    else:
        raise IOError("a history filename or instance is required")
    try:  # read standard logfile (or monitor)
        if monitor:
            params, cost = read_monitor(source)
        elif solver:
            params, cost = source.solution_history, source.energy_history
        else:
            try:
                from mystic.solvers import LoadSolver
                return read_history(LoadSolver(source))
            except:  #KeyError
                _step, params, cost = logfile_reader(source)
                #FIXME: doesn't work for multi-id logfile; select id?
        params, cost = raw_to_support(params, cost)
    except:
        params, cost = read_import(source + '.py', 'params', 'cost')
    return params, cost
Example #4
0
import os
import sys

is_pypy = hasattr(sys, 'pypy_version_info')
if is_pypy:
    print('Skipping: test_solver_sanity.py')
    exit()

solver = PowellDirectionalSolver(3)
solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.])
term = VTR()
solver.Solve(rosen, term)
x = solver.bestSolution
y = solver.bestEnergy
assert solver._state == None
assert LoadSolver(solver._state) == None

solver = PowellDirectionalSolver(3)
solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.])
term = VTR()
tmpfile = 'mysolver.pkl'
solver.SetSaveFrequency(10, tmpfile)
solver.Solve(rosen, term)
x = solver.bestSolution
y = solver.bestEnergy
_solver = LoadSolver(tmpfile)
os.remove(tmpfile)
assert all(x == _solver.bestSolution)
assert y == _solver.bestEnergy

solver = NelderMeadSimplexSolver(3)