Example #1
0
import os
from neuromancer import arg
import argparse
from neuromancer import datasets

parser = argparse.ArgumentParser()
parser.add_argument('-gpu', type=int, default=None)
args = parser.parse_args()
if args.gpu is not None:
    gpu = f'-gpu {args.gpu}'

p = arg.ArgParser(parents=[
    arg.log(),
    arg.opt(),
    arg.data(),
    arg.loss(),
    arg.lin(),
    arg.ssm()
])
options = {
    k: v.choices
    for k, v in p._option_string_actions.items()
    if v.choices is not None and k != '-norm'
}
systems = list(datasets.systems.keys())
for i, (k, v) in enumerate(options.items()):
    for j, opt in enumerate(v):
        print(k, opt)
        code = os.system(
            f'python ../train_scripts/system_id.py -norm Y '
            f'{k} {opt} -epochs 1 -nsteps 8 -verbosity 1 -nsim 128 -system {systems[(i*j) % len(systems)]}'
Example #2
0
    if umax is not None:
        u_max = umax * np.ones([nstep + 1, umax.shape[0]])
        ax[1].plot(u_max, 'k--', linewidth=2)
    ax[1].set(xlabel='time')
    ax[1].grid()
    ax[1].set_xlim(0, nstep)
    plt.tight_layout()
    if save_path is not None:
        plt.savefig(save_path + '/closed_loop_quadcopter_dpc.pdf')


if __name__ == "__main__":
    """
    # # #  Arguments
    """
    parser = arg.ArgParser(parents=[arg.log(), arg_dpc_problem()])
    args, grps = parser.parse_arg_groups()
    args.bias = True
    """
    # # # 3D quadcopter model 
    """

    # Discrete time model of a quadcopter
    A = np.array(
        [[1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0., 0., 0.],
         [0., 1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0., 0.],
         [0., 0., 1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0.],
         [0.0488, 0., 0., 1., 0., 0., 0.0016, 0., 0., 0.0992, 0., 0.],
         [0., -0.0488, 0., 0., 1., 0., 0., -0.0016, 0., 0., 0.0992, 0.],
         [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.0992],
         [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
Example #3
0
        "Y_max": np.ones([nsim, ny]),
        "Y_min": 0.7 * np.ones([nsim, ny]),
        "U_max": np.ones([nsim, nu]),
        "U_min": np.zeros([nsim, nu]),
        "R": psl.Periodic(nx=ny, nsim=nsim, numPeriods=20, xmax=0.9, xmin=0.8)[:nsim, :]
        # 'Y_ctrl_': psl.WhiteNoise(nx=ny, nsim=nsim, xmax=[1.0] * ny, xmin=[0.0] * ny)
    })
    # indices of controlled states, e.g. [0, 1, 3] out of 5 outputs
    dataset.ctrl_outputs = args.controlled_outputs
    return dataset


if __name__ == "__main__":
    # for available systems in PSL library check: psl.systems.keys()
    system = 'CSTR'         # keyword of selected system
    parser = arg.ArgParser(parents=[arg.log(), arg.log(prefix='sysid_'),
                                    arg.opt(), arg.data(system=system),
                                    arg.loss(), arg.lin(), arg.policy(),
                                    arg.ctrl_loss(), arg.freeze()])
    path = './test/CSTR_best_model.pth'
    parser.add('-model_file', type=str, default=path,
               help='Path to pytorch pickled model.')

    args = parser.parse_args()
    args.savedir = 'test_control'

    log_constructor = MLFlowLogger if args.logger == 'mlflow' else BasicLogger
    metrics = ["nstep_dev_loss", "loop_dev_loss", "best_loop_dev_loss",
               "nstep_dev_ref_loss", "loop_dev_ref_loss"]
    logger = log_constructor(args=args, savedir=args.savedir, verbosity=args.verbosity, stdout=metrics)
    # evaluate conficence level given risk tolerance epsilon
    confidence = 1-2*np.exp(-2*samples*epsilon**2)
    mu = mu_tilde-epsilon
    # evaluate risk lower bound given number of samples, and desired confidence
    mu_lbound = mu_tilde - np.sqrt(-np.log(delta/2)/(2*samples))

    return mu, mu_tilde, confidence, mu_lbound, \
           I_s, I_c, X_c_mean, U_c_mean, X_norm, U_norm


if __name__ == "__main__":

    """
    # # #  Arguments
    """
    parser = arg.ArgParser(parents=[arg.log(),
                                    arg_dpc_problem()])
    args, grps = parser.parse_arg_groups()
    args.bias = True

    """
    # # #  VTOL aircraft model 
    """
    # System parameters
    m = 4  # mass of aircraft
    J = 0.0475  # inertia around pitch axis
    r = 0.25  # distance to center of force
    g = 9.8  # gravitational constant
    c = 0.05  # damping factor (estimated)
    # State space dynamics
    xe = [0, 0, 0, 0, 0, 0]  # equilibrium point of interest