示例#1
0
文件: search.py 项目: rizoid/pypge
	def loop(self, iterations):
		# main loop for # iterations
		for I in range(iterations):
			print ("\n\nITER: ", I)
			self.curr_iter = I
		
			L = None
			if self.log_details:
				L = self.logs
			T = None
			if self.print_timing:
				T = timewith()

			def pfunc(msg, iarg, numto=0, lognames=["stdout"], final=False):
				if T:
					s = "  {:3d} {:24s}| {:4d} ".format(self.curr_iter, msg, iarg)
					if numto == 0:
						s += "        "
					else:
						s += "-> {:4d} ".format(numto)
					if final:
						s += T.finalize()
						s += '      {:10.4f} total runtime'.format(self.curr_time - self.start_time)
					else:
						s += T.checkpoint(iarg)

					if len(lognames) == 0:
						return s
					else:
						for logname in lognames:
							print(s, file=L[logname])


			# Multi expand and grow
			curr = []
			prev = []
			popd = []
			expanded = []
			for i,_ in enumerate(self.multi_expanders):
				nsga2_list = self.multi_expanders[i]['nsga2_list']
				pop_count = self.multi_expanders[i]['pop_count']
				grower = self.multi_expanders[i]['grower']

				if len(prev) > 0:
					self.heap_push(nsga2_list, prev)

				fromlen = len(nsga2_list)
				curr, nsga2_list = self.heap_pop(nsga2_list, pop_count, self.fitness_calc)
				popd.extend(curr)
				pfunc("heap " + str(i) + " popping", fromlen, numto=len(curr), lognames=["stdout","main"])

				expand_tmp = self.expand_models(curr, grower)
				pfunc("popped => expanded", len(curr), numto=len(expand_tmp), lognames=["stdout","main"])
				expanded.extend(expand_tmp)

				self.multi_expanders[i]['nsga2_list'] = nsga2_list
				prev = curr


			self.assign_iter_id(expanded)
			# pfunc("popped => expanded", len(popd), numto=len(expanded), lognames=["stdout","main"])


			# filter and memoize expanded models
			to_memo = self.filter_models(expanded)			
			pfunc("expanded => filtered", len(expanded), numto=len(to_memo), lognames=["stdout","main"])

			to_alge = []
			if self.algebra_methods is not None and len(self.algebra_methods) > 0:
				to_alge = self.memoize_models(to_memo)
				pfunc("memoize => algebra", len(to_memo), numto=len(to_alge), lognames=["stdout","main"])

				# algebra the models which made it through
				algebrad = self.algebra_models(to_alge)
				self.assign_iter_id(algebrad)
				pfunc("algebra => result models", len(to_alge), numto=len(algebrad), lognames=["stdout","main"])

				# filter and memoize the algebrad models
				to_memo = self.filter_models(algebrad)
				pfunc("algebrad => filtered", len(algebrad), numto=len(to_memo), lognames=["stdout","main"])

			to_peek = self.memoize_models(to_memo)
			pfunc("memoized => fitting", len(to_memo), numto=len(to_peek), lognames=["stdout","main"])

			# pass along the unique expressions, plus the algebrad offspring unique
			to_peek = to_alge + to_peek
			pfunc("total for fitting", len(to_peek), lognames=["stdout","main"])


			to_eval = []
			if self.peek_npts == 0:
				to_eval = to_peek
			else:
				# peek evaluate the unique models
				self.eval_models(to_peek, peek=True)
				pfunc("peeking", len(to_peek), lognames=["stdout","main"])

				# push the peek'd models
				self.peek_push_models(to_peek)
				pfunc("peek pushing", len(to_peek), lognames=["stdout","main"])

				# pop some models for full evaluation
				fromlen = len(self.nsga2_peek)
				to_eval = self.peek_pop()
				pfunc("peek popping", fromlen, numto=len(to_eval), lognames=["stdout","main"])

			# fully fit and evaluate these models
			self.eval_models(to_eval)
			pfunc("evaling", len(to_eval), lognames=["stdout","main"])

			# after fully eval'd, push to final_paretos
			self.final_push(to_eval)

			# push fully fit models into the queue for expansion candidacy
			# self.eval_push_models(to_eval)
			self.heap_push(self.multi_expanders[0]['nsga2_list'], to_eval)
			pfunc("eval pushing", len(to_eval), lognames=["stdout","main"])

			self.curr_time = time.time()
			pfunc("total loop time", I, final=True, lognames=["stdout","main"])
			self.print_best(32)

			s = "{:3d} {:.4f} {} {}   {} {} {} {} {}   {} {} {} {}   {} {} {} {}".format(
					self.curr_iter,
					self.curr_time - self.start_time,
					self.peekd_models, 
					self.evald_models,

					self.peek_nfev, 
					self.peek_nfev * self.peek_npts,
					self.eval_nfev, 
					self.eval_nfev * self.eval_npts,
					self.peek_nfev * self.peek_npts + self.eval_nfev * self.eval_npts,

					self.best_size,
					self.best_err,
					self.best_r2,
					self.best_vari,

					self.ave_size,
					self.ave_err,
					self.ave_r2,
					self.ave_vari

				)
			print(s, file=self.logs["errs"])

			if self.log_details:
				for name, file_obj in self.logs.items():
					file_obj.flush()
