Ejemplo n.º 1
0
def run(Args):
    specs = read_specs(path_to_specs)
    f, fjacx, fjacp = get_dynamics(specs)

    if specs.get('generate_twin', False) is True:
        generate_twin(specs, f)

    optimizer = specs.get('optimizer', 'IPOPT')
    print_level = 0 if charm else 5

    if optimizer == 'IPOPT':
        opt_options = {
            'print_level': print_level,
            'max_iter': max_iter_per_step,  # Set the max number of iterations
            # 'derivative_test' : 'first-order', # set derivative test
            'tol': tol_per_step,  # set termination criteria
            'linear_solver': 'mumps',
            'output_file': specs['data_folder'] + 'IPOPT.out'
        }
    if optimizer == 'SNOPT':
        from snopt import SNOPT_options
        opt_options = SNOPT_options()
        opt_options.setOption('Print filename',
                              specs['data_folder'] + 'SNOPT.out')
        opt_options.setOption('Iteration limit', max_iter_per_step)
        opt_options.setOption('Optimality tolerance', tol_per_step)
        if charm:
            opt_options.setOption('Major print level', 0)
            opt_options.setOption('Minor print level', 0)
            opt_options.setOption('Verbose', False)

    obs_dim = specs['obs_dim'] if specs['obs_dim'] != -1 else np.arange(
        specs['num_dims'])

    params = {
        'name': specs['name'],
        'data_folder': specs['data_folder'],
        'data_file': specs['data_file'],
        'stim_file': specs.get('stim_file'),
        'nstart': specs.get('start_data', 0),
        'ndata': specs.get('num_data', -1),
        'f': f,
        'fjacx': fjacx,
        'fjacp': fjacp,
        'alpha': specs['alpha'],
        'max_beta': specs['max_beta'],
        'Rm': specs['RM'],
        'Rf0': specs['RF0'],
        'Lidx': obs_dim,
        'dt_model': specs['time_step'],
        'optimizer': optimizer,
        'opt_options': opt_options,
        'num_pars': specs['num_par'],
        'num_dims': specs['num_dims'],
        'bounds': read_bounds(path_to_params),
    }

    rng = np.random.default_rng()
    ss = rng.bit_generator._seed_seq
    init_seeds = ss.spawn(num_init_cond)
    if charm:
        sol = np.array(charm.pool.map(partial(min_action, params=params),
                                      init_seeds),
                       dtype=object)
        np.savez(specs['data_folder'] + specs['name'] + '_results.npz',
                 path=np.array(sol[:, 0]),
                 params=np.array(sol[:, 1]),
                 action=np.array(sol[:, 2]),
                 time=np.array(sol[0, 3]),
                 **specs)
    else:
        sol = min_action(init_seeds[0], params)
        np.savez(specs['data_folder'] + specs['name'] + '_results.npz',
                 path=sol[0],
                 params=sol[1],
                 action=sol[2],
                 time=sol[3],
                 **specs)

    exit()
Ejemplo n.º 2
0
    # Nonlinear objective term only
    sumx = x[0] + x[1] + x[2]
    fObj = 0.0
    fObj = sumx**2

    #gObj[0] = 2.0*sum
    #gObj[1] = 2.0*sum
    #gObj[2] = 2.0*sum

    return mode, fObj, gObj, fCon, gCon


options = SNOPT_options()
inf = 1.0e+20

options.setOption('Infinite bound', inf)
options.setOption('Verify level', 3)
options.setOption('Print filename', 'sntoyc.out')

m = 4
n = 4

nnCon = 2
nnJac = 2
nnObj = 3

x0 = np.zeros(n + m, float)

# J contains the sparsity pattern of the Jacobian matrix.
# For nonlinear elements, enter any nonzero number (in this case 100).
# Linear elements must be correctly defined.
Ejemplo n.º 3
0
import numpy as np
import scipy.sparse as sp
from   snopt import snopta, SNOPT_options


def dieta_fun(status,x,F,G,needF,needG):
    # LP has no nonlinear terms in the objective
    F = []
    G = []
    return status, F, G


inf     = 1.0e20
options = SNOPT_options()

