Beispiel #1
0
def optimize(solver_config, constraints, default, call_log, maximize, max_evals):
    """Emulates :func:`optunity.optimize`."""
    replacements = comm._find_replacements(_illegal_keys, solver_config)
    solver_config = comm._replace_keys(solver_config, replacements)
    constraints = comm._replace_keys(constraints, replacements)

    # prepare objective function
    mgr = comm.EvalManager(replacements=replacements)
    func = prepare_fun(mgr, constraints, default, call_log)

    # make the solver
    try:
        solver = optunity.make_solver(**solver_config)
    except (ValueError, KeyError) as e:
        msg = {'error_msg': 'Unable to instantiate solver: ' + str(e)}
        comm.send(comm.json_encode(msg))
        print(solver_config, file=sys.stderr)
        exit(1)

    # solve problem
    try:
        solution, rslt = optunity.optimize(solver, func, maximize=maximize,
                                           max_evals=max_evals, pmap=mgr.pmap)
    except EOFError:
        msg = {'error_msg': 'Broken pipe.'}
        comm.send(comm.json_encode(msg))
        exit(1)

    # send solution and exit
    result = rslt._asdict()
    result['solution'] = solution
    result_json = comm.json_encode(result)
    comm.send(result_json)
    exit(0)
Beispiel #2
0
def model_selection():
    numparticles = 30
    num_generations = 10
    hpConstraints = {'logC':[-4, 3], 'logGamma':[-6, 0], 'logEpsilon':[-2, 1]}
    solver = optunity.solvers.ParticleSwarm.ParticleSwarm(num_particles, 
                                                    num_generations, 
                                                    max_speed=None, 
                                                    phi1=2.0, 
                                                    phi2=2.0,
                                                    hpConstraints)
    optimal_pars, _, _ = optunity.optimize(solver, svr_mse, 
                                           maximize = False, max_evals=300)
    print(optimal_pars)
    optimal_model = sklearn.svm.SVR(C=10 ** optimal_pars['logC'],
                                    gamma=10 ** optimal_pars['logGamma'], 
                                    epsilon=10 ** optimal_pars['logEpsilon']
                                    ).fit(features, target)
    joblib.dump(optimal_model, optimal_model_file)
Beispiel #3
0
def optimize(startin_point):
    print "\nBegin optimization"
    midpoint = startin_point
    constraints = {'lat':[midpoint[0]-0.1 , midpoint[0]+0.1], 'lon': [midpoint[1]-0.1 , midpoint[1]+0.1]}

    print "\tStarting from:\t\t", midpoint
    print "\tCurrent distance:\t", function_to_optimize(midpoint[0], midpoint[1])[0]

    for sname in optunity.available_solvers(): #['particle swarm']
        #create a solver
        suggestion = optunity.suggest_solver(num_evals=500, solver_name=sname, **constraints)
        solver = optunity.make_solver(**suggestion)

        #optimize the function
        optimum = optunity.optimize(solver, function_to_optimize, maximize=False, max_evals=100)

        print "\n\t==================================="
        print "\tSolver name:\t", suggestion['solver_name']
        print "\tMidpoint:\t", [optimum[0]['lat'], optimum[0]['lon']]
        print "\tDistance:\t", optimum[1][0][0]
        print "\tIterations:\t", optimum[1][1]['num_evals']
Beispiel #4
0
def optimize(solver_config, constraints, default, call_log, maximize,
             max_evals):
    """Emulates :func:`optunity.optimize`."""
    replacements = comm._find_replacements(_illegal_keys, solver_config)
    solver_config = comm._replace_keys(solver_config, replacements)
    constraints = comm._replace_keys(constraints, replacements)

    # prepare objective function
    mgr = comm.EvalManager(replacements=replacements)
    func = prepare_fun(mgr, constraints, default, call_log)

    # make the solver
    try:
        solver = optunity.make_solver(**solver_config)
    except (ValueError, KeyError) as e:
        msg = {'error_msg': 'Unable to instantiate solver: ' + str(e)}
        comm.send(comm.json_encode(msg))
        print(solver_config, file=sys.stderr)
        exit(1)

    # solve problem
    try:
        solution, rslt = optunity.optimize(solver,
                                           func,
                                           maximize=maximize,
                                           max_evals=max_evals,
                                           pmap=mgr.pmap)
    except EOFError:
        msg = {'error_msg': 'Broken pipe.'}
        comm.send(comm.json_encode(msg))
        exit(1)

    # send solution and exit
    result = rslt._asdict()
    result['solution'] = solution
    result_json = comm.json_encode(result)
    comm.send(result_json)
    exit(0)
