Skip to content

Commit

Permalink
fix: Fixed single_run and added multiple_tr_chaining
Browse files Browse the repository at this point in the history
 - Created new method for Iterated Chaining method (also writes to a file)
- Fixed single_run function to now return the NFE value.
- Silenced all mid-run print_schedule and plot_scores, plot_fig all to False ( now all runs happen silently without printing or popping with a plot in btwn runs' did not match any file(s) known to git
  • Loading branch information
Agrover112 committed Aug 17, 2021
1 parent 033767b commit 5297d20
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions flightscheduling.py
Expand Up @@ -15,8 +15,8 @@
from fitness import *
from utils.ga_utils import multi_mutation, mutation
from utils.utils import people, plot_scores, print_schedule, read_file, time

matplotlib.use('TKAgg')
matplotlib.use('TKAgg') # Remove if not using WSL

_FLIGHTS_FILE_='flights.txt'

Expand Down Expand Up @@ -66,6 +66,21 @@ def multiple_runs(algorithm,domain,fitness_function, init=[], use_multiproc=Fals
f.close()
print("Total time ", round(sum(times), 3))

def multiple_runs_itr_chaining(algorithm_1, algorithm_2,domain,fitness_function,seed=random.randint(10,100), rounds=10 , n_obs=2, tol=90, save_fig=False,n=20):
res=list([])
seeds=[10,24,32,100,20,67,13,19,65,51,35,61,154,85,144,162,48,79,69,186]
if n>0 and n<20:
seeds=seeds[:n]
for i in seeds:
start=time.time()
final_soln,cost,scores,nfe = sol_chaining(algorithm_1,algorithm_2 ,domain,fitness_function,save_fig=True,seed=i,rounds=rounds,n_obs=n_obs,tol=tol)
res.append([cost,time.time()-start,nfe])

with open('./results/multi_proc/fitness_function/iterated_solution_chaining.csv','w+') as f:
f.write("Cost"+","+"Run_Time"+","+"NFE"+"\n")
for item in res:
f.write(str(item[0]) + "," + str(item[1]) +"," + str(item[2])+ "\n")


def single_run(algorithm,domain,fitness_function,init=[],seed=random.randint(10,100),seed_init=True,save_fig=False, print_sch=True):
global scores
Expand All @@ -81,66 +96,69 @@ def single_run(algorithm,domain,fitness_function,init=[],seed=random.randint(10,

if print_sch and fitness_function.__name__ == 'fitness_function':
print_schedule(soln, 'FCO')
return soln, cost
return soln, cost,nfe

def sol_chaining(algorithm_1, algorithm_2,domain,fitness_function,seed=random.randint(10,100), rounds=10 , n_obs=2, tol=90, save_fig=False):
# Note scores here is the best cost of each particular single_run
scores = []
NFE=0
for i in range(rounds):
if i == 0:
soln, cost = single_run(algorithm_1,domain,fitness_function, print_sch=False,seed=seed)
soln, cost,nfe = single_run(algorithm_1,domain,fitness_function,save_fig=True, print_sch=False,seed=seed)
soln=mutation(domain,random.randint(0,1),soln) # Either 1 step or no step InitMutation
#soln=multi_mutation(domain,1,soln)
scores.append(cost)
NFE+=nfe
print("Cost at {}=={}".format(i, cost))
elif i == rounds-1:
final_soln, cost = single_run(
algorithm_2,domain=domain,fitness_function=fitness_function, init=soln, save_fig=False, print_sch=True,seed=seed)
final_soln, cost,nfe = single_run(
algorithm_2,domain=domain,fitness_function=fitness_function, init=soln, save_fig=True, print_sch=False)
scores.append(cost)
NFE+=nfe
print("Cost at {}=={}".format(i, cost))
plot_scores(scores, sol_chaining.__name__, save_fig)
return final_soln, scores[-1], scores
return final_soln, scores[-1], scores,NFE
else:
soln, cost = single_run(
algorithm_1,domain=domain,fitness_function=fitness_function, init=init, save_fig=False, print_sch=False,seed=seed)
soln, cost,nfe = single_run(
algorithm_1,domain=domain,fitness_function=fitness_function, init=init, save_fig=True, print_sch=False)
print("Cost at {}=={}".format(i, cost))
soln=mutation(domain,random.randint(0,1),soln)
#soln=multi_mutation(domain,1,soln)
scores.append(cost)
NFE+=nfe

final_soln, cost = single_run(algorithm_2,domain=domain,fitness_function=fitness_function, init=soln, print_sch=False,seed=seed)
final_soln, cost,nfe = single_run(algorithm_2,domain=domain,fitness_function=fitness_function, init=soln,save_fig=True, print_sch=False)
scores.append(cost)
NFE+=nfe
if cost - random.randint(tol, 100) > int(sum(scores[-n_obs:])/n_obs):
print("----Ending early at iteration{}----".format(i))
print("Cost{}".format(cost))
if fitness_function.__name__ == 'fitness_function':
print_schedule(final_soln, 'FCO')
plot_scores(scores, sol_chaining.__name__, save_fig)
return final_soln, scores[-1], scores
#plot_scores(scores, sol_chaining.__name__, save_fig)
return final_soln, scores[-1], scores,NFE
print("Cost at {}=={}".format(i, cost))
init = mutation(domain,1,final_soln) # IntMutation



def main():
"""CHANGES In order:
10. Exception handling
"""
#print_schedule(soln,'FCO')
#multiple_runs(genetic_algorithm_with_reversals, n=20, use_multiproc=True,domain=domain['domain'],fitness_function=fitness_function)
#final_soln,cost,scores = sol_chaining(random_search,hill_climb ,domain=domain['domain'],fitness_function=fitness_function,save_fig=True,seed=10)
soln, cost = single_run(genetic_algorithm_with_reversals,domain['domain'],fitness_function,seed_init=False, save_fig=False, print_sch=True)
#print(soln,cost)
soln, cost,nfe = single_run(genetic_algorithm_with_reversals,domain['griewank']*13,griewank,seed_init=False, save_fig=False, print_sch=True)
print(soln,cost,nfe)
print_schedule(soln,'FCO')
#multiple_runs(simulated_annealing,domain[ 'matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(random_search,domain['matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(hill_climb,domain['matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm,domain['matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm_reversed,domain['matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm_with_reversals,domain['matyas'],matyas,n=20,use_multiproc=True)
#multiple_runs(hill_climb,domain['griewank']*13,griewank,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm,domain['three_hump_camel'],three_hump_camel,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm_reversed,domain['three_hump_camel'],three_hump_camel,n=20,use_multiproc=True)
#multiple_runs(genetic_algorithm_with_reversals,domain['three_hump_camel'],three_hump_camel,n=20,use_multiproc=True)
#soln,cost=single_run(simulated_annealing,save_fig=False,print_sch=False,domain=domain['griewank']*13,fitness_function=griewank,seed=10)

if __name__ == "__main__":
#CHANGES In order:
# 1. Exception handling
# 2. SHorten parameter names?
# 3. Change to multiproc functions/restructuring?
main()


0 comments on commit 5297d20

Please sign in to comment.