示例#2
0
文件: search.py 项目: rizoid/pypge
	def preloop(self):
		# preloop setup (generates,evals,queues first models)
		print ("Preloop setup")

		if self.workers > 0:
			for proc in self.peek_procs:
				proc.start()
			for proc in self.eval_procs:
				proc.start()
			for proc in self.alge_procs:
				proc.start()
			for proc in self.expd_procs:
				proc.start()

		L = None
		if self.log_details:
			L = self.logs
		T = None
		if self.print_timing:
			T = timewith()

		def pfunc(msg, iarg, numto=0, lognames=["stdout"], final=False):
			if T:
				s = "  {:3d} {:24s}| {:4d} ".format(self.curr_iter, msg, iarg)
				if numto == 0:
					s += "        "
				else:
					s += "-> {:4d} ".format(numto)
				if final:
					s += T.finalize()
					s += '      {:10.4f} total runtime'.format(self.curr_time - self.start_time)
				else:
					s += T.checkpoint(iarg)

				if len(lognames) == 0:
					return s
				elif L is not None:
					for logname in lognames:
						print(s, file=L[logname])


		# create first models
		first_exprs = self.multi_expanders[0]['grower'].first_exprs()
		self.assign_iter_id(first_exprs)
		pfunc("create first exprs", len(first_exprs), lognames=["stdout","main"])

		# filter and memoize first_exprs models
		to_memo = self.filter_models(first_exprs)
		pfunc("first => filtered", len(first_exprs), numto=len(to_memo), lognames=["stdout","main"])

		to_alge = []
		if self.algebra_methods is not None and len(self.algebra_methods) > 0:
			to_alge = self.memoize_models(to_memo)
			pfunc("memoize => algebra", len(to_memo), numto=len(to_alge), lognames=["stdout","main"])

			# algebra the models which made it through
			algebrad = self.algebra_models(to_alge)
			self.assign_iter_id(algebrad)
			pfunc("algebra => result models", len(to_alge), numto=len(algebrad), lognames=["stdout","main"])

			# filter and memoize the algebrad models
			to_memo = self.filter_models(algebrad)
			pfunc("algebrad => filtered", len(algebrad), numto=len(to_memo), lognames=["stdout","main"])

		to_peek = self.memoize_models(to_memo)
		pfunc("memoized => fitting", len(to_memo), numto=len(to_peek), lognames=["stdout","main"])

		# pass along the unique expressions, plus the algebrad offspring unique
		to_peek = to_alge + to_peek
		pfunc("total for fitting", len(to_peek), lognames=["stdout","main"])


		to_eval = []
		if self.peek_npts == 0:
			to_eval = to_peek
		else:
			self.eval_models(to_peek, peek=True)
			pfunc("peeking", len(to_peek), lognames=["stdout","main"])

			self.peek_push_models(to_peek)
			pfunc("peek pushing", len(to_peek), lognames=["stdout","main"])

			fromlen = len(self.nsga2_peek)
			to_eval = self.peek_pop() + self.peek_pop() # twice the first time
			pfunc("peek popping", fromlen, numto=len(to_eval), lognames=["stdout","main"])


		self.eval_models(to_eval)
		pfunc("evaling", len(to_eval), lognames=["stdout","main"])

		# after fully eval'd, push to final_paretos
		self.final_push(to_eval)

		# self.eval_push_models(to_eval)
		self.heap_push(self.multi_expanders[0]['nsga2_list'], to_eval)
		pfunc("eval pushing", len(to_eval), lognames=["stdout","main"])

		self.curr_time = time.time()
		pfunc("total preloop time", -1, final=True, lognames=["stdout","main"])

		self.print_best(16)

		if self.log_details:
			for name, file_obj in self.logs.items():
				file_obj.flush()