def PPCSD_solver(self, opt, ic, start, end, Tn, m, mu_list, options=None): h = self.mesh.hmax() X = Function(self.V) xN = len(ic.vector().array()) control0 = self.get_control(opt, ic, m) self.update_SD_options(options) res = [] PPC = self.PC_maker(opt, ic, start, end, Tn, m) for k in range(len(mu_list)): mu = mu_list[k] J, grad_J = self.create_reduced_penalty_j(opt, ic, start, end, Tn, m, mu) solver = PPCSteepestDecent(J, grad_J, control0.copy(), PPC, options=self.SD_options) result = solver.solve() res.append(result) control0 = result.x.copy() if len(res) == 1: return res[0] else: return res
def parallel_SD_penalty_solve(self,N,m,mu_list,tol_list=None,x0=None,options=None): comm = self.comm rank = self.rank if x0==None: x0= self.initial_control2(N,m=m) initial_counter = self.counter.copy() self.update_Lbfgs_options(options) Result = [] PPC = self.PC_maker4(N,m,comm,step=1) for i in range(len(mu_list)): def J(u): self.counter[0]+=1 return self.parallel_penalty_functional(u,N,mu_list[i]) def grad_J(u): self.counter[1]+=1 return self.penalty_grad(u,N,m,mu_list[i]) #solver = SplitLbfgs(J,grad_J,x0,options=self.Lbfgs_options,mpi=True) solver = PPCSteepestDecent(J,grad_J,x0,PPC,options=self.Lbfgs_options) res = solver.mpi_solve() x0 = res.x res.add_FuncGradCounter(self.counter-initial_counter) Result.append(res) val =self.find_jump_diff(res.x) if self.rank == 0: print 'jump diff:',val return Result
def PPCSDsolve(self, N, m, my_list, x0=None, tol_list=None, options=None, split=False): dt = float(self.T) / N if x0 == None: x0 = np.zeros(N + m) result = [] PPC = self.PC_maker2(N, m, step=1) initial_counter = self.counter.copy() for i in range(len(my_list)): J, grad_J = self.generate_reduced_penalty(dt, N, m, my_list[i]) self.update_SD_options(options) if tol_list != None: try: opt = {'jtol': tol_list[i]} self.update_SD_options(opt) except: print 'no good tol_list' SDopt = self.SD_options Solver = PPCSteepestDecent(J, grad_J, x0.copy(), PPC, decomp=m, options=SDopt) if split: res = Solver.split_solve(m) else: res = Solver.solve(m) x0 = res.x result.append(res) res.add_FuncGradCounter(self.counter - initial_counter) if len(result) == 1: #y,Y = self.ODE_penalty_solver(x0,N,m) #import matplotlib.pyplot as plt #plt.plot(Y) #plt.show() return res else: return result
def penalty_solve(self,N,m,my_list,tol_list=None,x0=None,Lbfgs_options=None,algorithm='my_lbfgs',scale=False): """ Solve the optimazation problem with penalty Arguments: * N: number of discritization points * m: number ot processes * my_list: list of penalty variables, that we want to solve the problem for. * x0: initial guess for control * options: same as for class initialisation """ self.t,self.T_z = self.decompose_time(N,m) dt=float(self.T)/N if x0==None: x0 = self.initial_control(N,m=m)#np.zeros(N+m) x = None if algorithm=='my_lbfgs': x0 = self.Vec(x0) Result = [] initial_counter = self.counter.copy() for i in range(len(my_list)): #""" def J(u): self.counter[0]+=1 return self.Penalty_Functional(u,N,m,my_list[i]) def grad_J(u): self.counter[1]+=1 return self.Penalty_Gradient(u,N,m,my_list[i]) #""" #J,grad_J = self.generate_reduced_penalty(dt,N,m,my_list[i]) if algorithm=='my_lbfgs': self.update_Lbfgs_options(Lbfgs_options) if tol_list!=None: try: opt = {'jtol':tol_list[i]} self.update_Lbfgs_options(opt) except: print 'no good tol_list' if scale: scaler={'m':m,'factor':self.Lbfgs_options['scale_factor']} #solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options,scale=scaler) solver = SplitLbfgs(J,grad_J,x0.array(),options=self.Lbfgs_options,scale=scaler) else: #solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options) solver = SplitLbfgs(J,grad_J,x0.array(),m=m,options=self.Lbfgs_options) #res = solver.solve() res = solver.normal_solve() x0 = res['control'] #print J(x0.array()) elif algorithm=='my_steepest_decent': self.update_SD_options(Lbfgs_options) SDopt = self.SD_options if scale: scale = {'m':m,'factor':SDopt['scale_factor']} Solver = SteepestDecent(J,grad_J,x0.copy(), options=SDopt,scale=scale) res = Solver.solve() res.rescale() else: Solver = PPCSteepestDecent(J,grad_J,x0.copy(), lambda x: x,options=SDopt) res = Solver.split_solve(m) x0 = res.x.copy() elif algorithm=='slow_steepest_decent': self.update_SD_options(Lbfgs_options) SDopt = self.SD_options Solver = SteepestDecent(J,grad_J,x0.copy(), options=SDopt) res = Solver.solve() x0 = res.x.copy() elif algorithm == 'split_lbfgs': self.update_Lbfgs_options(Lbfgs_options) Solver = SplitLbfgs(J,grad_J,x0,m,options=self.Lbfgs_options) res = Solver.solve() x0 = res.x.copy() res.jump_diff=self.jump_diff Result.append(res) print 'jump diff:',self.jump_diff res.add_FuncGradCounter(self.counter-initial_counter) if len(Result)==1: return res else: return Result