def apply_flow(phi, u): surf = sqp_problem.make_interp(phi) out = np.empty_like(phi) for i in range(out.shape[0]): for j in range(out.shape[1]): out[i,j] = surf.eval_xys(surf.to_xys([i,j])[0] - u[i,j,:]) return out
def __init__(self, init_phi): self.phi_surf = sqp_problem.make_interp(init_phi) self.curr_u = np.zeros(sqp_problem.Config.GRID_SHAPE + (2, )) self.curr_obs = None self.prob = None self.phi_cmap = np.zeros((256, 3), dtype='uint8') self.phi_cmap[:128, 0] = 255 self.phi_cmap[:128, 1] = np.linspace(0, 255, 128).astype(int) self.phi_cmap[:128, 2] = np.linspace(0, 255, 128).astype(int) self.phi_cmap[128:, 0] = np.linspace(255, 0, 128).astype(int) self.phi_cmap[128:, 1] = np.linspace(255, 0, 128).astype(int) self.phi_cmap[128:, 2] = 255
def __init__(self, init_phi): self.phi_surf = sqp_problem.make_interp(init_phi) self.curr_u = np.zeros(sqp_problem.Config.GRID_SHAPE + (2,)) self.curr_obs = None self.prob = None self.phi_cmap = np.zeros((256, 3), dtype='uint8') self.phi_cmap[:128,0] = 255 self.phi_cmap[:128,1] = np.linspace(0, 255, 128).astype(int) self.phi_cmap[:128,2] = np.linspace(0, 255, 128).astype(int) self.phi_cmap[128:,0] = np.linspace(255, 0, 128).astype(int) self.phi_cmap[128:,1] = np.linspace(255, 0, 128).astype(int) self.phi_cmap[128:,2] = 255
def apply_flow_forwards(fig, N, phi, u, x=None, y=None, z=None): # x goes to x + u(x) # so we want out(x+u(x)) == phi(x) surf = sqp_problem.make_interp(phi) pts = np.empty((phi.shape + (2,))) if x is None or y is None or z is None: x, y, z = [], [], [] for i in range(phi.shape[0]): for j in range(phi.shape[1]): p = surf.to_xys([i,j])[0] + u[i,j,:] x.append(p[0]) y.append(p[1]) z.append(phi[i,j]) ax = fig.add_subplot(N, aspect='equal') #pts = pts.reshape((-1,2)) #ax.scatter(pts[:,0], pts[:,1], c=phi.reshape(-1), cmap='Greys_r') ax.scatter(x, y, c=z, cmap='Greys_r')
def optimize_sqp(self): if self.prob is None: self.prob = sqp_problem.TrackingProblem() self.prob.set_coeffs(flow_agree=0) self.prob.set_obs_points(self.curr_obs) self.prob.set_prev_phi_surf(self.phi_surf) self.phi_surf, self.curr_u = self.prob.optimize(self.prob.prev_phi_surf.data, np.zeros((SIZE,SIZE,2))) print 'phi max, min:', abs(self.phi_surf.data).max(), abs(self.phi_surf.data).min() # rescale self.phi_surf = sqp_problem.make_interp(self.phi_surf.data / abs(self.phi_surf.data).max()) # print 'smoothing' # print self.phi_surf.data # self.phi_surf = sqp_problem.make_interp(smooth_by_edt(self.phi_surf.data)) # print 'after' # print self.phi_surf.data # print 'done' self.prob.set_coeffs(flow_agree=1)
def optimize_sqp(self): if self.prob is None: self.prob = sqp_problem.TrackingProblem() self.prob.set_coeffs(flow_agree=0) self.prob.set_obs_points(self.curr_obs) self.prob.set_prev_phi_surf(self.phi_surf) self.phi_surf, self.curr_u = self.prob.optimize( self.prob.prev_phi_surf.data, np.zeros((SIZE, SIZE, 2))) print 'phi max, min:', abs(self.phi_surf.data).max(), abs( self.phi_surf.data).min() # rescale self.phi_surf = sqp_problem.make_interp(self.phi_surf.data / abs(self.phi_surf.data).max()) # print 'smoothing' # print self.phi_surf.data # self.phi_surf = sqp_problem.make_interp(smooth_by_edt(self.phi_surf.data)) # print 'after' # print self.phi_surf.data # print 'done' self.prob.set_coeffs(flow_agree=1)
def simple(): empty_phi = np.ones((SIZE, SIZE)) square_phi = np.ones_like(empty_phi) square_phi[int(SIZE / 2.), int(SIZE / 4.):-int(SIZE / 4.)] = 0. square_phi[int(SIZE / 2.):-int(SIZE / 4.), int(SIZE / 4.)] = 0. square_phi[int(SIZE / 2.):-int(SIZE / 4.), -int(SIZE / 4.)] = 0. square_phi[-int(SIZE / 4.), int(SIZE / 4.):-int(SIZE / 4.) + 1] = 0. init_phi = square_phi # init_phi = np.ones_like(empty_phi) # init_phi[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. # init_phi[int(SIZE/4.),int(SIZE/3.):-int(SIZE/3.)] = 0. init_phi = smooth(init_phi) # negate inside init_phi[int(SIZE / 2.):-int(SIZE / 4.), int(SIZE / 4.):-int(SIZE / 4.)] *= -1 # import cPickle # with open('/tmp/init_phi.pkl', 'w') as f: cPickle.dump(init_phi, f) init_phi_observed = np.ones_like(empty_phi) init_phi_observed[int(SIZE / 2.), int(SIZE / 4.):-int(SIZE / 4.)] = 0. # init_phi_observed = smooth(init_phi_observed) new_phi_observed = init_phi_observed new_phi_observed[int(SIZE / 2.), int(SIZE / 10.):-int(SIZE / 10.)] = 0. # new_phi_observed = sn.interpolation.zoom(init_phi_observed, 1.2, cval=1., order=0) # new_phi_observed = sn.interpolation.shift(init_phi_observed, [2,0], cval=1., order=0) # new_phi_observed = sn.interpolation.rotate(init_phi_observed, 5., cval=1., order=0) # plt.imshow(new_phi_observed, cmap=cm.Greys_r) # plt.show() obs_pts = np.transpose(np.nonzero(new_phi_observed == 0)) # prob = sqp_problem.TrackingProblem() prob = ctimbpy.TrackingProblem(WORLD_MIN[0], WORLD_MAX[0], WORLD_MIN[1], WORLD_MAX[1], SIZE, SIZE) prob.set_observation_points(obs_pts) prob.set_prev_phi(init_phi) # prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-3, flow_tps=0, obs=1, flow_agree=1) init_u = np.zeros(init_phi.shape + (2, )) #init_u[:,:,0] = 1. # out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) result = prob.optimize() out_phis = [result.phi] out_us = [result.u] opt_result = result.opt_result for out_phi, out_u in [zip(out_phis, out_us)[-1]]: # import cPickle # with open('/tmp/dump.pkl', 'w') as f: # cPickle.dump((out_phi, out_u, opt_result), f) global fig fig = plt.figure(figsize=(15, 15)) init_phi[obs_pts[:, 0], obs_pts[:, 1]] = 0 plot_phi(221, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(224) ax.plot(np.arange(len(opt_result.cost_over_iters)), opt_result.cost_over_iters) #out_phi = reintegrate(out_phi) plot_phi(223, sqp_problem.make_interp(out_phi)) plot_u(222, out_u) plt.show()
def rotate(): empty_phi = np.ones((SIZE, SIZE)) square_phi = np.ones_like(empty_phi) square_phi[int(SIZE / 2.), int(SIZE / 4.):-int(SIZE / 4.)] = 0. square_phi[int(SIZE / 2.):-int(SIZE / 4.), int(SIZE / 4.)] = 0. square_phi[int(SIZE / 2.):-int(SIZE / 4.), -int(SIZE / 4.)] = 0. square_phi[-int(SIZE / 4.), int(SIZE / 4.):-int(SIZE / 4.) + 1] = 0. orig_phi = smooth(square_phi) init_phi = np.ones_like(empty_phi) init_obs_inds = calc_observable_inds(square_phi) init_phi[init_obs_inds[:, 0], init_obs_inds[:, 1]] = 0. init_phi = smooth(init_phi) # go through a rotation of the square prob = sqp_problem.TrackingProblem() prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1, obs=1, flow_agree=1) for i_iter, angle in enumerate(np.arange(0, 359, 5)): if i_iter == 0: prob.set_coeffs(flow_agree=0) else: prob.set_coeffs(flow_agree=1) print 'Current angle:', angle # make an observation rotated = sn.interpolation.rotate(square_phi, angle, cval=1., order=0, reshape=False) print rotated.shape, square_phi.shape obs_inds = calc_observable_inds(rotated) prob.set_obs_points(obs_inds) prob.set_prev_phi(init_phi) init_u = np.zeros(init_phi.shape + (2, )) out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: global fig fig = plt.figure() not_oob = (0 <= obs_inds[:, 0]) & ( obs_inds[:, 0] < init_phi.shape[0]) & (0 <= obs_inds[:, 1]) & ( obs_inds[:, 1] < init_phi.shape[1]) init_phi[obs_inds[:, 0][not_oob], obs_inds[:, 1][not_oob]] = .5 plot_phi(231, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(234) tmp = rotated.copy() tmp[obs_inds[:, 0], obs_inds[:, 1]] = .5 tmp = np.flipud(tmp.T) ax.imshow(tmp, cmap=cm.Greys_r).set_interpolation('nearest') # ax = fig.add_subplot(224) # ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) plot_phi(232, sqp_problem.make_interp(out_phi)) plot_u(233, out_u) # zero_thresh = sqp_problem.make_interp(out_phi).eval_ijs(obs_inds).mean() # print 'zero thresh', zero_thresh out_phi = reintegrate(out_phi) #, zero_thresh) plot_phi(235, sqp_problem.make_interp(out_phi)) plt.show() # TODO: REINTEGRATE HERE init_phi = out_phi
def rotate_full_obs(): square_phi = make_square_phi() init_phi = square_phi.copy() # go through a rotation of the square prob = sqp_problem.TrackingProblem() prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-3, flow_tps=1e-5, obs=1, flow_agree=1) for i_iter, angle in enumerate(np.arange(0, 359, 5)): print 'Current angle:', angle # make an observation rotated = sn.interpolation.rotate(square_phi, angle, cval=1., order=0, reshape=False) print rotated.shape, square_phi.shape obs_inds = calc_observable_inds(rotated) prob.set_obs_points(obs_inds) prob.set_prev_phi(init_phi) init_u = np.zeros(init_phi.shape + (2, )) out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: global fig fig = plt.figure() # superimpose observation drawing not_oob = (0 <= obs_inds[:, 0]) & ( obs_inds[:, 0] < init_phi.shape[0]) & (0 <= obs_inds[:, 1]) & ( obs_inds[:, 1] < init_phi.shape[1]) init_phi[obs_inds[:, 0][not_oob], obs_inds[:, 1][not_oob]] = 0 plot_phi(231, sqp_problem.make_interp(init_phi)) # ax = fig.add_subplot(234) # tmp = rotated.copy() # tmp[obs_inds[:,0],obs_inds[:,1]] = .5 # tmp = np.flipud(tmp.T) # ax.imshow(tmp, cmap=cm.Greys_r).set_interpolation('nearest') ax = fig.add_subplot(234) ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) plot_phi(232, sqp_problem.make_interp(out_phi)) plot_u(233, out_u) out_phi = reintegrate(out_phi) plot_phi(235, sqp_problem.make_interp(out_phi)) plt.show() init_phi = out_phi init_phi_observed = np.ones_like(empty_phi) init_phi_observed[int(SIZE / 2.), int(SIZE / 4.):-int(SIZE / 4.)] = 0. new_phi_observed = init_phi_observed new_phi_observed[int(SIZE / 2.), int(SIZE / 10.):-int(SIZE / 10.)] = 0. obs_pts = np.transpose(np.nonzero(new_phi_observed == 0)) prob = sqp_problem.TrackingProblem() prob.set_obs_points(obs_pts) prob.set_prev_phi(init_phi) prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-8, flow_tps=1e-5, obs=1, flow_agree=1) init_u = np.zeros(init_phi.shape + (2, )) #init_u[:,:,0] = 1. out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: # import cPickle # with open('/tmp/dump.pkl', 'w') as f: # cPickle.dump((out_phi, out_u, opt_result), f) global fig fig = plt.figure(figsize=(15, 15)) init_phi[obs_pts[:, 0], obs_pts[:, 1]] = 0 plot_phi(221, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(224) ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) out_phi = reintegrate(out_phi) plot_phi(223, sqp_problem.make_interp(out_phi)) plot_u(222, out_u) plt.show()
def simple(): empty_phi = np.ones((SIZE, SIZE)) square_phi = np.ones_like(empty_phi) square_phi[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. square_phi[int(SIZE/2.):-int(SIZE/4.),int(SIZE/4.)] = 0. square_phi[int(SIZE/2.):-int(SIZE/4.),-int(SIZE/4.)] = 0. square_phi[-int(SIZE/4.),int(SIZE/4.):-int(SIZE/4.)+1] = 0. init_phi = square_phi # init_phi = np.ones_like(empty_phi) # init_phi[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. # init_phi[int(SIZE/4.),int(SIZE/3.):-int(SIZE/3.)] = 0. init_phi = smooth(init_phi) # negate inside init_phi[int(SIZE/2.):-int(SIZE/4.),int(SIZE/4.):-int(SIZE/4.)] *= -1 # import cPickle # with open('/tmp/init_phi.pkl', 'w') as f: cPickle.dump(init_phi, f) init_phi_observed = np.ones_like(empty_phi) init_phi_observed[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. # init_phi_observed = smooth(init_phi_observed) new_phi_observed = init_phi_observed; new_phi_observed[int(SIZE/2.),int(SIZE/10.):-int(SIZE/10.)] = 0. # new_phi_observed = sn.interpolation.zoom(init_phi_observed, 1.2, cval=1., order=0) # new_phi_observed = sn.interpolation.shift(init_phi_observed, [2,0], cval=1., order=0) # new_phi_observed = sn.interpolation.rotate(init_phi_observed, 5., cval=1., order=0) # plt.imshow(new_phi_observed, cmap=cm.Greys_r) # plt.show() obs_pts = np.transpose(np.nonzero(new_phi_observed == 0)) # prob = sqp_problem.TrackingProblem() prob = ctimbpy.TrackingProblem(WORLD_MIN[0], WORLD_MAX[0], WORLD_MIN[1], WORLD_MAX[1], SIZE, SIZE) prob.set_observation_points(obs_pts) prob.set_prev_phi(init_phi) # prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-3, flow_tps=0, obs=1, flow_agree=1) init_u = np.zeros(init_phi.shape + (2,)) #init_u[:,:,0] = 1. # out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) result = prob.optimize() out_phis = [result.phi] out_us = [result.u] opt_result = result.opt_result for out_phi, out_u in [zip(out_phis, out_us)[-1]]: # import cPickle # with open('/tmp/dump.pkl', 'w') as f: # cPickle.dump((out_phi, out_u, opt_result), f) global fig fig = plt.figure(figsize=(15,15)) init_phi[obs_pts[:,0],obs_pts[:,1]] = 0 plot_phi(221, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(224) ax.plot(np.arange(len(opt_result.cost_over_iters)), opt_result.cost_over_iters) #out_phi = reintegrate(out_phi) plot_phi(223, sqp_problem.make_interp(out_phi)) plot_u(222, out_u) plt.show()
def rotate(): empty_phi = np.ones((SIZE, SIZE)) square_phi = np.ones_like(empty_phi) square_phi[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. square_phi[int(SIZE/2.):-int(SIZE/4.),int(SIZE/4.)] = 0. square_phi[int(SIZE/2.):-int(SIZE/4.),-int(SIZE/4.)] = 0. square_phi[-int(SIZE/4.),int(SIZE/4.):-int(SIZE/4.)+1] = 0. orig_phi = smooth(square_phi) init_phi = np.ones_like(empty_phi) init_obs_inds = calc_observable_inds(square_phi) init_phi[init_obs_inds[:,0],init_obs_inds[:,1]] = 0. init_phi = smooth(init_phi) # go through a rotation of the square prob = sqp_problem.TrackingProblem() prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1, obs=1, flow_agree=1) for i_iter, angle in enumerate(np.arange(0, 359, 5)): if i_iter == 0: prob.set_coeffs(flow_agree=0) else: prob.set_coeffs(flow_agree=1) print 'Current angle:', angle # make an observation rotated = sn.interpolation.rotate(square_phi, angle, cval=1., order=0, reshape=False) print rotated.shape, square_phi.shape obs_inds = calc_observable_inds(rotated) prob.set_obs_points(obs_inds) prob.set_prev_phi(init_phi) init_u = np.zeros(init_phi.shape + (2,)) out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: global fig fig = plt.figure() not_oob = (0 <= obs_inds[:,0]) & (obs_inds[:,0] < init_phi.shape[0]) & (0 <= obs_inds[:,1]) & (obs_inds[:,1] < init_phi.shape[1]) init_phi[obs_inds[:,0][not_oob],obs_inds[:,1][not_oob]] = .5 plot_phi(231, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(234) tmp = rotated.copy() tmp[obs_inds[:,0],obs_inds[:,1]] = .5 tmp = np.flipud(tmp.T) ax.imshow(tmp, cmap=cm.Greys_r).set_interpolation('nearest') # ax = fig.add_subplot(224) # ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) plot_phi(232, sqp_problem.make_interp(out_phi)) plot_u(233, out_u) # zero_thresh = sqp_problem.make_interp(out_phi).eval_ijs(obs_inds).mean() # print 'zero thresh', zero_thresh out_phi = reintegrate(out_phi)#, zero_thresh) plot_phi(235, sqp_problem.make_interp(out_phi)) plt.show() # TODO: REINTEGRATE HERE init_phi = out_phi
def rotate_full_obs(): square_phi = make_square_phi() init_phi = square_phi.copy() # go through a rotation of the square prob = sqp_problem.TrackingProblem() prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-3, flow_tps=1e-5, obs=1, flow_agree=1) for i_iter, angle in enumerate(np.arange(0, 359, 5)): print 'Current angle:', angle # make an observation rotated = sn.interpolation.rotate(square_phi, angle, cval=1., order=0, reshape=False) print rotated.shape, square_phi.shape obs_inds = calc_observable_inds(rotated) prob.set_obs_points(obs_inds) prob.set_prev_phi(init_phi) init_u = np.zeros(init_phi.shape + (2,)) out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: global fig fig = plt.figure() # superimpose observation drawing not_oob = (0 <= obs_inds[:,0]) & (obs_inds[:,0] < init_phi.shape[0]) & (0 <= obs_inds[:,1]) & (obs_inds[:,1] < init_phi.shape[1]) init_phi[obs_inds[:,0][not_oob],obs_inds[:,1][not_oob]] = 0 plot_phi(231, sqp_problem.make_interp(init_phi)) # ax = fig.add_subplot(234) # tmp = rotated.copy() # tmp[obs_inds[:,0],obs_inds[:,1]] = .5 # tmp = np.flipud(tmp.T) # ax.imshow(tmp, cmap=cm.Greys_r).set_interpolation('nearest') ax = fig.add_subplot(234) ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) plot_phi(232, sqp_problem.make_interp(out_phi)) plot_u(233, out_u) out_phi = reintegrate(out_phi) plot_phi(235, sqp_problem.make_interp(out_phi)) plt.show() init_phi = out_phi init_phi_observed = np.ones_like(empty_phi) init_phi_observed[int(SIZE/2.),int(SIZE/4.):-int(SIZE/4.)] = 0. new_phi_observed = init_phi_observed; new_phi_observed[int(SIZE/2.),int(SIZE/10.):-int(SIZE/10.)] = 0. obs_pts = np.transpose(np.nonzero(new_phi_observed == 0)) prob = sqp_problem.TrackingProblem() prob.set_obs_points(obs_pts) prob.set_prev_phi(init_phi) prob.set_coeffs(flow_norm=1e-9, flow_rigidity=1e-8, flow_tps=1e-5, obs=1, flow_agree=1) init_u = np.zeros(init_phi.shape + (2,)) #init_u[:,:,0] = 1. out_phis, out_us, opt_result = prob.optimize(init_phi, init_u, return_full=True) for out_phi, out_u in [zip(out_phis, out_us)[-1]]: # import cPickle # with open('/tmp/dump.pkl', 'w') as f: # cPickle.dump((out_phi, out_u, opt_result), f) global fig fig = plt.figure(figsize=(15,15)) init_phi[obs_pts[:,0],obs_pts[:,1]] = 0 plot_phi(221, sqp_problem.make_interp(init_phi)) ax = fig.add_subplot(224) ax.plot(np.arange(len(opt_result.costs_over_iters)), opt_result.costs_over_iters) out_phi = reintegrate(out_phi) plot_phi(223, sqp_problem.make_interp(out_phi)) plot_u(222, out_u) plt.show()
#u2 = np.empty_like(u) #u2[:,:,0] = u[:,:,1] #u2[:,:,1] = u[:,:,0] #u = u2 fig = plt.figure() ax = fig.add_subplot(221, aspect='equal') #mask = Y[:,:] == 15 #u[:,:,0][np.logical_not(mask)] = 0 #u[:,:,1][np.logical_not(mask)] = 0 print u ax.quiver(X, Y, u[:,:,0], u[:,:,1], angles='xy', scale_units='xy', scale=1.) init_phi_surf = sqp_problem.make_interp(init_phi) plot_phi(fig, 222, init_phi_surf) out_phi = apply_flow(init_phi, u) apply_flow_forwards(fig, 224, init_phi, u, X2.ravel(), Y2.ravel(), init_phi.ravel()) #for i in range(20): # print i # out_phi = apply_flow(out_phi, u) out_phi_surf = sqp_problem.make_interp(out_phi) ax = plot_phi(fig, 223, out_phi_surf) # ax.quiver(X, Y, np.fliplr(u[:,:,0].T), u[:,:,1].T, pivot='tip') plt.show()