def load(self, entry): problem, algorithm, run = from_dict(entry, "problem", "algorithm", "run") entry = dict(entry) del entry["args"] path = os.path.join(self.folder, algorithm, problem) if os.path.exists(path): for key in ["X", "CV", "F"]: file = os.path.join(path, f"{run}.{key.lower()}") if os.path.exists(file): try: vals = np.loadtxt(file) except: return None if len(vals.shape) == 0: vals = np.array([float(vals)]) entry[key] = at_least2d(vals, expand="r") else: return None file = os.path.join(path, f"{run}.dat") if os.path.exists(file): entry["callback"] = pickle.load(open(file, 'rb')) return entry
def do(self, x, out, *args, **kwargs): super().do(x, out, *args, **kwargs) F, G = from_dict(out, "F", "G") assert G is not None, "To converge a function's constraint to objective it needs G to be set!" out["__F__"] = out["F"] out["__G__"] = out["G"] out["F"] = calc_constr(G, eps=self.eps, beta=1.0) del out["G"]
def run_looped(params, batch, verbose=False, loader=None, writer=None, extractor=None, mem_free=False, run_if_loading_fails=True, exception_if_not_available=False, **kwargs): ret = [] for i, param in enumerate(params): problem, algorithm, run, args = from_dict(param, "problem", "algorithm", "run", "args") if verbose: line = f"{i + 1}/{len(params)} | {algorithm} | {problem} | {run}" if batch is not None: line = f"{batch} | {line}" print(line, end="") entry = None if loader is not None: entry = loader.load(param) if entry is not None: if verbose: print(f" | Loaded") if entry is None: if run_if_loading_fails: res = execute(param) entry = extractor.extract(param, res) if verbose: print(f" | {np.round(res.exec_time, 6)} s") if writer is not None: writer.write(entry) else: if exception_if_not_available: assert entry is not None, f"Error while loading {param}" if verbose: print(f" | Failed") # if the algorithm should not keep anything in memory and just write files do that if entry is not None and not mem_free: ret.append(entry) return ret
def write(self, entry): problem, algorithm, run, callback = from_dict(entry, "problem", "algorithm", "run", "callback") folder = os.path.join(self.folder, algorithm, problem) if not os.path.exists(folder): os.makedirs(folder) for key in ["X", "CV", "F"]: np.savetxt(os.path.join(folder, f"{run}.{key.lower()}"), entry.get(key)) path = os.path.join(folder, f"{run}.json") with open(path, 'w') as f: json.dump(entry.get("info"), f, ensure_ascii=False, indent=4) if callback is not None: pickle.dump(callback, open(os.path.join(folder, f"{run}.dat"), 'wb'))
def do(self, data, scope=None, benchmark=None, inplace=False, **kwargs): assert benchmark is not None, "The benchmark is necessary to retrieve the known optimum of a function" problem = benchmark.problems[data["problem"]]["obj"] CV, F = from_dict(data, "CV", "F") igd = np.inf pf = problem.pareto_front(**kwargs) if pf is not None: igd = IGD(pf, zero_to_one=True).do(F) ret = { "pf": pf, "igd": igd, } if inplace: for k, v in ret.items(): data[k] = v return ret
def do(self, data, benchmark=None, inplace=False, **kwargs): assert benchmark is not None, "The benchmark is necessary to retrieve the known optimum of a funtion" problem = benchmark.problems[data["problem"]]["obj"] CV, F = from_dict(data, "CV", "F") f = F[0, 0] cv = CV[0, 0] fopt = problem.pareto_front() if fopt is not None: fopt = fopt[0, 0].astype(np.float) fgap = f - fopt ret = {"f": f, "cv": cv, "fopt": fopt, "fgap": fgap} if inplace: for k, v in ret.items(): data[k] = v return ret