| 23 | SOLUTIONS = [] # TODO: remove global variable |
| 24 | |
| 25 | class SolutionStore(object): |
| 26 | def __init__(self, evaluations, max_time, success_cost, verbose, max_memory=INF): |
| 27 | # TODO: store a map from head to value? |
| 28 | # TODO: include other problem information here? |
| 29 | # TODO: determine when the plan converges |
| 30 | self.evaluations = evaluations |
| 31 | #self.initial_evaluations = copy.copy(evaluations) |
| 32 | self.start_time = time.time() |
| 33 | self.max_time = max_time |
| 34 | self.max_memory = max_memory |
| 35 | self.success_cost = success_cost # Inclusive |
| 36 | self.verbose = verbose |
| 37 | #self.best_cost = self.cost_fn(self.best_plan) |
| 38 | self.solutions = [] |
| 39 | self.sample_time = 0. |
| 40 | @property |
| 41 | def search_time(self): |
| 42 | return self.elapsed_time() - self.sample_time |
| 43 | @property |
| 44 | def best_plan(self): |
| 45 | # TODO: return INFEASIBLE if can prove no solution |
| 46 | return self.solutions[-1].plan if self.solutions else FAILED |
| 47 | @property |
| 48 | def best_cost(self): |
| 49 | return self.solutions[-1].cost if self.solutions else INF |
| 50 | def add_plan(self, plan, cost): |
| 51 | # TODO: double-check that plan is a solution |
| 52 | if is_plan(plan) and (cost < self.best_cost): |
| 53 | self.solutions.append(Solution(plan, cost, elapsed_time(self.start_time))) |
| 54 | def has_solution(self): |
| 55 | return is_plan(self.best_plan) |
| 56 | def is_solved(self): |
| 57 | return self.has_solution() and (self.best_cost <= self.success_cost) |
| 58 | def elapsed_time(self): |
| 59 | return elapsed_time(self.start_time) |
| 60 | def is_timeout(self): |
| 61 | return (self.max_time <= self.elapsed_time()) or not check_memory(self.max_memory) |
| 62 | def is_terminated(self): |
| 63 | return self.is_solved() or self.is_timeout() |
| 64 | #def __repr__(self): |
| 65 | # raise NotImplementedError() |
| 66 | def extract_solution(self): |
| 67 | SOLUTIONS[:] = self.solutions |
| 68 | return revert_solution(self.best_plan, self.best_cost, self.evaluations) |
| 69 | def export_summary(self): # TODO: log, etc... |
| 70 | # TODO: SOLUTIONS |
| 71 | #status = SUCCEEDED if self.is_solved() else FAILED # TODO: INFEASIBLE, OPTIMAL |
| 72 | return { |
| 73 | 'solved': self.is_solved(), |
| 74 | #'solved': self.has_solution(), |
| 75 | 'solutions': len(self.solutions), |
| 76 | 'cost': self.best_cost, |
| 77 | 'length': get_length(self.best_plan), |
| 78 | 'evaluations': len(self.evaluations), |
| 79 | 'search_time': self.search_time, |
| 80 | 'sample_time': self.sample_time, |
| 81 | 'run_time': self.elapsed_time(), |
| 82 | 'timeout': self.is_timeout(), |
no outgoing calls
no test coverage detected