def solver(self, opt, ic, start, end, Tn, algorithm='scipy_lbfgs', options=None): h = self.mesh.hmax() def J(x): loc_opt, loc_ic = self.get_opt(x, opt, ic, 1) U = self.PDE_solver(loc_ic, loc_opt, start, end, Tn) return self.J(loc_opt, loc_ic, U, start, end) def grad_J(x): loc_opt, loc_ic = self.get_opt(x, opt, ic, 1) P = self.adjoint_solver(loc_ic, loc_opt, start, end, Tn) return self.grad_J(P, loc_opt, loc_ic, h) control0 = self.get_control(opt, ic, 1) if algorithm == 'my_lbfgs': control0 = SimpleVector(control0) self.update_lbfgs_options(options) solver = Lbfgs(J, grad_J, control0, options=self.Lbfgs_options) res = solver.solve() elif algorithm == 'scipy_lbfgs': res = Mini(J, control0.copy(), method='L-BFGS-B', jac=grad_J, options={ 'gtol': 1e-5, 'disp': True }) elif algorithm == 'my_steepest_decent': self.update_SD_options(options) SDopt = self.SD_options Solver = SteepestDecent(J, grad_J, control0.copy(), options=SDopt) res = Solver.solve() return res
def parallel_penalty_one_iteration_solve(self,N,m,my_list,x0=None, Lbfgs_options=None, algorithm='my_lbfgs',scale=False): dt=float(self.T)/N if x0==None: x0 = np.zeros(N+m) x = None if algorithm=='my_lbfgs': x0 = self.Vec(x0) Result = [] rank = self.comm.Get_rank() for i in range(len(my_list)): def J(u): return self.parallel_penalty_functional(u,N,my_list[i]) def grad_J(u): return self.penalty_grad(u,N,m,my_list[i]) if algorithm=='my_lbfgs': self.update_Lbfgs_options(Lbfgs_options) if scale: scaler={'m':m,'factor':self.Lbfgs_options['scale_factor']} solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options, scale=scaler) else: solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options) res = solver.one_iteration(self.comm) Result.append(res) #print res.array(),rank #x0 = res['control'] #print J(x0.array()) if rank ==0: Result.append(res) else: Result.append(0) return Result if len(Result)==1: return res else: return Result
def penalty_solver(self,opt,ic,start,end,Tn,m,mu_list, algorithm='scipy_lbfgs',options=None): h = self.h X = Function(self.V) xN = self.xN control0 = self.get_control(opt,ic,m) if algorithm=='my_lbfgs': control0 = SimpleVector(control0) res =[] for k in range(len(mu_list)): J,grad_J = self.create_reduced_penalty_j(opt,ic,start,end,Tn,m,mu_list[k]) if algorithm == 'my_steepest_decent': self.update_SD_options(options) SDopt = self.SD_options Solver = SteepestDecent(J,grad_J,control0.copy(), options=SDopt) res1 = Solver.solve() control0 = res1.x.copy() else: self.update_lbfgs_options(options) if algorithm=='my_lbfgs': solver = Lbfgs(J,grad_J,control0,options=self.Lbfgs_options) res1 = solver.solve() control0 = res1['control'].copy() elif algorithm=='scipy_lbfgs': res1 = Mini(J,control0.copy(),method='L-BFGS-B',jac=grad_J, options={'gtol':1e-6, 'disp':True,'maxcor':10}) control0 = res1.x.copy() res.append(res1) if len(res)==1: return res[0] return res
def solve(self,N,x0=None,Lbfgs_options=None,algorithm='my_lbfgs'): """ Solve the optimazation problem without penalty Arguments: * N: number of discritization points * x0: initial guess for control * Lbfgs_options: same as for class initialisation """ dt=float(self.T)/N if x0==None: x0 = np.zeros(N+1) if algorithm=='my_lbfgs': x0 = self.Vec(x0) def J(u): return self.Functional(u,N) def grad_J(u): l = self.adjoint_solver(u,N) return self.grad_J(u,l,dt) if algorithm=='my_lbfgs': self.update_Lbfgs_options(Lbfgs_options) solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options) res = solver.solve() elif algorithm=='my_steepest_decent': self.update_SD_options(Lbfgs_options) SDopt = self.SD_options Solver = SteepestDecent(J,grad_J,x0.copy(), options=SDopt) res = Solver.solve() import matplotlib.pyplot as plt #Y = self.ODE_solver(res.x,N) #plt.plot(Y) #plt.show() return res
def solver(self,opt,ic,start,end,Tn,algorithm='scipy_lbfgs', options=None): h = self.mesh.hmax() def J(x): loc_opt,loc_ic = self.get_opt(x,opt,ic,1) U = self.PDE_solver(loc_ic,loc_opt,start,end,Tn) return self.J(loc_opt,loc_ic,U,start,end) def grad_J(x): loc_opt,loc_ic = self.get_opt(x,opt,ic,1) P = self.adjoint_solver(loc_ic,loc_opt,start,end,Tn) return self.grad_J(P,loc_opt,loc_ic,h) control0 = self.get_control(opt,ic,1) if algorithm=='my_lbfgs': control0 = SimpleVector(control0) self.update_lbfgs_options(options) solver = Lbfgs(J,grad_J,control0,options=self.Lbfgs_options) res = solver.solve() elif algorithm=='scipy_lbfgs': res = Mini(J,control0.copy(),method='L-BFGS-B', jac=grad_J,options={'gtol': 1e-5, 'disp': True}) elif algorithm=='my_steepest_decent': self.update_SD_options(options) SDopt = self.SD_options Solver = SteepestDecent(J,grad_J,control0.copy(),options=SDopt) res = Solver.solve() return res
def PPCLBFGSsolve2(self, N, m, my_list, x0=None, options=None, scale=False): dt = float(self.T) / N if x0 == None: x0 = SimpleVector(x0=self.initial_control(N, m=m)) result = [] PPC = self.PC_creator(N, m, step=1) if scale: scaler = {'m': m, 'factor': 1} else: scaler = None for i in range(len(my_list)): J, grad_J = self.generate_reduced_penalty(dt, N, m, my_list[i]) self.update_Lbfgs_options(options) Lbfgsopt = self.Lbfgs_options Solver = Lbfgs(J, grad_J, x0, Hinit=None, options=Lbfgsopt, pc=PPC, scale=scaler) res = Solver.solve() x0 = res['control'] result.append(res) if len(result) == 1: return res else: return result
def lagrange_penalty_solve(self,N,m,my_list,x0=None,Lbfgs_options=None): """ Solve the optimazation problem with augmented lagrange 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 * Lbfgs_options: same as for class initialisation """ dt=float(self.T)/N if x0==None: x0 = self.Vec(np.zeros(N+m)) x = None Result = [] G = np.zeros(m-1) for i in range(len(my_list)): print print my_list[i],G print def init_pen(self,y,u,my,N,k): return self.initial_lagrange(y,u,my,N,k,G) def J(u): return self.Lagrange_Penalty_Functional(u,N,m,my_list[i],G) def grad_J(u): l,L = self.adjoint_penalty_solver(u,N,m,my_list[i],init=init_pen ) g = np.zeros(len(u)) g[:N+1]=self.grad_J(u[:N+1],L,dt) for j in range(m-1): g[N+1+j]=l[j+1][0]-l[j][-1] - G[j] return g if Lbfgs_options==None: Loptions = self.Lbfgs_options else: Loptions = self.Lbfgs_options for key, val in Lbfgs_options.iteritems(): Loptions[key]=val solver = Lbfgs(J,grad_J,x0,options=Loptions) res = solver.solve() Result.append(res) x0 = res['control'] print y,Y = self.ODE_penalty_solver(res['control'].array(),N,m) for j in range(m-1): G[j]=G[j]-my_list[i]*(y[j][-1]-res['control'].array()[N+1+j]) if len(Result)==1: return res else: return Result
def penalty_solve(self,N,m,my_list,x0=None,Lbfgs_options=None,algorithm='my_lbfgs'): """ 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 """ dt=float(self.T)/N if x0==None: x0 = np.zeros(N+m) x = None if algorithm=='my_lbfgs': x0 = self.Vec(x0) Result = [] for i in range(len(my_list)): def J(u): return self.Penalty_Functional(u,N,m,my_list[i]) def grad_J(u): l,L = self.adjoint_penalty_solver(u,N,m,my_list[i]) g = np.zeros(len(u)) g[:N+1]=self.grad_J(u[:N+1],L,dt) for j in range(m-1): g[N+1+j]= l[j+1][0] - l[j][-1] return g #J,grad_J = self.generate_reduced_penalty(dt,N,m,my_list[i]) if algorithm=='my_lbfgs': self.update_Lbfgs_options(Lbfgs_options) solver = Lbfgs(J,grad_J,x0,options=self.Lbfgs_options) res = solver.solve() Result.append(res) x0 = res['control'] print J(x0.array()) elif algorithm=='my_steepest_decent': self.update_SD_options(Lbfgs_options) SDopt = self.SD_options Solver = PPCSteepestDecent(J,grad_J,x0.copy(), lambda x: x,options=SDopt) res = Solver.split_solve(m) x0 = res.x.copy() Result.append(res) 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() Result.append(res) if len(Result)==1: return res else: return Result
def thread_parallel_penalty_solve(self, N, m, mu_list, x0=None, Lbfgs_options=None, algorithm='my_lbfgs', scale=False): dt = float(self.T) / N if x0 == None: x0 = np.zeros(N + m) x = None if algorithm == 'my_lbfgs': x0 = self.Vec(x0) Result = [] for i in range(len(mu_list)): def J(u): return self.thread_parallel_penalty_functional( u, N, m, mu_list[i]) def grad_J(u): return self.thread_parallel_penalty_grad(u, N, m, mu_list[i]) #J,grad_J = self.generate_reduced_penalty(dt,N,m,mu_list[i]) if algorithm == 'my_lbfgs': self.update_Lbfgs_options(Lbfgs_options) if scale: scaler = { 'm': m, 'factor': self.Lbfgs_options['scale_factor'] } solver = Lbfgs(J, grad_J, x0, options=self.Lbfgs_options, scale=scaler) else: solver = Lbfgs(J, grad_J, x0, options=self.Lbfgs_options) res = solver.solve() Result.append(res) 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() Result.append(res) 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() Result.append(res) 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() Result.append(res) if len(Result) == 1: return res else: return Result
def penalty_solver(self, opt, ic, start, end, Tn, m, mu_list, algorithm='scipy_lbfgs', options=None): h = self.h X = Function(self.V) xN = self.xN control0 = self.get_control(opt, ic, m) if algorithm == 'my_lbfgs': control0 = SimpleVector(control0) res = [] for k in range(len(mu_list)): J, grad_J = self.create_reduced_penalty_j(opt, ic, start, end, Tn, m, mu_list[k]) if algorithm == 'my_steepest_decent': self.update_SD_options(options) SDopt = self.SD_options Solver = SteepestDecent(J, grad_J, control0.copy(), options=SDopt) res1 = Solver.solve() control0 = res1.x.copy() else: self.update_lbfgs_options(options) if algorithm == 'my_lbfgs': solver = Lbfgs(J, grad_J, control0, options=self.Lbfgs_options) res1 = solver.solve() control0 = res1['control'].copy() elif algorithm == 'scipy_lbfgs': res1 = Mini(J, control0.copy(), method='L-BFGS-B', jac=grad_J, options={ 'gtol': 1e-6, 'disp': True, 'maxcor': 10 }) control0 = res1.x.copy() res.append(res1) if len(res) == 1: return res[0] return res