Beispiel #5
0
#!/usr/bin/env python

# A simple smoke test for all available solvers.

import optunity

def f(x, y):
    return x + y

solvers = optunity.available_solvers()

for solver in solvers:
    # simple API
    opt, _, _ = optunity.maximize(f, 100,
                                  x=[0, 5], y=[-5, 5],
                                  solver_name=solver)

    # expert API
    suggestion = optunity.suggest_solver(num_evals=100, x=[0, 5], y=[-5, 5],
                                         solver_name=solver)
    s = optunity.make_solver(**suggestion)
    # without parallel evaluations
    opt, _ = optunity.optimize(s, f)
    # with parallel evaluations
    opt, _ = optunity.optimize(s, f, pmap=optunity.pmap)
Beispiel #6
0
# A simple smoke test for all available solvers.

import optunity


def f(x, y):
    return x + y


solvers = optunity.available_solvers()

for solver in solvers:
    # simple API
    opt, _, _ = optunity.maximize(f,
                                  100,
                                  x=[0, 5],
                                  y=[-5, 5],
                                  solver_name=solver)

    # expert API
    suggestion = optunity.suggest_solver(num_evals=100,
                                         x=[0, 5],
                                         y=[-5, 5],
                                         solver_name=solver)
    s = optunity.make_solver(**suggestion)
    # without parallel evaluations
    opt, _ = optunity.optimize(s, f)
    # with parallel evaluations
    opt, _ = optunity.optimize(s, f, pmap=optunity.pmap)
Beispiel #7
0
def main():

    # open a socket if port [+ host] specified in commandline args
    if len(sys.argv) > 1:
        if sys.argv[1] == 'server':
            port, server_socket = comm.open_server_socket()
            print(port)
            ## flush is needed for R pipe():
            sys.stdout.flush()
            comm.accept_server_connection(server_socket)

        else:
            try:
                port = int(sys.argv[1])
            except ValueError as e:
                print('Invalid socket port: ' + str(e))
                sys.exit(1)
            if len(sys.argv) > 2:
                host = sys.argv[2]
            else:
                host = 'localhost'
            comm.open_socket(port, host)

    startup_json = comm.receive()

    try:
        startup_msg = comm.json_decode(startup_json)

        if 'manual' in startup_msg:
            solver_name = startup_msg['manual']
            manual_request(solver_name)

        elif 'generate_folds' in startup_msg:
            import optunity.cross_validation as cv
            cv_opts = startup_msg['generate_folds']
            fold_request(cv_opts)

        elif 'make_solver' in startup_msg:
            solver_config = startup_msg['make_solver']
            replacements = comm._find_replacements(_illegal_keys,
                                                   solver_config)
            solver_config = comm._replace_keys(solver_config, replacements)
            make_solver(solver_config)

        elif 'maximize' in startup_msg or 'minimize' in startup_msg:
            if startup_msg.get('maximize', False):
                kwargs = startup_msg['maximize']
                solve_fun = optunity.maximize
            else:
                kwargs = startup_msg['minimize']
                solve_fun = optunity.minimize

            max_or_min(solve_fun, kwargs, startup_msg.get('constraints', {}),
                       startup_msg.get('default', None),
                       startup_msg.get('call_log', None))

        elif 'optimize' in startup_msg:
            max_evals = startup_msg['optimize'].get('max_evals', 0)
            maximize = startup_msg['optimize'].get('maximize', True)

            # sanity check
            if not 'solver' in startup_msg:
                msg = {'error_msg': 'No solver specified in startup message.'}
                comm.send(comm.json_encode(msg))
                print(startup_msg, file=sys.stderr)
                exit(1)

            optimize(startup_msg['solver'], startup_msg.get('constraints', {}),
                     startup_msg.get('default', None),
                     startup_msg.get('call_log', None), maximize, max_evals)

        else:  # solving a given problem
            mgr = comm.EvalManager()
            func = optunity.wrap_constraints(
                comm.make_piped_function(mgr),
                startup_msg.get('default', None),
                **startup_msg.get('constraints', {}))

            if startup_msg.get('call_log', False):
                func = optunity.wrap_call_log(func, startup_msg['call_log'])
            else:
                func = functions.logged(func)

            maximize = startup_msg.get('maximize', True)

            # instantiate solver
            try:
                solver = optunity.make_solver(startup_msg['solver'],
                                              **startup_msg['config'])
            except (ValueError, KeyError):
                msg = {'error_msg': 'Unable to instantiate solver.'}
                comm.send(comm.json_encode(msg))
                print(startup_msg, file=sys.stderr)
                exit(1)
            except EOFError:
                msg = {'error_msg': 'Broken pipe.'}
                comm.send(comm.json_encode(msg))
                exit(1)

            # solve and send result
            try:
                solution, rslt = optunity.optimize(solver,
                                                   func,
                                                   maximize,
                                                   pmap=mgr.pmap)
            except EOFError:
                msg = {'error_msg': 'Broken pipe.'}
                comm.send(comm.json_encode(msg))
                exit(1)

            result = rslt._asdict()
            result['solution'] = solution
            result_json = comm.json_encode(result)
            comm.send(result_json)
            exit(0)

    except (ValueError, TypeError, AttributeError) as e:
        msg = {'error_msg': str(e)}
        comm.send(comm.json_encode(msg))
        exit(1)
