From e7b0ad345bc41f0469c112bc8a766b412387f34a Mon Sep 17 00:00:00 2001 From: omega-coder Date: Sat, 13 Jul 2019 15:36:04 +0200 Subject: [PATCH 1/5] Adds parameters for genetic algorithm from Client request --- app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index ef2012f..ef61e15 100644 --- a/app.py +++ b/app.py @@ -168,7 +168,11 @@ def solve(): return response elif pfsp_algorithm == "genetic-algorithm": - seq, jobs, opt_makespan, t_t = problem_inst.genetic_algorithm() + population_number = int(prob["population_number"]) + it_number = int(prob["it_number"]) + p_crossover = float(prob["p_crossover"]) + p_mutation = float(prob["p_mutation"]) + seq, jobs, opt_makespan, t_t = problem_inst.genetic_algorithm(population_number, it_number, p_crossover, p_mutation) fig = jobs_to_gantt_fig(jobs, num_machines, num_jobs) graph_json = ganttfig_to_json(fig) if float(t_t) * 1000 > 1000.0: From d06735e0ec3625455bc57f308a10398e0889ca2d Mon Sep 17 00:00:00 2001 From: omega-coder Date: Sat, 13 Jul 2019 15:36:37 +0200 Subject: [PATCH 2/5] Make genetic algorithm parameterized --- flowshop.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/flowshop.py b/flowshop.py index 2219ac9..2a18ac6 100644 --- a/flowshop.py +++ b/flowshop.py @@ -350,7 +350,10 @@ def brute_force_exact(self): t_t = e -s return seq, schedules, makespan, t_t - def genetic_algorithm(self): + def genetic_algorithm(self, population_number, it_number=5000, p_crossover=1.0, p_mutation=1.0): + if population_number is None: + population_number = self.nb_jobs**2 + default_timer = None if sys.platform == "win32": default_timer = time.clock @@ -368,10 +371,10 @@ def genetic_algorithm(self): temp.append(self.data[j][i]) processing_time.append(temp) # generate an initial population proportional to no_of_jobs - number_of_population = no_of_jobs**2 - no_of_iterations = 5000 - p_crossover = 1.0 - p_mutation = 1.0 + number_of_population = population_number + no_of_iterations = it_number + p_crossover = p_crossover + p_mutation = p_mutation # Initialize population population = initialize_population( From 2e6be97e4cac5fadcc058d7b8deca8d6196c09f3 Mon Sep 17 00:00:00 2001 From: omega-coder Date: Wed, 17 Jul 2019 09:32:33 +0200 Subject: [PATCH 3/5] Handles nograph parameter --- app.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index ef61e15..b8fd961 100644 --- a/app.py +++ b/app.py @@ -172,17 +172,28 @@ def solve(): it_number = int(prob["it_number"]) p_crossover = float(prob["p_crossover"]) p_mutation = float(prob["p_mutation"]) - seq, jobs, opt_makespan, t_t = problem_inst.genetic_algorithm(population_number, it_number, p_crossover, p_mutation) - fig = jobs_to_gantt_fig(jobs, num_machines, num_jobs) - graph_json = ganttfig_to_json(fig) + nograph = prob["nograph"] + if nograph: + seq, jobs, opt_makespan, t_t = problem_inst.genetic_algorithm(population_number, it_number, p_crossover, p_mutation, nograph=True) + print("No Graph genetic done") + else: + seq, jobs, opt_makespan, t_t = problem_inst.genetic_algorithm(population_number, it_number, p_crossover, p_mutation) + fig = jobs_to_gantt_fig(jobs, num_machines, num_jobs) + graph_json = ganttfig_to_json(fig) + if float(t_t) * 1000 > 1000.0: time_ts = t_t time_typ = "seconds" else: time_ts = float(t_t * 1000) time_typ = "msecs" - resp = json.dumps( - {"graph": graph_json, "optim_makespan": opt_makespan, "opt_seq": seq, "t_time": time_ts, "tt": time_typ}) + if nograph: + resp = json.dumps( + {"graph": None, "optim_makespan": opt_makespan, "opt_seq": seq, "t_time": time_ts, "tt": time_typ}) + else: + resp = json.dumps( + {"graph": graph_json, "optim_makespan": opt_makespan, "opt_seq": seq, "t_time": time_ts, "tt": time_typ}) + response = app.response_class( response=resp, status=200, From e25dbbd1bd74844f31031cb58ee4028779bac2cf Mon Sep 17 00:00:00 2001 From: omega-coder Date: Wed, 17 Jul 2019 09:33:10 +0200 Subject: [PATCH 4/5] Adds nograph param for GA --- flowshop.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/flowshop.py b/flowshop.py index 2a18ac6..cf8fa81 100644 --- a/flowshop.py +++ b/flowshop.py @@ -350,7 +350,7 @@ def brute_force_exact(self): t_t = e -s return seq, schedules, makespan, t_t - def genetic_algorithm(self, population_number, it_number=5000, p_crossover=1.0, p_mutation=1.0): + def genetic_algorithm(self, population_number, it_number=5000, p_crossover=1.0, p_mutation=1.0, nograph=False): if population_number is None: population_number = self.nb_jobs**2 @@ -420,6 +420,9 @@ def genetic_algorithm(self, population_number, it_number=5000, p_crossover=1.0, seq = list(map(int, costed_population[0][1])) makespan = self._get_makespan(seq, self.data) e = default_timer.__call__() + if nograph: + t_t = e - s + return seq, None, makespan, t_t schedules = np.zeros((self.nb_machines, self.nb_jobs), dtype=dict) # schedule first job alone first @@ -593,9 +596,10 @@ def get_problem_instance(self): if __name__ == "__main__": - random_problem = RandomFlowshop(3, 8) + random_problem = RandomFlowshop(20, 100) random_problem_instance = random_problem.get_problem_instance() - seq = random_problem_instance.cds() - b_seq, b_scheds, b_opt_makespan = random_problem_instance.brute_force_exact() - cds_seq, cds_scheds, cds_makespan = random_problem_instance.cds() - print("[Bruteforce] makespan: {} \n [CDS] makespan: {}".format(b_opt_makespan, cds_makespan)) + #seq = random_problem_instance.cds() + #b_seq, b_scheds, b_opt_makespan = random_problem_instance.brute_force_exact() + ga_seq, ga_scheds, ga_makespan, t_t = random_problem_instance.genetic_algorithm(population_number=None, it_number=500, p_crossover=1.0, p_mutation=1.0) + print("ga_mkspan: {}, t_t: {}".format(ga_makespan, t_t)) + From cde64a2568c7d920ed8fe6a4d6579fd7dcc1abcb Mon Sep 17 00:00:00 2001 From: omega-coder Date: Wed, 17 Jul 2019 09:33:49 +0200 Subject: [PATCH 5/5] Handles when nograph is specified --- static/js/main.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/static/js/main.js b/static/js/main.js index d7f51a9..3380791 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -46,6 +46,7 @@ $(document).ready(function() { }); } else if ($("#algorithm").val() === "genetic-algorithm") { var data_tbs = JSON.stringify({ + nograph: true, algorithm: $("#algorithm").val(), data: $("#data").val(), nb_machines: $("#nb_machines").val(), @@ -72,11 +73,19 @@ $(document).ready(function() { data: data_tbs, processData: false, success: function(data, textStatus, jQxhr) { - Plotly.newPlot("gantt", JSON.parse(data["graph"]), {}); - $("#sequence").text(data["opt_seq"]); - $("#opt_makespan").text(data["optim_makespan"]); - var time_str = data["t_time"].toString() + " " + data["tt"].toString(); - $("#time").text(time_str); + if (data["graph"] === null) { + $("#sequence").text(data["opt_seq"]); + $("#opt_makespan").text(data["optim_makespan"]); + var time_str = data["t_time"].toString() + " " + data["tt"].toString(); + $("#time").text(time_str); + } else { + Plotly.newPlot("gantt", JSON.parse(data["graph"]), {}); + $("#sequence").text(data["opt_seq"]); + $("#opt_makespan").text(data["optim_makespan"]); + var time_str = data["t_time"].toString() + " " + data["tt"].toString(); + $("#time").text(time_str); + } + }, error: function(jQxhr, textStatus, errorThrow) { console.log(textStatus);