#!/usr/bin/env python import helper as h import numpy as np import pandas as pd import csv, scipy, json import warnings warnings.filterwarnings("ignore") np.set_printoptions(precision=3, suppress=True) # Parameters repetitions = 10 datasets = h.datasets() clfs = h.classifiers().keys() results = h.results() measures = ["t", "w"] cv_methods = ["k10", "k20", "k30", "k2x5"] p_s = h.p_s() for i, clf in enumerate(clfs): collisions = [] print("---\n%s [%i]" % (clf, i)) for measure in measures: for p in p_s: for cv_method in cv_methods: for r in range(repetitions): db_count = 0 dbs = [] for dataset in datasets: dbname = dataset[1] filename = "jsons/%s_r%i_%s_p%i.json" % (dbname,r,cv_method,int(p*100))
def hill_climber(cargo_list_csv, slice, boundaries, number): """ Hill climber used for questions a, b and c Swaps parcels between spaceships and cargolists """ # load the first cargo list cargo1_list = hlp.load_csv_cargolist(str(cargo_list_csv)) # slice list and sort it on kg/m3 ratio combined_list = hlp.sort_and_slice(cargo1_list, slice) cargo1_list = combined_list[0][:] remaining_list = combined_list[1][:] # define properties of the spacecrafts Cygnus = Spacecraft(2000, 18.9, 7400, 390000000, 0.73) Progress = Spacecraft(2400, 7.6, 7020, 175000000, 0.74) Kounotori = Spacecraft(5200, 14, 10500, 420000000, 0.71) Dragon = Spacecraft(6000, 10, 12200, 347000000, 0.72) TianZhou = Spacecraft(6500, 15, 13500, 412000000, 0.75) Verne_ATV = Spacecraft(7500, 48, 20500, 1080000000, 0.72) # create a list containing all the spaceships spacecrafts = [Cygnus, Dragon, Kounotori, Progress] # sort list on kg/m3 ratio spacecrafts = sorted(spacecrafts, key=lambda spacecraft: spacecraft.ratio) # experiment: start hill climber with some cargo already pre-loaded hlp.preload_spacecrafts(cargo1_list, spacecrafts, remaining_list, boundaries) # organize data with list # make copy of list also for the hill climber spacecrafts.append(remaining_list) copy_spacecrafts = spacecrafts[:] # counter variables counter = 0 number_found = 0 cost = 0 previous_len = 0 total_len = 0 # list needed to save data for matplotlib counter_list = [] total_len_list = [] while counter < number: # total_len < 90: # counter needed for the visualization counter += 1 counter_list.append(counter) # boolean which can turn true if the hill climber wants it to accept = False # copy all list incase hill climber wants to go back alt_Cygnus = copy.deepcopy(Cygnus.cargo_list) alt_Dragon = copy.deepcopy(Dragon.cargo_list) alt_Kounotori = copy.deepcopy(Kounotori.cargo_list) alt_Progress = copy.deepcopy(Progress.cargo_list) # choose random object random_object = random.choice(copy_spacecrafts) random_object2 = random.choice(copy_spacecrafts) # check if random object is not selected twice if random_object != random_object2: # if random object is a list, it is the list containing the remaining parcels # loop to load these remaining parcels into a random spacecraft if type(random_object) == list: accept = hlp.swap_list_spacecraft(random_object, random_object2) elif type(random_object2) == list: accept = hlp.swap_list_spacecraft(random_object2, random_object) else: accept = hlp.swap_between_spacecraft(random_object, random_object2) # double-check total length cargo float and remaining weight and volume result_dic = hlp.results(spacecrafts) total_len = result_dic["length"] total_cost = result_dic["cost"] total_filled = result_dic["total_filled"] # check if score will be accepted if (accept == True): if ((total_len > previous_len) or (total_cost < cost)): previous_len = total_len cost = total_cost if type(random_object) != list: random_object.cargo_list = random_object.cargo_list if type(random_object2) != list: random_object2.cargo_list = random_object2.cargo_list else: accept == False # if new situation is not better return to old situation if (accept == False) or (total_len < previous_len): Cygnus.cargo_list = alt_Cygnus Dragon.cargo_list = alt_Dragon Kounotori.cargo_list = alt_Kounotori Progress.cargo_list = alt_Progress # store best result in a textfile if higer than 83 if (total_len > 83) and (cost < total_cost): with open("output.txt", 'w') as output: number_found += 1 json.dump(total_filled, output) total_len_list.append(total_len) # plot and draw graph plt.plot(counter_list, total_len_list) plt.draw() # result to return multiple values result = {"length": total_len, "cost": cost} return result
"======\n") h_C.fill(0.0) # Do the multiplication start_time = time() mmul(queue, (N, N), None, N, d_a, d_b, d_c) queue.finish() run_time = time() - start_time cl.enqueue_copy(queue, h_C, d_c) # show results helper.results(N, h_C, run_time, AVAL, BVAL, TOL) #-------------------------------------------------------------------------------- # STRATEGY 3.b) C row per work item #-------------------------------------------------------------------------------- #initiate the computations kernelsource = open("./CL/C_row.cl").read() program = cl.Program(context, kernelsource).build() mmul = program.mmul mmul.set_scalar_arg_dtypes([numpy.int32, None, None, None]) print("\n===== OpenCL, matrix mult, C row per work item, order", N, "======\n") h_C.fill(0.0)
# boundaries needed for pre loading cargolists boundaries_cargo1 = [100, 15, 19, 250] boundaries_cargo2 = [130, 20, 45, 150] if question == "a" or question == "b": # answer a and b with greedy algorithm if algorithm == "greedy": prepared_list = pre_load_greedy("CargoList1", 83, 0) # define the properties of the spacecraft # the list of spacecrafts is sorted on kg/m3 ratio spacecrafts = hlp.define_spacecrafts() hlp.greedy_filling(prepared_list, spacecrafts) greedy_results = hlp.results(spacecrafts) print("Result Greedy, question A:", greedy_results["length"]) print("Result Greedy, question B:", greedy_results["cost"]) # highest length found with the random greedy was 83 # lowest cost found with 83 with the random greedy was 1985465920 dollar if algorithm == "random greedy": result_rnd_greedy_1 = random_greedy("CargoList1", 83, 0, 1000, boundaries_cargo1) print("Result random greedy question A:", result_rnd_greedy_1["length"]) print("Result random greedy question B:", result_rnd_greedy_1["cost"]) # highest length found with the hill climber was 83 # lowest cost found with 83 with the hill climber was 1980951280 dollar if algorithm == "hill climber": result_hc_1 = hill_climber("CargoList1", 83, boundaries_cargo1, 1000) print("Result hill climber question A:", result_hc_1["length"])
def random_greedy(cargolist, volume_slicer, mass_slicer, number, boundaries): """ Random Greedy used for questions a, b and class Fills the the spacecrafts patially with the best density parcels and after that we randomly put packages into the spacecrafts Give a number to decide how many time the random loop will be executed """ # load the cargo list cargo_list = hlp.load_csv_cargolist(str(cargolist)) # slice and sort it on kg/m3 ratio combined_list = hlp.sort_and_slice(cargo_list, volume_slicer, mass_slicer) cargo_list = combined_list[0][:] remain1 = combined_list[1] # variables cost = 0 counter = 0 max_len = 0 total_len = 0 # repeat 'number' times while counter < number: remaining_list = [] counter += 1 # define properties of the spacecrafts Cygnus = Spacecraft(2000, 18.9, 7400, 390000000, 0.73) Progress = Spacecraft(2400, 7.6, 7020, 175000000, 0.74) Kounotori = Spacecraft(5200, 14, 10500, 420000000, 0.71) Dragon = Spacecraft(6000, 10, 12200, 347000000, 0.72) TianZhou = Spacecraft(6500, 15, 13500, 412000000, 0.75) Verne_ATV = Spacecraft(7500, 48, 20500, 1080000000, 0.72) Cygnus.cargo_list = [] Progress.cargo_list = [] Kounotori.cargo_list = [] Dragon.cargo_list = [] # create a list containing all the spaceships spacecrafts = [Cygnus, Dragon, Kounotori, Progress] # sort list on kg/m3 ratio spacecrafts = sorted(spacecrafts, key=lambda spacecraft: spacecraft.ratio) remaining_list = [] boundaries = boundaries # preload the the spacecrafts hlp.preload_spacecrafts(cargo_list, spacecrafts, remaining_list, boundaries) remaining_lists = remaining_list[:] + remain1 counter2 = 0 # repeat the random choices 300 times while (counter2 < 300): counter2 += 1 total_cost = 0 # choose random parcel from remaining list and spacecrafts parcel = random.choice(remaining_lists) spacecraft = random.choice(spacecrafts) # check if placement is possible if (parcel["mass"] <= spacecraft.remaining_mass) and ( parcel["volume"] <= spacecraft.remaining_volume): spacecraft.add_cargo(parcel["id"], parcel["mass"], parcel["volume"]) remaining_lists.remove(parcel) else: continue # get the results dictionary result = hlp.results(spacecrafts) # unpack dictionary total_len = result["length"] total_cost = result["cost"] cargo_dicts = result["total_filled"] # only write out the best solution if (total_len > 70) and (max_len < total_len or total_cost < cost): with open( "outputfile(" + str(total_len) + ') (' + str(total_cost) + ").txt", 'w') as output: json.dump(cargo_dicts, output) cost = total_cost if total_len > max_len: max_len = total_len # put cost and length in dictionary result = {"length": max_len, "cost": cost} return result