def INC_algo(self) : # line 6-26 self.status_log(self.A) for i in range(self.k) : top_assignment = self.get_top_assignment(self.M) printer(("top_assignment: ",self.E[top_assignment.event], self.T[top_assignment.time_interval]),verbose=self.verbose) self.update_validity(top_assignment) #self.status_log(self.A) #self.L_i[top_assignment.time_interval].l.remove(top_assignment) self.S.append(top_assignment) self.update__L_i(top_assignment) self.update_M(top_assignment) self.bound = self.get_bound() printer((" status_log 1 "),verbose=self.verbose) self.status_log(self.A) self.update_assignments(top_assignment) printer((" status_log 2 "),verbose=self.verbose) self.status_log(self.A)
def greedy_alg(self, k=3): self.generate_assigment(list(range(len(self.E))), list(range(len(self.T)))) self.assign_score() self.status_log() for i in range(k): best_assignment = self.select_assignment() printer(best_assignment, verbose=self.verbose) printer( ("Selection:", best_assignment.event, ' ', best_assignment.time_interval, ' ', best_assignment.score), verbose=self.verbose) printer(("Selection:", self.E[best_assignment.event], ' ', self.T[best_assignment.time_interval], ' ', best_assignment.score), verbose=self.verbose) self.S.append(best_assignment) self.remove_assignment(self.A, best_assignment) self.update_assignment(self.A, best_assignment) self.status_log()
def printer_updated_assignments(self,UA_list): printer("Updated Assignments: ",end = ' ',verbose=self.verbose) for j in UA_list : if j.update : printer(self.printer_assignment(j),end = " ",verbose=self.verbose) printer("\n\n",verbose=self.verbose)
def status_log(self,assignment_list) : printer("\n",verbose=self.verbose) printer("\n",verbose=self.verbose) printer("-------------------------------------------------------------",verbose=self.verbose) printer("Event Time Interval Score Location Validity",verbose=self.verbose) for i in assignment_list : if len(str(i.score)) >= 5 : printer((self.E[i.event], ' ', self.T[i.time_interval], ' ', '{:.5}'.format(str(i.score)), '', '{:7}'.format(i.location), ' ', i.valid),verbose=self.verbose) else : printer((self.E[i.event], ' ', self.T[i.time_interval], ' ', '{:5}'.format(str(i.score)), '', '{:7}'.format(i.location), ' ', i.valid),verbose=self.verbose) printer(("Bound: ",self.printer_assignment(self.bound), " ", self.bound.score),verbose=self.verbose) printer(("M: ", list(map(self.printer_assignment,self.M)),"\n"),verbose=self.verbose) printer(("L_1: ",list(map(self.printer_assignment,self.L_i[0].l))," update: ",self.L_i[0].update),verbose=self.verbose) printer(("L_2: ", list(map(self.printer_assignment,self.L_i[1].l))," update: ",self.L_i[1].update,"\n"),verbose=self.verbose) printer(("--------------------------------------------------------------"),verbose=self.verbose) printer("\n",verbose=self.verbose) printer("\n",verbose=self.verbose)
def update_assignments(self,top_assignment): update_assignment_list = [] for i in range(len(self.T)): if self.bound.score == "unavailable" : max = Assignment() for j in range(len(self.A)) : if self.A[j].valid and self.A[j].score > max.score : max = self.A[j] self.update_score(max,top_assignment) max.update = True update_assignment_list.append(max) self.bound = max break if self.L_i[i].update==False and self.M[i].score<=self.bound.score: #printer(i) j = 0 while j < len(self.L_i[i].l) : # if self.L_i[i].l[j].valid==False: self.L_i[i].l.pop(j) elif self.L_i[i].l[j].update==False and self.L_i[i].l[j].score >=self.bound.score: self.update_score(self.L_i[i].l[j],top_assignment) self.L_i[i].l[j].update=True update_assignment_list.append(self.L_i[i].l[j]) self.M[i] = self.getBetterAssignment(self.M[i],self.L_i[i].l[j]) self.bound = self.getBetterAssignment(self.bound,self.L_i[i].l[j]) j += 1 temp=0 for j in self.L_i[i].l: if j.update==False: temp=1 break if temp==0: self.L_i[i].update=True for i in range(len(self.M)): #max = Assignment() printer(("M[i] = ", self.printer_assignment(self.M[i])),verbose=self.verbose) for j in range(len(self.L_i[i].l)): printer(("L_i.l= ",self.printer_assignment(self.L_i[i].l[j]) ),verbose=self.verbose) if self.L_i[i].l[j].valid == True and self.L_i[i].l[j].update == True : self.M[i] = self.getBetterAssignment(self.M[i],self.L_i[i].l[j]) self.printer_updated_assignments(update_assignment_list)
def update_assignment(self, A, best_assignment): printer("\n", verbose=self.verbose) printer("Assignments to be updated: ", verbose=self.verbose) printer("\n", verbose=self.verbose) for i in A: if i.time_interval == best_assignment.time_interval and i.valid: printer(('(', i.time_interval, ' a ', i.event, ')'), end=' ', verbose=self.verbose) i.score = self.update_score(i, best_assignment) printer("\n", verbose=self.verbose) printer("\n", verbose=self.verbose)
def generator(n): u=0 e=0 t=1 l=0 K=0 U=[] E=[] T=[] L=[] sigma=[[]] mu_E=[[]] mu_C=[[]] ui=0 ei=0 ti=0 li=0 f=0 fnames=[] t_gre=[] t_inc=[] t_hor=[] t_hor_i=[] e_list=[] pperc=0 print("Generating...") while e<=n: u+=random.randint(1,4) #u should preferably grow fastest e_inc=random.randint(1,3) e+=e_inc t+=random.randint(1,e_inc) #t should preferably grow slower than e l+=e_inc #l should grow at the same rate as e K+=random.randint(1,e_inc) #k should always be less than e for i in range(ui,u): U.append('u'+str(i)) ui=u for i in range(ei,e): E.append('e'+str(i)) ei=e for i in range(ti,t): T.append('t'+str(i)) ti=t for i in range(li,l): L.append('loc'+str(i)) li=l for k in range(len(sigma)): for i in range(len(sigma[k]),len(T)): sigma[k].append(round(random.uniform(0.1,1),1)) for k in range(len(mu_C)): for i in range(len(mu_C[k]),len(T)): mu_C[k].append(round(random.uniform(0.1,1),1)) for k in range(len(mu_E)): for i in range(len(mu_E[k]),len(E)): mu_E[k].append(round(random.uniform(0.1,1),1)) for i in range(len(sigma),len(U)): sigma.append([round(random.uniform(0.1,1),1) for k in range(len(T))]) for i in range(len(mu_C),len(U)): mu_C.append([round(random.uniform(0.1,1),1) for k in range(len(T))]) for i in range(len(mu_E),len(U)): mu_E.append([round(random.uniform(0.1,1),1) for k in range(len(E))]) #print("K:",K) #print("U:",U) #print("E:",E) #print("T:",T) #print("L:",L) #print("Sigma:", sigma) #print("mu_E:", mu_E) #print("mu_C:", mu_C) e_list.append(e) t_gre.append(measureGRE(K,U,E,T,L,sigma,mu_E,mu_C,False)) t_inc.append(measureINC(K,U,E,T,L,sigma,mu_E,mu_C,False)) t_hor.append(measureHOR(K,U,E,T,L,sigma,mu_E,mu_C,False)) t_hor_i.append(measureHOR_I(K,U,E,T,L,sigma,mu_E,mu_C,False)) #print("---------------------------------------------------------") perc=math.ceil((e/n)*100) while pperc!=perc: pperc+=1 print("█", end='',flush=True) inputs={ "No of events":e_list, "GRE":t_gre, "HOR":t_hor, "INC": t_inc, "HOR_I": t_hor_i, } sampler=pd.DataFrame({key:pd.Series(value) for key, value in inputs.items()}) while os.path.exists("data_%s.csv" % f): f+=1 fname="data_"+str(f)+".csv" sampler.to_csv(fname, index=False) print("\nFile written: ") print(fname, end=' ') df_GRE=sampler[['No of events','GRE']] df_INC=sampler[['No of events','INC']] df_HOR=sampler[['No of events','HOR']] df_HOR_I=sampler[['No of events','HOR_I']] print("Graphing...") #Graphing sns.set(style='whitegrid', font='Calibri',palette='Reds_r') GREplot=sns.lineplot(x='No of events',y='GRE', data=df_GRE) GREplot.set_title('Greedy') GREfig=GREplot.get_figure() GREfig.savefig('GRE.png') plt.clf() printer("████████████████████", end='') sns.set(style='whitegrid', font='Calibri',palette='Greens_r') INCplot=sns.lineplot(x='No of events',y='INC', data=df_INC) INCplot.set_title('INC') INCfig=INCplot.get_figure() INCfig.savefig('INC.png') plt.clf() printer("████████████████████", end='') sns.set(style='whitegrid', font='Calibri',palette='Blues_r') HORplot=sns.lineplot(x='No of events',y='HOR', data=df_HOR) HORplot.set_title('HOR') HORfig=HORplot.get_figure() HORfig.savefig('HOR.png') plt.clf() printer("████████████████████", end='') sns.set(style='whitegrid', font='Calibri',palette='cubehelix') HOR_Iplot=sns.lineplot(x='No of events',y='HOR_I', data=df_HOR_I) HOR_Iplot.set_title('HOR_I') HOR_Ifig=HOR_Iplot.get_figure() HOR_Ifig.savefig('HOR_I.png') plt.clf() printer("████████████████████", end='') melted=pd.melt(sampler,id_vars="No of events", value_name='time',var_name='Algorithms') allplot=sns.lineplot(x='No of events',y='time', hue='Algorithms', style='Algorithms', data=melted) sns.set(style='whitegrid', font='Calibri',palette='cubehelix') allfig=allplot.get_figure() allfig.savefig('all.png') plt.clf() printer("████████████████████", end='')