implemented mult iterations support
This commit is contained in:
		
							
								
								
									
										72
									
								
								al2/plan.py
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								al2/plan.py
									
									
									
									
									
								
							| @@ -28,13 +28,33 @@ class Plan: | |||||||
|             self.__load() |             self.__load() | ||||||
|  |  | ||||||
|     def __create(self): |     def __create(self): | ||||||
|  |         content = self.__create_content() | ||||||
|  |  | ||||||
|  |         self.pending_instances = content["pending"] | ||||||
|  |         self.iterations_left = content["iterations_left"] | ||||||
|  |  | ||||||
|  |         with self.__lock: | ||||||
|  |             self.__update_file() | ||||||
|  |  | ||||||
|  |     def __create_content(self, iterations_left = None): | ||||||
|  |         content = {} | ||||||
|  |  | ||||||
|         with open(self.experiment, "r") as expf: |         with open(self.experiment, "r") as expf: | ||||||
|             exp_obj = json.loads(expf.read()) |             exp_obj = json.loads(expf.read()) | ||||||
|  |  | ||||||
|             instances = batch.load(pl.Path(exp_obj["batch"]).resolve()) |             instances = batch.load(pl.Path(exp_obj["batch"]).resolve()) | ||||||
|              |              | ||||||
|         self.pending_instances = instances |             if iterations_left == None: | ||||||
|         self.__update_file() |                 if "iterations" in exp_obj: | ||||||
|  |                     iterations_left = exp_obj["iterations"] - 1 | ||||||
|  |                 else: | ||||||
|  |                     iterations_left = 0 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         content["pending"] = instances | ||||||
|  |         content["iterations_left"] = iterations_left | ||||||
|  |  | ||||||
|  |         return content | ||||||
|  |  | ||||||
|  |  | ||||||
|     def __set_file(self): |     def __set_file(self): | ||||||
| @@ -60,6 +80,9 @@ class Plan: | |||||||
|             if "pending" in content: |             if "pending" in content: | ||||||
|                 self.pending_instances = content["pending"] |                 self.pending_instances = content["pending"] | ||||||
|  |  | ||||||
|  |             if "iterations_left" in content: | ||||||
|  |                 self.iterations_left = content["iterations_left"] | ||||||
|  |              | ||||||
|     def __is_finished(self): |     def __is_finished(self): | ||||||
|         return False if self.file.is_file() else True |         return False if self.file.is_file() else True | ||||||
|  |  | ||||||
| @@ -69,7 +92,10 @@ class Plan: | |||||||
|             self.__load() |             self.__load() | ||||||
|  |  | ||||||
|             if len(self.pending_instances) == 0: |             if len(self.pending_instances) == 0: | ||||||
|                 return None |                 if self.iterations_left > 0: | ||||||
|  |                     self.__load_next_iteration() | ||||||
|  |                 else: | ||||||
|  |                     return None | ||||||
|              |              | ||||||
|             next_instance = self.pending_instances.pop() |             next_instance = self.pending_instances.pop() | ||||||
|             self.assigned_instances.append(next_instance) |             self.assigned_instances.append(next_instance) | ||||||
| @@ -91,20 +117,44 @@ class Plan: | |||||||
|     def __update_file(self): |     def __update_file(self): | ||||||
|         content = {} |         content = {} | ||||||
|          |          | ||||||
|  |         all_done = True | ||||||
|  |  | ||||||
|  |         content["iterations_left"] = self.iterations_left | ||||||
|  |  | ||||||
|         if len(self.assigned_instances) > 0: |         if len(self.assigned_instances) > 0: | ||||||
|             content["assigned"] = list(map(str, self.assigned_instances)) |             content["assigned"] = self.assigned_instances | ||||||
|  |             all_done = False | ||||||
|  |  | ||||||
|         if len(self.pending_instances) > 0: |         if len(self.pending_instances) > 0: | ||||||
|             content["pending"] = list(map(str, self.pending_instances)) |             content["pending"] = self.pending_instances | ||||||
|  |             all_done = False | ||||||
|    |    | ||||||
|         if content: |         if all_done: | ||||||
|             with open(self.file, "w") as pfile: |             if self.iterations_left > 0: | ||||||
|                 pfile.write(json.dumps(content)) |                 self.__load_next_iteration() | ||||||
|  |             elif self.file.is_file(): | ||||||
|  |                 self.file.unlink() | ||||||
|  |         else: | ||||||
|  |             self.__write_content(content) | ||||||
|  |  | ||||||
|         elif self.file.is_file(): |     def __load_next_iteration(self):  | ||||||
|             self.file.unlink() |         content = self.__create_content(self.iterations_left - 1) | ||||||
|  |          | ||||||
|  |         self.pending_instances = content["pending"] | ||||||
|  |         self.iterations_left = content["iterations_left"] | ||||||
|  |  | ||||||
|  |         self.__write_content(content) | ||||||
|  |  | ||||||
|  |     def __write_content(self, content): | ||||||
|  |         if "assigned" in content: | ||||||
|  |             content["assigned"][:] = map(str, content["assigned"]) | ||||||
|  |  | ||||||
|  |         if "pending" in content: | ||||||
|  |             content["pending"][:] = map(str, content["pending"]) | ||||||
|  |  | ||||||
|  |         with open(self.file, "w") as pfile: | ||||||
|  |             pfile.write(json.dumps(content)) | ||||||
|      |      | ||||||
|     #def __del__(self): |  | ||||||
|     def delete(self): |     def delete(self): | ||||||
|         with self.__lock: |         with self.__lock: | ||||||
|             self.__load() |             self.__load() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user