def simulation(self, p, i, p_puzzle): layers = self.layers flag = self.flag start_time = self.start_time r_corner, r_inner, r_sides = self.back_prop(i, p, math.sqrt(i)) puzzle = [] puzzle = sim.puzzle_generate(r_corner[:], r_sides[:], r_inner[:], layers, p_puzzle[:]) fitness = fit_eval.fit_evaluate(puzzle[:], layers) #print i,fitness if fitness >= -1 or i == (layers * layers) - 1: print("--- %s seconds ---" % (time.time() - start_time)) flag == False print puzzle pd.draw_puzzle(puzzle, layers) exit() else: flag == True return fitness, flag
best_gen_fitness=[] best_gen_count=[] for i in population: # print (i.fitness) best_gen_count.append(0) best_gen_fitness.append(i.fitness) gen = 0 #for gen in range(0,generation): while(True): gen+=1 fit = [individual.fitness for individual in population] select = s.selection(fit) parents = [population[i] for i in select] offsprings = variation(parents) offsprings = calculate_fitness_score(offsprings) offsprings = sorted(offsprings, key=lambda individual: individual.fitness, reverse=False) population=survivor_selection(population,offsprings) if population[0].fitness <= 0: print ("Generation ",gen) for j in population: print (j.fitness) print "Threshold readched" break print("--- %s seconds ---" % (time.time() - start_time)) p=population[0] puzzle = puzzle_generate.puzzle_gen(p.corners,p.sides,p.inner,layers) print puzzle pd.draw_puzzle(puzzle,layers)
def populate_children(self, parent, i, root, count): ## Each node is compared with the respective postion in the layer layers = self.layers current_layer = int(math.sqrt(i)) r_corner, r_inner, r_sides = self.back_prop(i, parent, current_layer) ## For the first corner if i == 0: while (len(r_corner) > 2): j = r_corner[0] child = Node() child.parent = parent if j[0] != '0': child.value = t_rot.rotate(j[:], 2) elif j[1] != '0': child.value = t_rot.rotate(j[:], 1) else: child.value = j[:] r_corner.remove(j) parent.children += (child, ) #For the leftmost corner elif i == (layers - 1) * (layers - 1): while (len(r_corner) > 0): j = r_corner[0] pos_m = 0 found = 0 #Ensuring that there is atleast one inner pieces that can be placed after this corner for m in r_inner: if m[1] == j[0] or \ m[1] == j[1] or \ m[1] == j[2] : #found = 1 for n in r_sides: if m[0] == n [0] or \ m[0] == n [1] or \ m[0] == n [2] : found = 1 break if found == 1: break if found != 1: r_corner.remove(j) continue child = Node() child.parent = parent if j[0] != '0': child.value = t_rot.rotate(j[:], 1) elif j[2] != '0': child.value = t_rot.rotate(j[:], 2) else: child.value = j[:] r_corner.remove(j) parent.children += (child, ) ## The final piece elif i == (layers) * (layers) - 1: #laste piece while (len(r_corner) > 0): j = r_corner[0] child = Node() child.parent = parent if j[2] != '0': child.value = t_rot.rotate(j[:], 1) elif j[0] != '0': child.value = t_rot.rotate(j[:], 2) else: child.value = j[:] r_corner.remove(j) parent.children += (child, ) ## left side pieces elif i == current_layer * current_layer: while (len(r_sides) > 0): j = r_sides[0] child = Node() child.parent = parent if j[2] == '0': child.value = t_rot.rotate(j[:], 1) elif j[1] == '0': child.value = t_rot.rotate(j[:], 2) else: child.value = j[:] r_sides.remove(j) parent.children += (child, ) ## The final layer elif current_layer == layers - 1: ## if the finat layer is evem the bottom pieces are at the even posistions and ## vice versa #if i == 43: # print r_sides # print r_inner # print r_corner # print parent.value,parent.parent.value if (current_layer%2==0 and i%2==0) or \ (current_layer%2!=0 and i%2!=0): while (len(r_sides)) > 0: j = r_sides[0] if j[1] == '0': value = t_rot.rotate(j[:], 1) elif j[0] == '0': value = t_rot.rotate(j[:], 2) else: value = j[:] if value[0] != parent.value[0]: r_sides.remove(j) continue child = Node() child.parent = parent child.value = value r_sides.remove(j) parent.children += (child, ) else: # for inner in the final layer t_puzzle = [] p_puzzle = [] temp_parent = parent for _ in range(0, i): t_puzzle.append(temp_parent.value) temp_parent = temp_parent.parent p_puzzle = [ t_puzzle[j] for j in range(len(t_puzzle) - 1, -1, -1) ] while (len(r_inner)) > 0: j = r_inner[0] if j[1] != parent.value[1]: r_inner.remove(j) continue elif (j[2] != p_puzzle[len(p_puzzle) - current_layer * 2][2]): r_inner.remove(j) continue child = Node() child.value = j[:] child.parent = parent r_inner.remove(j) parent.children += (child, ) ## For right edges of the puzzle elif i == current_layer * current_layer + 2 * current_layer: while (len(r_sides)) > 0: j = r_sides[0] value = j if j[0] == '0': value = t_rot.rotate(j[:], 1) elif j[2] == '0': value = t_rot.rotate(j[:], 2) else: value = j[:] if value[0] != parent.value[0]: r_sides.remove(j) continue child = Node() child.parent = parent child.value = value r_sides.remove(j) parent.children += (child, ) ## For other inner pieces else: t_puzzle = [] p_puzzle = [] temp_parent = parent for _ in range(0, i): t_puzzle.append(temp_parent.value) temp_parent = temp_parent.parent p_puzzle = [t_puzzle[j] for j in range(len(t_puzzle) - 1, -1, -1)] pos_j = 0 ## Here the each selected piece are compared with the remaining pieces to ensure that there is ## atleast one piece to place after the current piece while len(r_inner) > 0: j = r_inner[0] if (current_layer % 2 == 0): if i % 2 != 0: ### inverted layers at even postion when the current layer is odd and vice versa if ( j[1] != parent.value[1] ): ## comapring the first edge witht the piece on the left r_inner.remove(j) continue elif ( j[2] != p_puzzle[len(p_puzzle) - current_layer * 2][2] ): ## comparing the bottom edge with the piece on the top r_inner.remove(j) continue pos_n = 0 found = 0 for n in r_inner: pos_n += 1 if pos_n == pos_j: continue else: if n[0] == j[0]: found = 1 break if found != 1: r_inner.remove(j) continue temp = [] if i == current_layer * current_layer + 2 * current_layer - 1: found = 0 for s in r_sides: if s[0] == '0': temp = t_rot.rotate(s[:], 1) elif s[2] == '0': temp = t_rot.rotate(s[:], 2) else: temp = s[:] if j[0] == temp[0]: found = 1 break if found != 1: r_inner.remove(j) continue else: ### other inner pieces if (j[0] != parent.value[0]): r_inner.remove(j) continue found = 0 pos_m = 0 for m in r_inner: pos_m += 1 if pos_m == pos_j: continue else: if (m[1] == j[1]): pos_n = 0 for n in r_inner: pos_n += 1 if pos_n == pos_m or pos_n == pos_j: continue else: if n[2] == j[2]: found = 1 break if found == 1: break if found != 1: r_inner.remove(j) continue elif (current_layer % 2 != 0): if i % 2 == 0: if (j[1] != parent.value[1]): r_inner.remove(j) continue elif (j[2] != p_puzzle[len(p_puzzle) - current_layer * 2][2]): r_inner.remove(j) continue pos_n = 0 found = 0 for n in r_inner: pos_n += 1 if pos_n == pos_j: continue else: if n[0] == j[0]: found = 1 break if found != 1: r_inner.remove(j) continue temp = [] if i == current_layer * current_layer + 2 * current_layer - 1: found = 0 for s in r_sides: if s[0] == '0': temp = t_rot.rotate(s[:], 1) elif s[2] == '0': temp = t_rot.rotate(s[:], 2) else: temp = s[:] if j[0] == temp[0]: found = 1 break if found != 1: r_inner.remove(j) continue else: if (j[0] != parent.value[0]): r_inner.remove(j) continue found = 0 pos_m = 0 for m in r_inner: pos_m += 1 if pos_m == pos_j: continue else: if (m[1] == j[1]): pos_n = 0 for n in r_inner: pos_n += 1 if pos_n == pos_m or pos_n == pos_j: continue else: if n[2] == j[2]: found = 1 break if found == 1: break if found != 1: r_inner.remove(j) continue child = Node() child.value = j[:] child.parent = parent r_inner.remove(j) parent.children += (child, ) ## Only doing the simulation when the parent has children #if i == 43: # print "numbe of new nodes",len(parent.children),parent.value,len(parent.parent.children),parent.parent.parent.value,parent.parent.parent.parent.value # for ch in parent.parent.children: # print "kutikal",ch.value if len(parent.children) == 0: parent.score = -99999 count += 1 if i == layers * layers: #o = random.randint(0,len(parent.children)-1) t_puzzle = [] p_puzzle = [] temp_parent = parent for _ in range(0, i): t_puzzle.append(temp_parent.value) temp_parent = temp_parent.parent ## Generating the puzzle at current postion p_puzzle = [t_puzzle[j] for j in range(len(t_puzzle) - 1, -1, -1)] print p_puzzle print count count = 0 if p_puzzle[i - 1][1] != '0': p_puzzle[i - 1] = t_rot.rotate(p_puzzle[i - 1][:], 2) elif p_puzzle[i - 1][2] != '0': p_puzzle[i - 1] = t_rot.rotate(p_puzzle[i - 1][:], 1) print p_puzzle pd.draw_puzzle(p_puzzle, layers) exit() ## calculating the scores ## when there is no children return with high penalty and delete the parent return parent, count