예제 #1
0
def build_model(args):
    feature_fn, margin_fn, num_features, actions = select_feature_fn(args)
    print 'Building model into {}.'.format(args.modelfile)
    if args.model == 'multi':
        mm_model = MultiSlackMaxMarginModel(actions, args.C, num_features, feature_fn, margin_fn)
    elif args.model == 'bellman':
        mm_model = BellmanMaxMarginModel(actions, args.C, args.D, args.F, 1, num_features, feature_fn, margin_fn) # changed
    else:
        mm_model = MaxMarginModel(actions, args.C, num_features, feature_fn, margin_fn)
    if not args.goal_constraints and args.model == 'bellman':
        demofile = h5py.File(args.demofile, 'r')
        ignore_keys = [k for k in demofile if demofile[k]['knot'][()]]
        demofile.close()
    else:
        ignore_keys = None
    mm_model.load_constraints_from_file(args.constraintfile, ignore_keys)
    mm_model.save_model(args.modelfile)
예제 #2
0
def optimize_model(args):
    feature_fn, margin_fn, num_features, actions = select_feature_fn(args)
    print 'Found model: {}'.format(args.modelfile)
    if args.model == 'multi':
        mm_model = MultiSlackMaxMarginModel.read(args.modelfile, actions, num_features, feature_fn, margin_fn)
    elif args.model == 'bellman':
        mm_model = BellmanMaxMarginModel.read(args.modelfile, actions, num_features, feature_fn, margin_fn)
        mm_model.D = args.D
        mm_model.F = args.F
    else:
        mm_model = MaxMarginModel.read(args.modelfile, actions, num_features, feature_fn, margin_fn)
    if args.save_memory:
        mm_model.model.setParam('threads', 1)  # Use single thread instead of maximum
        # barrier method (#2) is default for QP, but uses more memory and could lead to error
        mm_model.model.setParam('method', 1)  # Use dual simplex method to solve model
        #mm_model.model.setParam('method', 0)  # Use primal simplex method to solve model
    mm_model.C = args.C
    mm_model.optimize_model()
    mm_model.save_weights_to_file(args.weightfile)
예제 #3
0
def build_constraints(args):
    #test_features(args, "sc")
    test_features(args, "rope_dist")
    feature_fn, margin_fn, num_features, actions = select_feature_fn(args)
    print 'Building constraints into {}.'.format(args.constraintfile)
    if args.model == 'multi':
        mm_model = MultiSlackMaxMarginModel(actions, args.C, num_features, feature_fn, margin_fn)
    elif args.model == 'bellman':
        mm_model = BellmanMaxMarginModel(actions, args.C, args.D, args.F, .9, num_features, feature_fn, margin_fn)
    else:
        mm_model = MaxMarginModel(actions, args.C, num_features, feature_fn, margin_fn)
    if args.model == 'bellman':
        add_bellman_constraints_from_demo(mm_model,
                                          args.demofile,
                                          args.start, args.end,
                                          outfile=args.constraintfile,
                                          verbose=True)
    else:
        add_constraints_from_demo(mm_model,
                                  args.demofile,
                                  args.start, args.end,
                                  outfile=args.constraintfile,
                                  verbose=True)
예제 #4
0
def bellman_test_features(args):
    if args.model != 'bellman':
        raise Exception, 'wrong model for this'
    (feature_fn, margin_fn, num_features, actions) = select_feature_fn(args)
    demofile = h5py.File(args.demofile, 'r')
    # get a random set of trajectories
    trajectories = []
    traj = []
    for uid in range(len(demofile)):
        key = str(uid)
        group = demofile[key]
        state = [key,group['cloud_xyz'][:]] # these are already downsampled
        action = group['action'][()] if not group['knot'][()] else 'done'
        if group['pred'][()] == key:
            if traj:
                trajectories.append(traj)
                traj = []
        traj.append([state, action])
    if traj:
        trajectories.append(traj)
    random.shuffle(trajectories)
    constraint_trajs = trajectories[:args.num_constraints]
    # put them into a Bellman model
    mm_model = BellmanMaxMarginModel(action, 500, 1000, 10, .9, num_features, feature_fn, margin_fn)
    for i, t in enumerate(constraint_trajs):
        mm_model.add_trajectory(t, str(i), True)
    weights, w0 = mm_model.optimize_model()
    # evaluate value fn performance
    num_evals = len(trajectories) if args.num_evals < 0 else args.num_constraints + args.num_evals
    trajectories = trajectories[args.num_constraints:num_evals]
    values = np.zeros((len(trajectories), 6))
    num_decreases = 0
    for (i, t) in enumerate(trajectories):
        for j, (s, a) in enumerate(t):
            values[i, j] = np.dot(mm_model.weights, mm_model.feature_fn(s, a))
        for k in range(j):
            if values[i, k] > values[i, k+1]: 
                num_decreases += 1
                print "DECREASE", values[i, k], values[i, k+1]
        sys.stdout.write('num decreases:\t{} computed values for trajectory {}\r'.format(num_decreases, i))
        sys.stdout.flush()
    sys.stdout.write('\n')
    print "num decreases:\t", num_decreases
    ipy.embed()