Commit c35afdb5 authored by Justin Borromeo's avatar Justin Borromeo Committed by Harry Chan Chan
Browse files

Linear packing penalty and screencaps of results

parent e5bba5ce
...@@ -4,6 +4,7 @@ from os import cpu_count ...@@ -4,6 +4,7 @@ from os import cpu_count
import time import time
import path_constant as pc import path_constant as pc
import packing_penalty as pp import packing_penalty as pp
from os import makedirs
class cma_approach(object): class cma_approach(object):
def __init__(self, def __init__(self,
...@@ -21,7 +22,7 @@ class cma_approach(object): ...@@ -21,7 +22,7 @@ class cma_approach(object):
res_step = 1, res_step = 1,
penalty_offest = 10000000000, penalty_offest = 10000000000,
seeding_type="optimised", seeding_type="optimised",
hybird = True, hybrid = True,
print_to_csv = True, print_to_csv = True,
max_pack_size = 129 max_pack_size = 129
): ):
...@@ -35,7 +36,7 @@ class cma_approach(object): ...@@ -35,7 +36,7 @@ class cma_approach(object):
self.population_size = population_size self.population_size = population_size
self.penalty_offest = penalty_offest self.penalty_offest = penalty_offest
self.ending_iter = 0 self.ending_iter = 0
self.is_hybird = hybird self.is_hybrid = hybrid
self.data_src = {} self.data_src = {}
self.topology_file = path_to_topology self.topology_file = path_to_topology
...@@ -159,7 +160,8 @@ class cma_approach(object): ...@@ -159,7 +160,8 @@ class cma_approach(object):
return latencies, max_latency_idx return latencies, max_latency_idx
def eva_hybird_sq(self, layer):
def eva_hybrid_sq(self, layer):
res = [self.res_step] * self.k res = [self.res_step] * self.k
latencies = [] latencies = []
...@@ -170,11 +172,12 @@ class cma_approach(object): ...@@ -170,11 +172,12 @@ class cma_approach(object):
# for i in range(0, int(self.max_res_unit/self.res_step - self.k*self.res_step)): # for i in range(0, int(self.max_res_unit/self.res_step - self.k*self.res_step)):
# latencies, max_idx = self.find_max_latency(layer, res) # latencies, max_idx = self.find_max_latency(layer, res)
# res[max_idx] += self.res_step # res[max_idx] += self.res_step
if pp.packingPenalty(res, self.max_pack_size): # If all layers couldn't be packed, packingPenalty returns 0.
packing_penalty = pp.packingPenalty(res, self.max_pack_size)
if packing_penalty == 0:
return latencies[max_idx], latencies, res, layer return latencies[max_idx], latencies, res, layer
return latencies[max_idx] + packing_penalty, latencies, res, layer
return self.penalty_offest, latencies, res, layer
def evaluation_top_level(self, in_val): def evaluation_top_level(self, in_val):
pid, sampling = in_val pid, sampling = in_val
...@@ -184,19 +187,19 @@ class cma_approach(object): ...@@ -184,19 +187,19 @@ class cma_approach(object):
if not self.filter_layer(layer): if not self.filter_layer(layer):
penalty = self.penalty_layer(layer) penalty = self.penalty_layer(layer)
if self.is_hybird: if self.is_hybrid:
return pid, penalty return pid, penalty
else: else:
return pid, penalty*4 return pid, penalty*4
layer = self.regroup_layers(layer) layer = self.regroup_layers(layer)
# if self.is_hybird: # if self.is_hybrid:
# return pid, self.eva_hybird_sq(layer)[0] # return pid, self.eva_hybrid_sq(layer)[0]
# else: # else:
# score, _, res, _ = self.evaluate_full_relaxed(layer) # score, _, res, _ = self.evaluate_full_relaxed(layer)
# return pid, score, res # return pid, score, res
return pid, self.eva_hybird_sq(layer)[0] return pid, self.eva_hybrid_sq(layer)[0]
def run(self): def run(self):
self.trial += self.trial self.trial += self.trial
...@@ -233,7 +236,7 @@ class cma_approach(object): ...@@ -233,7 +236,7 @@ class cma_approach(object):
# if result[1] >= self.penalty_offest: # if result[1] >= self.penalty_offest:
# invalid_sampling += 1 # invalid_sampling += 1
# else: # else:
# if not self.is_hybird: # if not self.is_hybrid:
# res_combintaions[result[0]] = result[2] # res_combintaions[result[0]] = result[2]
# pool.close() # pool.close()
# pool.join() # pool.join()
...@@ -243,7 +246,7 @@ class cma_approach(object): ...@@ -243,7 +246,7 @@ class cma_approach(object):
if scores[tup[0]] >= self.penalty_offest: if scores[tup[0]] >= self.penalty_offest:
invalid_sampling += 1 invalid_sampling += 1
if not self.is_hybird: if not self.is_hybrid:
best_in_iteration = min(scores) best_in_iteration = min(scores)
if best_in_iteration < best_overall and best_in_iteration < self.penalty_offest: if best_in_iteration < best_overall and best_in_iteration < self.penalty_offest:
...@@ -264,20 +267,20 @@ class cma_approach(object): ...@@ -264,20 +267,20 @@ class cma_approach(object):
self.end = time.time() self.end = time.time()
self.best_layer = [self.decode(val, len(self.layers)) for val in self.es.result[0]] self.best_layer = [self.decode(val, len(self.layers)) for val in self.es.result[0]]
self.best_layer.append(len(self.layers) - sum(self.best_layer)) self.best_layer.append(len(self.layers) - sum(self.best_layer))
temp_out.append(self.report()[1]) temp_out.append(self.report(False)[1])
self.i += 1 self.i += 1
self.ending_iter = self.i self.ending_iter = self.i
return temp_out return temp_out
def report(self): def report(self, output_png):
##print(self.i, self.es.sigma) ##print(self.i, self.es.sigma)
max_latency = 0 max_latency = 0
layer = [] layer = []
res = [] res = []
latencies = [] latencies = []
if self.is_hybird: if self.is_hybrid:
if not self.filter_layer(self.best_layer): if not self.filter_layer(self.best_layer):
##print("RESULT NOT VALID") ##print("RESULT NOT VALID")
##print("Layer:", self.best_layer, "sum: ", sum(self.best_layer)) ##print("Layer:", self.best_layer, "sum: ", sum(self.best_layer))
...@@ -293,7 +296,7 @@ class cma_approach(object): ...@@ -293,7 +296,7 @@ class cma_approach(object):
return False, result return False, result
layer = self.regroup_layers(self.best_layer) layer = self.regroup_layers(self.best_layer)
max_latency, latencies, res, layers = self.eva_hybird_sq(layer) max_latency, latencies, res, layers = self.eva_hybrid_sq(layer)
else: else:
if not self.filter_res(self.best_res) and not self.filter_layer(self.best_layer): if not self.filter_res(self.best_res) and not self.filter_layer(self.best_layer):
#print("RESULT NOT VALID") #print("RESULT NOT VALID")
...@@ -332,8 +335,23 @@ class cma_approach(object): ...@@ -332,8 +335,23 @@ class cma_approach(object):
csvFile.close csvFile.close
if self.valid_sampling_percentage > 0: if self.valid_sampling_percentage > 0:
pngFileName = str(self.topology_file.replace(".csv", "")) + "_k=" + str(self.k) + "_max=" + str(self.max_res_unit) + "_packSize=" + str(self.max_pack_size) + ".png" directory_path = pc.RESULT_SCREENSHOT_PATH + \
pp.printPNG(res, self.max_pack_size, pngFileName) str(self.topology_file.replace(".csv", "")) + "/" + \
"pack_size_" + str(self.max_pack_size) + "/" + \
"linear_penalty_constant_" + str(pp.PENALTY_CONSTANT) + "/"
makedirs(directory_path, exist_ok = True)
pngFileName = "k=" + str(self.k) + "_max=" + str(self.max_res_unit) \
+ ".png"
packing_penalty = pp.packingPenalty(res, self.max_pack_size)
if packing_penalty == 0 and output_png:
bin_area = self.max_pack_size ** 2
packed_area = 0
for rect in res:
square_area = rect ** 2
packed_area += square_area
percentage_wasted = 100 * (bin_area - packed_area) / bin_area
consumed_area = 0
pp.printPNG(res, self.max_pack_size, directory_path + pngFileName)
result = [self.target_col,self.i,self.k, self.topology_file, 1,(1/max_latency), max_latency*self.k, 1/full_latency[full_max_idx], full_latency[full_max_idx], (1/max_latency)/(1/full_latency[full_max_idx]), (max_latency*self.k)/full_latency[full_max_idx], layer, res, self.end-self.start, self.es.sigma, self.seed_od,self.valid_sampling_percentage, self.trial, self.population_size, self.max_res_unit, self.seeding_type] result = [self.target_col,self.i,self.k, self.topology_file, 1,(1/max_latency), max_latency*self.k, 1/full_latency[full_max_idx], full_latency[full_max_idx], (1/max_latency)/(1/full_latency[full_max_idx]), (max_latency*self.k)/full_latency[full_max_idx], layer, res, self.end-self.start, self.es.sigma, self.seed_od,self.valid_sampling_percentage, self.trial, self.population_size, self.max_res_unit, self.seeding_type]
return True, result return True, result
...@@ -349,7 +367,7 @@ if __name__ == "__main__": ...@@ -349,7 +367,7 @@ if __name__ == "__main__":
seeding_type = sys.argv[5] seeding_type = sys.argv[5]
target_col = sys.argv[6] target_col = sys.argv[6]
es_hybird = cma_approach( es_hybrid = cma_approach(
path_to_datasrc = str(topology)+"_square_mem_bound.csv", path_to_datasrc = str(topology)+"_square_mem_bound.csv",
path_to_topology = str(topology)+".csv", path_to_topology = str(topology)+".csv",
target_col = str(target_col), target_col = str(target_col),
...@@ -361,14 +379,14 @@ if __name__ == "__main__": ...@@ -361,14 +379,14 @@ if __name__ == "__main__":
res_step = 3, res_step = 3,
penalty_offest = 100000000000, penalty_offest = 100000000000,
seeding_type = seeding_type, seeding_type = seeding_type,
hybird = True, hybrid = True,
print_to_csv = True print_to_csv = True
) )
trials = 1 trials = 1
es_hybird.run() es_hybrid.run()
while not es_hybird.report() and trials < 20: while not es_hybrid.report(True) and trials < 20:
es_hybird.run() es_hybrid.run()
trials += 1 trials += 1
# k += 1 # k += 1
......
from rectpack import newPacker from rectpack import newPacker
from rectpack import MaxRectsBssf from rectpack import MaxRectsBssf, MaxRectsBl, MaxRectsBaf, MaxRectsBlsf
from rectpack import GuillotineBssfSas
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import matplotlib.patches as patches import matplotlib.patches as patches
def packingPenalty(partitions, full_array): def packingPenalty(partitions, full_array):
# print(partitions, full_array)
packer = newPacker(pack_algo = MaxRectsBssf) packer = newPacker(pack_algo = MaxRectsBssf)
for p in partitions: for p in partitions:
packer.add_rect(p,p) packer.add_rect(p,p)
...@@ -13,7 +12,27 @@ def packingPenalty(partitions, full_array): ...@@ -13,7 +12,27 @@ def packingPenalty(partitions, full_array):
packer.add_bin(full_array, full_array) packer.add_bin(full_array, full_array)
packer.pack() packer.pack()
return len(packer[0]) == len(partitions) # First and only bin
bin_area = packer[0].width * packer[0].height
packed_area = 0
for rect in packer[0]:
side_length = rect.width
square_area = side_length ** 2
packed_area += square_area
percentage_wasted = 100 * (bin_area - packed_area) / bin_area
if len(packer[0]) == len(partitions):
return 0 # Fully packed, no penalty
# Small 0.0001 offset so a fully-packed bin with non-fully packed layers
# isn't treated as 0-penalty.
return penaltyFunction(percentage_wasted) + 0.001
PENALTY_CONSTANT = 40000
def penaltyFunction(percentage_wasted):
# 0 percent wasted -> 0 penalty
# 5 percent wasted -> 200000
return PENALTY_CONSTANT * percentage_wasted
def printPNG(partitions, full_array, filename = "sth.png"): def printPNG(partitions, full_array, filename = "sth.png"):
# print(partitions, full_array) # print(partitions, full_array)
...@@ -56,7 +75,8 @@ def printPNG(partitions, full_array, filename = "sth.png"): ...@@ -56,7 +75,8 @@ def printPNG(partitions, full_array, filename = "sth.png"):
papertype='letter', papertype='letter',
bbox_inches='tight') bbox_inches='tight')
fig.clear() fig.clear()
plt.clf()
if __name__ == "__main__": if __name__ == "__main__":
assert(packingPenalty([2,2],5) == True) assert(packingPenalty([2,2],5) == 0)
assert(packingPenalty([2,4],5) == False) assert(packingPenalty([2,4],5) == PENALTY_CONSTANT * 100)
\ No newline at end of file
DATA_SOURCE_PATH = "../data_source/" DATA_SOURCE_PATH = "../data_source/"
TOPOLOGIES_PATH = "../topologies/" TOPOLOGIES_PATH = "../topologies/"
RESULT_CSV_PATH = "../resulting_csv/" RESULT_CSV_PATH = "../resulting_csv/"
\ No newline at end of file RESULT_SCREENSHOT_PATH = "../packing_screenshots/"
\ No newline at end of file
...@@ -52,16 +52,16 @@ def trial(prop): ...@@ -52,16 +52,16 @@ def trial(prop):
res_step = 3, res_step = 3,
penalty_offest = 100000000000, penalty_offest = 100000000000,
seeding_type = seed, seeding_type = seed,
hybird = True, hybrid = True,
print_to_csv = False, print_to_csv = False,
max_pack_size = MAX_PACK_SIZE max_pack_size = MAX_PACK_SIZE
) )
process_out = instance.run() process_out = instance.run()
success, out = instance.report() success, out = instance.report(True)
while not success and trials < MAX_TRIAL: while not success and trials < MAX_TRIAL:
process_out = instance.run() process_out = instance.run()
success, out = instance.report() success, out = instance.report(True)
trials += 1 trials += 1
process_out.append(out) process_out.append(out)
return process_out return process_out
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment