def Mutation(ind) : #making list of mutated breed mutationList = [] #print('mutating') for item in ind : #Create copy of breed to edit indTemp = copy.deepcopy(item) #Iterating over each piece position for i in range(len(item['Positions'])): mutate = numpy.random.choice([True,False],p = [0.05,0.95]) #Randomizer to decide to mutate or not if mutate or indTemp['Positions'].count(indTemp['Positions'][i]) > 1: #Mutate the piece position according to randomizer above OR if it has duplicate on the list #Creating mutated position tupExist = True while tupExist : t1 = random.SystemRandom().randint(0,7) t2 = random.SystemRandom().randint(0,7) tupExist = (t1,t2) in item['Positions'] or (t1,t2) in indTemp['Positions'] #Assigning the mutated positon indTemp['Positions'][i] = (t1,t2) if (i < len(item['White'])): indTemp['White'][i].position = (t1,t2) else: indTemp['Black'][i-len(item['White'])].position= (t1,t2) #Re-calculate cost indTemp['Cost'] = countThreat1(indTemp['White'],indTemp['Positions']) + countThreat1(indTemp['Black'],indTemp['Positions']) - countThreat2(indTemp['White'], indTemp['Black'],indTemp['Positions']) mutationList.append(indTemp.copy()) return mutationList
def simulatedAnnealing(takenPos, whiteList, blackList): #Initialize minState minState = {} minState['Positions'] = copy.deepcopy(takenPos) minState['White'] = copy.deepcopy(whiteList) minState['Black'] = copy.deepcopy(blackList) minState['Cost'] = countThreat1(whiteList, takenPos) + countThreat1( blackList, takenPos) - countThreat2(whiteList, blackList, takenPos) #Initialize temperature temperature = 100 while (temperature > 0): #Make a successor state tempState = findBetterState(minState['Positions'], minState['White'], minState['Black']) #Compare with current state if (tempState['Cost'] < minState['Cost']): minState = copy.deepcopy(tempState) else: probFactor = numpy.exp( (minState['Cost'] - tempState['Cost']) / temperature) change = numpy.random.choice([True, False], p=[probFactor, 1 - probFactor]) if (change): minState = copy.deepcopy(tempState) #decrease temperature temperature = temperature - 1 #temperature <= 0 return minState
def hillClimbing(takenPos, whiteList, blackList): # Initialize minState minState = {} minState['Positions'] = copy.deepcopy(takenPos) minState['White'] = copy.deepcopy(whiteList) minState['Black'] = copy.deepcopy(blackList) minState['Cost'] = countThreat1(whiteList, takenPos) + countThreat1( blackList, takenPos) - countThreat2(whiteList, blackList, takenPos) # If there is better state, algorithm keep finding a better state betterExist = True while betterExist: tempState = findBetterState(minState['Positions'], minState['White'], minState['Black']) if (tempState['Cost'] < minState['Cost']): minState = tempState else: betterExist = False return minState
def findBetterState(takenPos, whiteList, blackList): # Initialize minState from minState = {} minState['Cost'] = countThreat1(whiteList, takenPos) + countThreat1( blackList, takenPos) - countThreat2(whiteList, blackList, takenPos) minState['White'] = whiteList minState['Black'] = blackList minState['Positions'] = takenPos # Iteration in white pion list to find a better assignment for x in range(0, len(whiteList)): # Iterate a and b to find new position for a in range(0, 8): for b in range(0, 8): if ((a, b) not in takenPos): # Make new temporary variable of position and white pion list, with position that haven't been taken tempPos = copy.deepcopy(takenPos) tempWhite = copy.deepcopy(whiteList) tempWhite[x].position = (a, b) tempPos[x] = (a, b) # Count threats from the new position tempV = countThreat1(tempWhite, takenPos) + countThreat1( blackList, takenPos) - countThreat2( tempWhite, blackList, takenPos) # If the threat cost is less than the current minState, assign new minState with the temporary variables if (tempV < minState['Cost']): minState['Cost'] = tempV minState['Positions'] = tempPos.copy() minState['White'] = tempWhite.copy() takenPos = tempPos.copy() whiteList = tempWhite.copy() # Iteration in black pion list to find a better assignment for x in range(0, len(blackList)): # Iterate a and b to find new position for a in range(0, 8): for b in range(0, 8): if ((a, b) not in takenPos): # Make new temporary variable of position and black pion list, with position that haven't been taken tempPos = copy.deepcopy(takenPos) tempBlack = copy.deepcopy(blackList) tempBlack[x].position = (a, b) tempPos[x + len(whiteList)] = (a, b) # Count threats from the new position tempV = countThreat1(tempWhite, takenPos) + countThreat1( tempBlack, takenPos) - countThreat2( whiteList, tempBlack, takenPos) # If the threat cost is less than the current minState, assign new minState with the temporary variables if (tempV < minState['Cost']): minState['Cost'] = tempV minState['Positions'] = tempPos.copy() minState['Black'] = tempBlack.copy() return (minState)
def generatePopulation(takenPos,whiteList,blackList): #Making Population Object obj = {} obj['Positions'] = takenPos obj['White'] = whiteList obj['Black'] = blackList obj['Cost'] = countThreat1(whiteList,takenPos) + countThreat1(blackList,takenPos) - countThreat2(whiteList,blackList,takenPos) population = [obj] #Making new state for i in range(31): tempWhite = copy.deepcopy(whiteList) tempBlack = copy.deepcopy(blackList) tempPos = copy.deepcopy(takenPos) #Making new White List for j in range(0,len(whiteList)): loc = (random.SystemRandom().randint(0,7),random.SystemRandom().randint(0,7)) while loc in tempPos: loc = (random.SystemRandom().randint(0,7),random.SystemRandom().randint(0,7)) tempWhite[j].position = loc tempPos[j] = loc #Making new Black List for j in range(0,len(blackList)): loc = (random.SystemRandom().randint(0,7),random.SystemRandom().randint(0,7)) while loc in tempPos: loc = (random.SystemRandom().randint(0,7),random.SystemRandom().randint(0,7)) tempBlack[j].position = loc tempPos[j+len(whiteList)] = loc #Completing new population and adding it to population list obj['Positions'] = tempPos obj['White'] = tempWhite obj['Black'] = tempBlack obj['Cost'] = countThreat1(tempWhite,takenPos) + countThreat1(tempBlack,takenPos) + countThreat2(tempWhite,tempBlack,takenPos) population.append(obj.copy()) return population
def Crossover(ind) : #Make list of new breed newInd = [] for tup in ind: #Making intialization of new breed newInd1 = {} newInd1["White"] = [] newInd1["Black"] = [] newInd1["Positions"] = [] newInd2 = {} newInd2["White"] = [] newInd2["Black"] = [] newInd2["Positions"] = [] #Crossovering White Piece Postition for i in range(0,len(tup[0]['White'])): rn = random.SystemRandom().randint(0,1) #Randomizing which one of the parent to copy the position if(rn == 0): if (tup[0]['White'][i] not in newInd1['Positions']): #Validation to prevent duplicate newInd1["White"].append(tup[0]['White'][i]) newInd1["Positions"].append(tup[0]['Positions'][i]) newInd2["White"].append(tup[1]['White'][i]) newInd2["Positions"].append(tup[1]['Positions'][i]) else: newInd1["White"].append(tup[1]['White'][i]) newInd1["Positions"].append(tup[1]['Positions'][i]) newInd2["White"].append(tup[0]['White'][i]) newInd2["Positions"].append(tup[0]['Positions'][i]) else : if (tup[1]['White'][i] not in newInd1['Positions']): newInd1["White"].append(tup[1]['White'][i]) newInd1["Positions"].append(tup[1]['Positions'][i]) newInd2["White"].append(tup[0]['White'][i]) newInd2["Positions"].append(tup[0]['Positions'][i]) else: newInd1["White"].append(tup[0]['White'][i]) newInd1["Positions"].append(tup[0]['Positions'][i]) newInd2["White"].append(tup[1]['White'][i]) newInd2["Positions"].append(tup[1]['Positions'][i]) #Crossovering Black Piece Postition for i in range(0,len(tup[0]['Black'])): rn = random.SystemRandom().randint(0,1) if(rn == 0): if (tup[0]['Black'][i] not in newInd1['Positions']): newInd1["Black"].append(tup[0]['Black'][i]) newInd1["Positions"].append(tup[0]['Positions'][i+len(newInd1['White'])]) newInd2["Black"].append(tup[1]['Black'][i]) newInd2["Positions"].append(tup[1]['Positions'][i+len(newInd2['White'])]) else: newInd1["Black"].append(tup[1]['Black'][i]) newInd1["Positions"].append(tup[1]['Positions'][i+len(newInd1['White'])]) newInd2["Black"].append(tup[0]['Black'][i]) newInd2["Positions"].append(tup[0]['Positions'][i+len(newInd2['White'])]) else : if (tup[1]['Black'][i] not in newInd1['Positions']): newInd1["Black"].append(tup[1]['Black'][i]) newInd1["Positions"].append(tup[1]['Positions'][i+len(newInd1['White'])]) newInd2["Black"].append(tup[0]['Black'][i]) newInd2["Positions"].append(tup[0]['Positions'][i+len(newInd2['White'])]) else: newInd1["Black"].append(tup[0]['Black'][i]) newInd1["Positions"].append(tup[0]['Positions'][i+len(newInd1['White'])]) newInd2["Black"].append(tup[1]['Black'][i]) newInd2["Positions"].append(tup[1]['Positions'][i+len(newInd2['White'])]) #Calculating the new cost newInd1['Cost'] = countThreat1(newInd1['White'],newInd1['Positions']) + countThreat1(newInd1['Black'],newInd1['Positions']) - countThreat2(newInd1['White'],newInd1['Black'],newInd1['Positions']) newInd2['Cost'] = countThreat1(newInd2['White'],newInd2['Positions']) + countThreat1(newInd2['Black'],newInd2['Positions']) - countThreat2(newInd2['White'],newInd2['Black'],newInd2['Positions']) newInd.append(newInd1) newInd.append(newInd2) return(newInd)