Beispiel #8
0
def main():

    # open a socket if port [+ host] specified in commandline args
    if len(sys.argv) > 1:
        if sys.argv[1] == 'server':
            port, server_socket = comm.open_server_socket()
            print(port)
            ## flush is needed for R pipe():
            sys.stdout.flush()
            comm.accept_server_connection(server_socket)

        else:
            try:
                port = int(sys.argv[1])
            except ValueError as e:
                print('Invalid socket port: ' + str(e))
                sys.exit(1)
            if len(sys.argv) > 2:
                host = sys.argv[2]
            else:
                host = 'localhost'
            comm.open_socket(port, host)

    startup_json = comm.receive()

    try:
        startup_msg = comm.json_decode(startup_json)

        if 'manual' in startup_msg:
            solver_name = startup_msg['manual']
            manual_request(solver_name)

        elif 'generate_folds' in startup_msg:
            import optunity.cross_validation as cv
            cv_opts = startup_msg['generate_folds']
            fold_request(cv_opts)

        elif 'make_solver' in startup_msg:
            solver_config = startup_msg['make_solver']
            replacements = comm._find_replacements(_illegal_keys, solver_config)
            solver_config = comm._replace_keys(solver_config, replacements)
            make_solver(solver_config)

        elif 'maximize' in startup_msg or 'minimize' in startup_msg:
            if startup_msg.get('maximize', False):
                kwargs = startup_msg['maximize']
                solve_fun = optunity.maximize
            else:
                kwargs = startup_msg['minimize']
                solve_fun = optunity.minimize

            max_or_min(solve_fun, kwargs,
                    startup_msg.get('constraints', {}),
                    startup_msg.get('default', None),
                    startup_msg.get('call_log', None))

        elif 'optimize' in startup_msg:
            max_evals = startup_msg['optimize'].get('max_evals', 0)
            maximize = startup_msg['optimize'].get('maximize', True)

            # sanity check
            if not 'solver' in startup_msg:
                msg = {'error_msg': 'No solver specified in startup message.'}
                comm.send(comm.json_encode(msg))
                print(startup_msg, file=sys.stderr)
                exit(1)

            optimize(startup_msg['solver'],
                    startup_msg.get('constraints', {}),
                    startup_msg.get('default', None),
                    startup_msg.get('call_log', None),
                    maximize, max_evals)

        else:  # solving a given problem
            mgr = comm.EvalManager()
            func = optunity.wrap_constraints(comm.make_piped_function(mgr),
                                            startup_msg.get('default', None),
                                            **startup_msg.get('constraints', {})
                                            )

            if startup_msg.get('call_log', False):
                func = optunity.wrap_call_log(func, startup_msg['call_log'])
            else:
                func = functions.logged(func)

            maximize = startup_msg.get('maximize', True)

            # instantiate solver
            try:
                solver = optunity.make_solver(startup_msg['solver'],
                                            **startup_msg['config'])
            except (ValueError, KeyError):
                msg = {'error_msg': 'Unable to instantiate solver.'}
                comm.send(comm.json_encode(msg))
                print(startup_msg, file=sys.stderr)
                exit(1)
            except EOFError:
                msg = {'error_msg': 'Broken pipe.'}
                comm.send(comm.json_encode(msg))
                exit(1)

            # solve and send result
            try:
                solution, rslt = optunity.optimize(solver, func, maximize,
                                                pmap=mgr.pmap)
            except EOFError:
                msg = {'error_msg': 'Broken pipe.'}
                comm.send(comm.json_encode(msg))
                exit(1)

            result = rslt._asdict()
            result['solution'] = solution
            result_json = comm.json_encode(result)
            comm.send(result_json)
            exit(0)

    except (ValueError, TypeError, AttributeError) as e:
        msg = {'error_msg': str(e)}
        comm.send(comm.json_encode(msg))
        exit(1)