options.setOption('Print filename','dieta.out')
options.setOption('Minor print level',1)
options.setOption('Summary frequency',1)
options.setOption('Print frequency',1)

n       = 6
nF      = 4
ObjRow  = 4

# We provide the linear components of the Jacobian
# matrix as a dense matrix.
A       = np.array([ [110, 205, 160, 160, 420, 260],
                     [  4,  32,  13,   8,   4, 14],
                     [  2,  12,  54, 285,  22, 80],
                     [  3,  24,  13,   9,  20, 19]],float)
Ejemplo n.º 4
0
    gObj[1] = -x[5]
    gObj[2] = -x[6] + x[7]
    gObj[3] = x[8]
    gObj[4] = -x[7]
    gObj[5] = -x[1]
    gObj[6] = -x[2] + x[0]
    gObj[7] = -x[4] + x[2]
    gObj[8] = x[3]

    return mode, fObj, gObj


inf = 1.0e+20

options = SNOPT_options()
options.setOption('Infinite bound', inf)
options.setOption('Specs filename', 'snmainb.spc')
options.setOption('Print filename', 'snmainb.out')

m = 18
n = 9
nnCon = 14
ne = 52
nnJac = n
nnObj = n

bl = -inf * np.ones(n + m)
bu = inf * np.ones(n + m)

# Nonlinear constraints
bu[1 + n:nnCon + n] = 1.0
Ejemplo n.º 5
0
        fObj = 100.0 * (x[1] - x[0]**2)**2 + (1.0 - x[0])**2

    if mode == 0:
        return mode, fObj

    if mode == 1 or mode == 2:
        gObj[0] = -400.0 * (x[1] - x[0]**2) * x[0] - 2.0 * (1.0 - x[0])
        gObj[1] = 200.0 * (x[1] - x[0]**2)

    return mode, fObj, gObj


options = SNOPT_options()
inf = 1.0e+20

options.setOption('Infinite bound', inf)
options.setOption('Verify level', 3)
options.setOption('Verbose', True)
options.setOption('Print filename', 't2banana.out')

m = 1
n = 2

nnCon = 0
nnJac = 0
nnObj = n

x0 = np.zeros(n + m, float)
x0[0] = 1.2
x0[1] = 1.0
Ejemplo n.º 6
0
al = np.array([
    -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0, -7.0,
    60.0, 50.0, 70.0, 85.0, 100.0
])

au = np.array([
    6.0, 6.0, 6.0, 6.0, 7.0, 7.0, 7.0, 7.0, 6.0, 6.0, 6.0, 6.0, inf, inf, inf,
    inf, inf
])

c = np.array([
    2.3, 1.7, 2.2, 2.3, 1.7, 2.2, 2.3, 1.7, 2.2, 2.3, 1.7, 2.2, 2.3, 1.7, 2.2
])

# Solve the problem
options = SNOPT_options()
options.setOption('Print frequency', 1)
options.setOption('Summary frequency', 1)

result = sqopt(userHx,
               x0,
               xl=xl,
               xu=xu,
               A=A,
               al=al,
               au=au,
               c=c,
               name='HS118   ',
               options=options)
Ejemplo n.º 7
0
    F[0] = x[1]  # objective row
    F[1] = x[0]**2 + 4.0 * x[1]**2
    F[2] = (x[0] - 2.0)**2 + x[1]**2

    G[0] = 2 * x[0]
    G[1] = 8 * x[1]
    G[2] = 2 * (x[0] - 2)
    G[3] = 2 * x[1]

    return status, F, G


inf = 1.0e20
options = SNOPT_options()

options.setOption('Verbose', True)
options.setOption('Solution print', True)
options.setOption('Print filename', 'sntoya.out')

options.setOption('Summary frequency', 1)

# Name arrays have to be dtype='|S1' and also have to be the
# correct length, else they are ignored by SNOPT:
xnames = np.empty(2, dtype='|S8')
xnames[0] = "      x0"
xnames[1] = "      x1"

Fnames = np.empty(3, dtype='|S8')
Fnames[0] = "      F0"
Fnames[1] = "      F1"
Fnames[2] = "      F2"