def populate_from_pickles(all_solutions, pickleSrc): """ Load program traces, args and return variables from pickle files as created by the pipeline preprocessor. Create a Solution instance for each pickle file and add them to all_solutions. all_solutions: list to add solutions to pickleSrc: string, path to directory containing pickle files mutates all_solutions """ print "Loading data" for filename in os.listdir(pickleSrc): solnum = filename.split('.')[0] # print solnum with open(path.join(pickleSrc, filename), 'r') as f: unpickled = pickle.load(f) sol = Solution(solnum, unpickled['trace'], unpickled['args'], unpickled['returnVars']) all_solutions.append(sol)
def N1(N,M,P,c,a,b,F,s,ns,k): for i in range(1,int(F[s-1].Xn[0])+1): dom = 1 Ra = np.zeros((M)) Za = np.zeros((P+1)) for j in range(M): Ra[j] = F[s-1].R[j] + a[j][int(F[s-1].Xn[i])] if Ra[j] > b[j]: Za[P] = Za[P] + 1 if Za[P] > F[s-1].Z[P]: break if Za[P] <= F[s-1].Z[P]: if Za[P] < F[s-1].Z[P]: dom = 0 for p in range(P): Za[p] = F[s-1].Z[p] + c[p][int(F[s-1].Xn[i])] if Za[p] > F[s-1].Z[p]: dom = 0 if dom==0: X = F[s-1].X.copy() X[int(F[s-1].Xn[i])] = 1 Xn = F[s-1].Xn.copy() Xs = F[s-1].Xs.copy() Xs[0]=Xs[0]+1 Xs[int(Xs[0])]=F[s-1].Xn[i] Xn[0]=Xn[0]-1 for h in range(i,int(Xn[0]+2)): Xn[h]=Xn[h+1] F.append(Solution(X,Xs,Xn,Za,Ra,np.zeros((6)))) ns=ns+1 return F, k, ns
def Drand(N, M, P, c, a, b, F): X = F.X.copy() Xs = F.Xs.copy() Xn = F.Xn.copy() Z = F.Z.copy() R = F.R.copy() it = 0 while it < 0.3 * Xs[0]: it = it + 1 sel = random.randint(1, Xs[0]) Z[P] = 0 for m in range(M): R[m] = R[m] - a[m][int(Xs[sel])] if R[m] > b[m]: Z[P] = Z[P] + 1 for p in range(P): Z[p] = Z[p] - c[p][int(Xs[sel])] X[int(Xs[sel])] = 0 Xn[0] = Xn[0] + 1 Xn[int(Xn[0])] = Xs[sel] Xs[0] = Xs[0] - 1 for h in range(sel, int(Xs[0]) + 2): Xs[h] = Xs[h + 1] Fs = [] Fs.append(Solution(X, Xs, Xn, Z, R)) return Fs
def N3(N,M,P,c,a,b,F,s,ns,k): for i in range(1,int(F[s-1].Xs[0]+1)): for j in range(1,int(F[s-1].Xn[0]+1)): dom = 1 Ra = np.zeros((M)) Za = np.zeros((P+1)) for m in range(M): Ra[m] = F[s-1].R[m] - a[m][int(F[s-1].Xs[i])] + a[m][int(F[s-1].Xn[j])] if Ra[m] > b[m]: Za[P] = Za[P] + 1 if Za[P] > F[s-1].Z[P]: break if Za[P] <= F[s-1].Z[P]: if Za[P] < F[s-1].Z[P]: dom = 0 for p in range(P): Za[p] = F[s-1].Z[p] - c[p][int(F[s-1].Xs[i])] + c[p][int(F[s-1].Xn[j])] if Za[p] > F[s-1].Z[p]: dom = 0 if dom == 0: X = F[s-1].X.copy() X[int(F[s-1].Xs[i])] = 0 X[int(F[s-1].Xn[j])] = 1 Xn = F[s-1].Xn.copy() Xs = F[s-1].Xs.copy() Xn[j]=F[s-1].Xs[i] Xs[i]=F[s-1].Xn[j] F.append(Solution(X,Xs,Xn,Za,Ra,np.zeros((6)))) ns=ns+1 return F, k, ns
def N4(N,M,P,c,a,b,F,s,ns,k,cputime): for i1 in range(1,int(F[s-1].Xs[0]+1)): if random.random() < 0.84: for i2 in range(i1+1,int(F[s-1].Xs[0]+1)): if random.random() < 0.84: for j1 in range(1,int(F[s-1].Xn[0]+1)): if random.random() < 0.84: for j2 in range(j1+1,int(F[s-1].Xn[0]+1)): if random.random() < 0.84: dom = 1 Ra = np.zeros((M)) Za = np.zeros((P+1)) for m in range(M): Ra[m] = F[s-1].R[m] - a[m][int(F[s-1].Xs[i1])] - a[m][int(F[s-1].Xs[i2])] + a[m][int(F[s-1].Xn[j1])] + a[m][int(F[s-1].Xn[j2])] if Ra[m] > b[m]: Za[P] = Za[P] + 1 if Za[P] > F[s-1].Z[P]: break if Za[P] <= F[s-1].Z[P]: if Za[P] < F[s-1].Z[P]: dom = 0 for p in range(P): Za[p] = F[s-1].Z[p] - c[p][int(F[s-1].Xs[i1])] - c[p][int(F[s-1].Xs[i2])] + c[p][int(F[s-1].Xn[j1])] + c[p][int(F[s-1].Xn[j2])] if Za[p] > F[s-1].Z[p]: dom = 0 if dom == 0: X = F[s-1].X.copy() X[int(F[s-1].Xs[i1])] = 0 X[int(F[s-1].Xs[i2])] = 0 X[int(F[s-1].Xn[j1])] = 1 X[int(F[s-1].Xn[j2])] = 1 Xn = F[s-1].Xn.copy() Xs = F[s-1].Xs.copy() Xn[j1]=F[s-1].Xs[i1] Xs[i1]=F[s-1].Xn[j1] Xn[j2]=F[s-1].Xs[i2] Xs[i2]=F[s-1].Xn[j2] F.append(Solution(X,Xs,Xn,Za,Ra,np.zeros((6)))) ns=ns+1 if (time.time()-cputime) > 5*60: break if (time.time()-cputime) > 5*60: break if (time.time()-cputime) > 5*60: break if (time.time()-cputime) > 5*60: break return F, k, ns
def Rgreedy(N,M,P,c,a,b,UB,LB,Fp,F,ns,cputime): it = 0 while it < 5: it = it + 1 if it == 1: w = [0.9, 0.05, 0.05] if it == 2: w = [0.05, 0.9, 0.05] if it == 3: w = [0.05, 0.05, 0.9] if it >= 4: w = [random.random(), random.random(), random.random()] sw=sum(w) for i in range(P): w[i]=w[i]/sw X = Fp[0].X.copy() Xs = Fp[0].Xs.copy() Xn = Fp[0].Xn.copy() Z = Fp[0].Z.copy() R = Fp[0].R.copy() check = np.zeros((N)) for i in range(int(Xs[0]+1)): check[int(Xs[i])] = 1 ap = np.zeros((M, N)) if Z[P] > 0: fact = 0 else: fact = 1 Xn = np.zeros((N+1)) # Find a feasible solution while sum(check) < N and fact == 0: Rp = np.zeros((N)) Rn = np.ones((N)) sel = -1 for j in range(N): if check[j] == 0: for i in range(M): ap[i][j] = a[i][j] / (b[i]-R[i]+0.001) if b[i] >= 0 and R[i]+a[i][j] > b[i]: check[j] = 1 Xn[0] = Xn[0] + 1 Xn[int(Xn[0])] = j break if b[i] >= 0 and ap[i][j] > Rp[j]: Rp[j] = ap[i][j] if b[i] < 0 and ap[i][j] < Rn[j]: Rn[j] = ap[i][j] if check[j] == 0: if sel == -1: sel = j else: if Rn[j] > Rn[sel]: sel = j else: if Rn[j] == Rn[sel] and Rp[j] < Rp[sel]: sel = j if sel >= 0: X[sel] = 1 Xs[0] = Xs[0] + 1 Xs[int(Xs[0])] = sel check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] # Find a better solution while sum(check) < N and fact == 1: Rp = np.ones((N)) * np.Infinity sel = -1 for j in range(N): if check[j] == 0: Rp[j] = 0 for p in range(P): ap[p][j] = (Z[p]+c[p][j]-LB[p])/(UB[p]-LB[p]) Rp[j] = Rp[j] + w[p]*ap[p][j] for i in range(M): if R[i]+a[i][j] > b[i]: check[j] = 1 Xn[0] = Xn[0] + 1 Xn[int(Xn[0])] = j break if check[j] == 0: if sel == -1: sel = j else: if Rp[j] > Rp[sel]: sel = j if sel >= 0: X[sel] = 1 Xs[0] = Xs[0] + 1 Xs[int(Xs[0])] = sel check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] Z[P] = 0 for i in range(M): if R[i]>b[i]: Z[P] = Z[P] +1 F.append(Solution(X,Xs,Xn,Z,R)) ns = ns + 1 return F, ns
def Rexact2(N,M,P,c,a,b,UB,LB,Fp,F,ns,cputime): it = 0 while it < 5: it = it + 1 if it == 1: w = [0.9, 0.05, 0.05] if it == 2: w = [0.05, 0.9, 0.05] if it == 3: w = [0.05, 0.05, 0.9] if it >= 4: w = [random.random(), random.random(), random.random()] sw=sum(w) for i in range(P): w[i]=w[i]/sw X = Fp[0].X.copy() Xs = Fp[0].Xs.copy() Xn = Fp[0].Xn.copy() Z = Fp[0].Z.copy() R = Fp[0].R.copy() mod = Model("Opt_KSP") Xg = mod.addVars(N, vtype=GRB.BINARY, name="Xg") Zg = mod.addVars(P, vtype=GRB.CONTINUOUS, name="Zg") Pg = mod.addVars(M, lb=0, vtype=GRB.CONTINUOUS, name="Pg") mod.setParam(GRB.Param.OutputFlag, 0) mod.setParam(GRB.Param.TimeLimit, 3) mod.update() name = "c1" for m in range(M): mod.addConstr(quicksum(a[m][j]*Xg[j] for j in range(N) ) <= b[m] + quicksum(Pg[j] for j in range(M) ), name=name) name = "c2" for i in range(N): mod.addConstr(Xg[i] <= X[i], name=name) name = "c3" for p in range(P): mod.addConstr(Zg[p] == quicksum(c[p][j]*Xg[j] for j in range(N)), name=name) mod.setObjective(quicksum(w[p]*(Zg[p]-LB[p])/(UB[p]-LB[p]) for p in range(P)) - quicksum(Pg[m] for m in range(M)), GRB.MAXIMIZE) mod.update() mod.optimize() Xs = np.zeros((N+1)) Xn = np.zeros((N+1)) Z = np.zeros((P+1)) R = np.zeros((M)) for i in range(N): X[i] = Xg[i].X if X[i] >= 0.95: X[i] = 1 Xs[0]=Xs[0]+1 Xs[int(Xs[0])]=i for p in range(P): Z[p] = Z[p] + c[p][i] for m in range(M): R[m] = R[m] + a[m][i] else: X[i]=0 Xn[0]=Xn[0]+1 Xn[int(Xn[0])]=i Z[P] = 0 for m in range(M): if R[m] > b[m]: Z[P] = Z[P] + 1 F.append(Solution(X,Xs,Xn,Z,R)) ns = ns + 1 return F, ns
class Api: def __init__(self): # config self.names = None self.cleaning_method = None self.problem_index = None self.algorithm = None self.repr_method = None self.score_method = None self.data_sets = None self.problem = None self.solution = None # settings self.first_line = 0 self.last_line = -1 self.do_save_solution = True self.do_show = True self.do_report = False self.do_report_time = True def set(self, attribute, value): """override attribute, only if value is not None""" if value is not None: if hasattr(self, attribute): setattr(self, attribute, value) def report(self, *args, inline=1): if self.do_report: print('\n' * inline, '>>> ', ' '.join([str(i) for i in args])) def report_time(self, *args): if self.do_report_time: print('\n', '>>> ', ' '.join([str(i) for i in args])) def compile(self, file_names=None, cleaning_method=None, problem_index=None, algorithm=None, repr_method=None, score_method=None): """set methods (None = don't change)""" self.set('names', file_names) self.set('cleaning_method', cleaning_method) self.set('problem_index', problem_index) self.set('algorithm', algorithm) self.set('repr_method', repr_method) self.set('score_method', score_method) return self def settings(self, first_line=None, last_line=None, do_save_solution=None, do_show=None, do_report=None, do_report_time=None): """set variables (None = don't change)""" self.set('first_line', first_line) self.set('last_line', last_line) self.set('do_save_solution', do_save_solution) self.set('do_show', do_show) self.set('do_report', do_report) self.set('do_report_time', do_report_time) return self def set_algorithm(self, algorithm): self.algorithm = algorithm def set_problem(self, problem_index): self.problem_index = problem_index def set_first_line(self, first_line): self.first_line = first_line def set_last_line(self, last_line): self.last_line = last_line def set_do_show(self, do_show): self.do_show = do_show # --- checks ------------------------------------------------------------------------ def check_defined(self, *args, error_type=None): """checks that both args and kwargs are both not None, in witch case raises error_type""" if error_type is None: error_type = NotImplementedError not_defined_attr = [] error = None # scan for attr in args: if hasattr(self, attr): if getattr(self, attr) is None: not_defined_attr += [attr] error = error_type(attr) else: not_defined_attr += [attr] error = AttributeError(attr) # report if len(not_defined_attr): self.report('Warning! Argument not defined:') for attr in not_defined_attr: self.report(' - ' + attr, inline=0) if error: raise error def optional(*args): """decorated methods are skipped if some parameter is missing""" def wrapped(fun): def wrapped_2(self, *ag, **kw): self.check_defined(*args, error_type=AssertionError) return fun(self, *ag, **kw) wrapped_2.__name__ = fun.__name__ + '_' return wrapped_2 return wrapped def compulsory(*args): """decorated methods stop the routine execution if some parameter is missing""" def wrapped(fun): def wrapped_2(self, *ag, **kw): self.check_defined(*args, error_type=NotImplementedError) return fun(self, *ag, **kw) wrapped_2.__name__ = fun.__name__ + '_' return wrapped_2 return wrapped # --- functionalities --------------------------------------------------------------- @compulsory('names', 'first_line', 'last_line', 'cleaning_method') def gen_data_sets(self): """dict of names with corresponding data-set generator""" self.data_sets = {i: data_points_generator(file_name=self.names[i], first_line=self.first_line, last_line=self.last_line, cleaning_method=self.cleaning_method) for i in self.names} @compulsory('data_sets', 'problem_index') def define_problem(self): self.problem = list(self.data_sets[self.problem_index]()) # when called returns data generator if self.do_show: fancy_print(self.data_sets[self.problem_index](), lines=10, title='problem ' + self.problem_index) @compulsory('problem', 'repr_method', 'algorithm') def compute_solution(self): # compute solution raw_solution = self.algorithm(self.problem) self.solution = Solution(raw_solution, self.problem, self.repr_method) @optional('solution', 'problem_index', 'score_method') def compute_score(self): self.solution.set_score_method(self.score_method) score = self.solution.get_score() if self.do_show: fancy_print(self.solution, lines=10, title='solution ' + self.problem_index) print('\n\tscore =', score, '\n') @optional('problem_index') def save(self): if self.do_save_solution: self.solution.save(name=self.problem_index) # --- routine ----------------------------------------------------------------------- def routine(self): """generator of methods to execute - enforces sequencing - returns control to user to allow interleaving code""" v = [self.gen_data_sets, self.define_problem, self.compute_solution, self.compute_score, self.save] def routine_generator(): for method in v: yield method return routine_generator def run(self, min_time=0.005): """ routine execution :param min_time: min time required that a method needs to take in order to report time (seconds) :return: """ self.report('running Api...') successful = True for method in self.routine()(): timer = Timer() try: self.report('calling "' + method.__name__ + '"') method() except NotImplementedError: # handle compulsory methods self.report('routine aborted at "' + method.__name__ + '"') successful = False break except AssertionError: # handle optional methods self.report('...skipping', method.__name__) pass finally: if self.do_report_time: t = timer(method.__name__).lapse # report only meaningful time if t > min_time: self.report_time(str(timer)) if successful: self.report('Api has been successfully executed!') else: self.report('Api has been shut down due to a lack of arguments!')
def compute_solution(self): # compute solution raw_solution = self.algorithm(self.problem) self.solution = Solution(raw_solution, self.problem, self.repr_method)
def construction(N,M,P,c,a,b): X = np.zeros((N)) R = np.zeros((M)) Z = np.zeros((P+1)) UB = np.zeros((P)) for p in range(P): for j in range(N): if c[p][j] > 0: UB[p] = UB[p] + c[p][j] check = np.zeros((N)) ap = np.zeros((M, N)) fact = 0 # Find a feasible solution while sum(check) < N and fact == 0: Rp = np.zeros((N)) Rn = np.ones((N)) sel = -1 for j in range(N): if check[j] == 0: for i in range(M): ap[i][j] = a[i][j] / (b[i]-R[i]+0.001) if b[i] >= 0 and R[i]+a[i][j] > b[i]: check[j] = 1 break if b[i] >= 0 and ap[i][j] > Rp[j]: Rp[j] = ap[i][j] if b[i] < 0 and ap[i][j] < Rn[j]: Rn[j] = ap[i][j] if check[j] == 0: if sel == -1: sel = j else: if Rn[j] > Rn[sel]: sel = j else: if Rn[j] == Rn[sel] and Rp[j] < Rp[sel]: sel = j if sel >= 0: X[sel] = 1 check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] # Find a better solution while sum(check) < N and fact == 1: Rp = np.ones((N)) * np.Infinity sel = -1 for j in range(N): if check[j] == 0: for p in range(P): ap[p][j] = (UB[p]-Z[p])/UB[p] if Rp[j] > ap[p][j]: Rp[j] = ap[p][j] for i in range(M): if R[i]+a[i][j] > b[i]: check[j] = 1 break if check[j] == 0: if sel == -1: sel = j else: if Rp[j] > Rp[sel]: sel = j if sel >= 0: X[sel] = 1 check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] Z[p+1] = 0 for i in range(M): if R[i]>b[i]: Z[p+1] = Z[p+1] +1 Xs = np.zeros((N+1)) Xn = np.zeros((N+1)) for i in range(N): if X[i]==1: Xs[0]=Xs[0]+1 Xs[int(Xs[0])] = i else: Xn[0]=Xn[0]+1 Xn[int(Xn[0])] = i F = Solution(X,Xs,Xn,Z,R) return F
def Rgreedy(N,M,P,c,a,b,Fp,F,cputime): X = Fp[0].X.copy() Xs = Fp[0].Xs.copy() Xn = Fp[0].Xn.copy() Z = Fp[0].Z.copy() R = Fp[0].R.copy() check = np.zeros((N)) for i in range(int(Xs[0]+1)): check[int(Xs[i])] = 1 ap = np.zeros((M, N)) if Z[P] > 0: fact = 0 else: fact = 1 Xn = np.zeros((N+1)) # Find a feasible solution while sum(check) < N and fact == 0: Rp = np.zeros((N)) Rn = np.ones((N)) sel = -1 for j in range(N): if check[j] == 0: for i in range(M): ap[i][j] = a[i][j] / (b[i]-R[i]+0.001) if b[i] >= 0 and R[i]+a[i][j] > b[i]: check[j] = 1 Xn[0] = Xn[0] + 1 Xn[int(Xn[0])] = j break if b[i] >= 0 and ap[i][j] > Rp[j]: Rp[j] = ap[i][j] if b[i] < 0 and ap[i][j] < Rn[j]: Rn[j] = ap[i][j] if check[j] == 0: if sel == -1: sel = j else: if Rn[j] > Rn[sel]: sel = j else: if Rn[j] == Rn[sel] and Rp[j] < Rp[sel]: sel = j if sel >= 0: X[sel] = 1 Xs[0] = Xs[0] + 1 Xs[int(Xs[0])] = sel check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] # Find a better solution while sum(check) < N and fact == 1: Rp = np.ones((N)) * np.Infinity sel = -1 for j in range(N): if check[j] == 0: Rp[j] = 0 for p in range(P): ap[p][j] = Z[p]+c[p][j] Rp[j] = Rp[j] + ap[p][j] for i in range(M): if R[i]+a[i][j] > b[i]: check[j] = 1 Xn[0] = Xn[0] + 1 Xn[int(Xn[0])] = j break if check[j] == 0: if sel == -1: sel = j else: if Rp[j] > Rp[sel]: sel = j if sel >= 0: X[sel] = 1 Xs[0] = Xs[0] + 1 Xs[int(Xs[0])] = sel check[sel] = 1 fact = 1 for i in range(M): R[i] = R[i] + a[i][sel] if fact == 1 and R[i] > b[i]: fact = 0 for p in range(P): Z[p] = Z[p] + c[p][sel] Z[P] = 0 for i in range(M): if R[i]>b[i]: Z[P] = Z[P] +1 if Z[P] < F.Z[P]: F = Solution(X,Xs,Xn,Z,R) if Z[P] == F.Z[P] and Z[0] > F.Z[0]: F = Solution(X,Xs,Xn,Z,R) return F
def Rexact(N,M,P,c,a,b,Fp,F,cputime): X = Fp[0].X.copy() Xs = Fp[0].Xs.copy() Xn = Fp[0].Xn.copy() Z = Fp[0].Z.copy() R = Fp[0].R.copy() mod = Model("Opt_KSP") Xg = mod.addVars(N, vtype=GRB.BINARY, name="Xg") Zg = mod.addVars(P, vtype=GRB.CONTINUOUS, name="Zg") Pg = mod.addVars(M, lb=0, vtype=GRB.CONTINUOUS, name="Pg") mod.setParam(GRB.Param.OutputFlag, 0) mod.update() name = "c1" for m in range(M): mod.addConstr(quicksum(a[m][j]*Xg[j] for j in range(N) ) <= b[m] + Pg[m], name=name) name = "c2" for i in range(N): mod.addConstr(Xg[i] >= X[i], name=name) name = "c3" for p in range(P): mod.addConstr(Zg[p] == quicksum(c[p][j]*Xg[j] for j in range(N)), name=name) mod.setObjective(Zg[0] - 100*quicksum(Pg[m] for m in range(M)), GRB.MAXIMIZE) mod.update() mod.optimize() Xs = np.zeros((N+1)) Xn = np.zeros((N+1)) Z = np.zeros((P+1)) R = np.zeros((M)) for i in range(N): X[i] = Xg[i].X if X[i] >= 0.95: X[i] = 1 Xs[0]=Xs[0]+1 Xs[int(Xs[0])]=i for p in range(P): Z[p] = Z[p] + c[p][i] for m in range(M): R[m] = R[m] + a[m][i] else: X[i]=0 Xn[0]=Xn[0]+1 Xn[int(Xn[0])]=i Z[P] = 0 for m in range(M): if R[m] > b[m]: Z[P] = Z[P] + 1 if Z[P] < F.Z[P]: F = Solution(X,Xs,Xn,Z,R) if Z[P] == F.Z[P] and Z[0] > F.Z[0]: F = Solution(X,Xs,Xn,Z,R) return F