def simulated_annealing(self, function_to_optimize, step_size, max_temp, xmin, xmax, ymin, ymax, startx=None, starty=None): if startx == None: startx = xmin if starty == None: starty = ymin currX = startx currY = starty temp = max_temp currValue = function_to_optimize(currX, currY) searching = True coord = Coordinate(currX, currY, currValue) while searching: progressed = False searching = False xPos = currX + step_size #Attempt to increase X by the step if xPos <= xmax: temp -= self.TEMP_DECREASE newVal = function_to_optimize(xPos, currY) xTemp = self.temp_function(temp, newVal, currValue) if newVal < currValue or xTemp: self.print_flag("CLIMBING BY INC. X " + str(xPos), self.SA_PROGRESS) if xTemp: xPos += step_size currValue = newVal currX = xPos searching = True progressed = True xNeg = currX - step_size #Attempt instead to decrease X by the step if xNeg >= xmin: temp -= self.TEMP_DECREASE newVal = function_to_optimize(xNeg, currY) xTemp = self.temp_function(temp, newVal, currValue) if newVal < currValue: self.print_flag("CLIMBING BY DEC. X " + str(xNeg), self.SA_PROGRESS) currValue = newVal currX = xNeg searching = True progressed = True yPos = currY + step_size #Attempt to increase Y by the step if yPos <= ymax: temp -= self.TEMP_DECREASE newVal = function_to_optimize(currX, yPos) yTemp = self.temp_function(temp, newVal, currValue) if newVal < currValue or yTemp: self.print_flag("CLIMBING BY INC. Y", self.SA_PROGRESS) if yTemp: yPos += step_size currValue = newVal currY = yPos searching = True progressed = True yNeg = currY - step_size #Attempt to decrease Y by the step if yNeg >= ymin: temp -= self.TEMP_DECREASE newVal = function_to_optimize(currX, yNeg) yTemp = self.temp_function(temp, newVal, currValue) if newVal < currValue: self.print_flag("CLIMBING BY DEC. Y", self.SA_PROGRESS) currValue = newVal currY = yNeg searching = True progressed = True #If we don't go anywhere, don't add the same coordinates twice if progressed: coord.addX(currX) coord.addY(currY) coord.addZ(currValue) coord.setX(currX) coord.setY(currY) coord.setValue(currValue) self.print_flag( "MIN FOUND X:" + str(currX) + " Y:" + str(currY) + " VALUE:" + str(currValue) + "\n", self.SA) return coord
def simulated_annealing(self,function_to_optimize, step_size, max_temp, xmin, xmax, ymin, ymax, startx=None, starty=None): if startx == None: startx=xmin if starty == None: starty=ymin currX = startx currY = starty temp = max_temp currValue = function_to_optimize(currX,currY) searching = True coord = Coordinate(currX,currY,currValue) while searching: progressed = False searching = False xPos = currX + step_size #Attempt to increase X by the step if xPos <= xmax: temp -= self.TEMP_DECREASE newVal = function_to_optimize(xPos,currY) xTemp = self.temp_function(temp,newVal,currValue) if newVal < currValue or xTemp: self.print_flag("CLIMBING BY INC. X "+str(xPos),self.SA_PROGRESS) if xTemp: xPos += step_size currValue = newVal currX = xPos searching = True progressed = True xNeg = currX - step_size #Attempt instead to decrease X by the step if xNeg >= xmin: temp -= self.TEMP_DECREASE newVal = function_to_optimize(xNeg,currY) xTemp = self.temp_function(temp,newVal,currValue) if newVal < currValue: self.print_flag("CLIMBING BY DEC. X "+str(xNeg),self.SA_PROGRESS) currValue = newVal currX = xNeg searching = True progressed = True yPos = currY + step_size #Attempt to increase Y by the step if yPos <= ymax: temp -= self.TEMP_DECREASE newVal = function_to_optimize(currX,yPos) yTemp = self.temp_function(temp,newVal,currValue) if newVal < currValue or yTemp: self.print_flag("CLIMBING BY INC. Y",self.SA_PROGRESS) if yTemp: yPos += step_size currValue = newVal currY = yPos searching = True progressed = True yNeg = currY - step_size #Attempt to decrease Y by the step if yNeg >= ymin: temp -= self.TEMP_DECREASE newVal = function_to_optimize(currX,yNeg) yTemp = self.temp_function(temp,newVal,currValue) if newVal < currValue: self.print_flag("CLIMBING BY DEC. Y",self.SA_PROGRESS) currValue = newVal currY = yNeg searching = True progressed = True #If we don't go anywhere, don't add the same coordinates twice if progressed: coord.addX(currX) coord.addY(currY) coord.addZ(currValue) coord.setX(currX) coord.setY(currY) coord.setValue(currValue) self.print_flag("MIN FOUND X:"+str(currX)+" Y:"+str(currY)+" VALUE:"+str(currValue)+"\n",self.SA) return coord
def hill_climb(self, function_to_optimize, step_size, xmin, xmax, ymin, ymax, startx=None, starty=None): if startx == None: startx = xmin if starty == None: starty = ymin currX = startx currY = starty currValue = function_to_optimize(currX, currY) coord = Coordinate(currX, currY, currValue) searching = True while searching: searching = False xPos = currX + step_size #Attempt to increase X by the step if xPos <= xmax: newVal = function_to_optimize(xPos, currY) if newVal < currValue: self.print_flag("CLIMBING BY INC. X " + str(xPos), self.HILL_PROGRESS) currValue = newVal currX = xPos searching = True xNeg = currX - step_size #Attempt instead to decrease X by the step if xNeg >= xmin: newVal = function_to_optimize(xNeg, currY) if newVal < currValue: self.print_flag("CLIMBING BY DEC. X " + str(xNeg), self.HILL_PROGRESS) currValue = newVal currX = xNeg searching = True yPos = currY + step_size #Attempt to increase Y by the step if yPos <= ymax: newVal = function_to_optimize(currX, yPos) if newVal < currValue: self.print_flag("CLIMBING BY INC. Y", self.HILL_PROGRESS) currValue = newVal currY = yPos searching = True yNeg = currY - step_size #Attempt to decrease Y by the step if yNeg >= ymin: newVal = function_to_optimize(currX, yNeg) if newVal < currValue: self.print_flag("CLIMBING BY DEC. Y " + str(yNeg), self.HILL_PROGRESS) currValue = newVal currY = yNeg searching = True coord.addX(currX) coord.addY(currY) coord.addZ(currValue) coord.setX(currX) coord.setY(currY) coord.setValue(currValue) self.print_flag( "MIN FOUND X:" + str(currX) + " Y:" + str(currY) + " VALUE:" + str(currValue) + "\n", self.HILL) return coord
def hill_climb(self, function_to_optimize,step_size,xmin,xmax,ymin,ymax,startx=None,starty=None): if startx == None: startx=xmin if starty == None: starty=ymin currX = startx currY = starty currValue = function_to_optimize(currX,currY) coord = Coordinate(currX,currY,currValue) searching = True while searching: searching = False xPos = currX + step_size #Attempt to increase X by the step if xPos <= xmax: newVal = function_to_optimize(xPos,currY) if newVal < currValue: self.print_flag("CLIMBING BY INC. X "+str(xPos),self.HILL_PROGRESS) currValue = newVal currX = xPos searching = True xNeg = currX - step_size #Attempt instead to decrease X by the step if xNeg >= xmin: newVal = function_to_optimize(xNeg,currY) if newVal < currValue: self.print_flag("CLIMBING BY DEC. X "+str(xNeg),self.HILL_PROGRESS) currValue = newVal currX = xNeg searching = True yPos = currY + step_size #Attempt to increase Y by the step if yPos <= ymax: newVal = function_to_optimize(currX,yPos) if newVal < currValue: self.print_flag("CLIMBING BY INC. Y",self.HILL_PROGRESS) currValue = newVal currY = yPos searching = True yNeg = currY - step_size #Attempt to decrease Y by the step if yNeg >= ymin: newVal = function_to_optimize(currX,yNeg) if newVal < currValue: self.print_flag("CLIMBING BY DEC. Y "+str(yNeg),self.HILL_PROGRESS) currValue = newVal currY = yNeg searching = True coord.addX(currX) coord.addY(currY) coord.addZ(currValue) coord.setX(currX) coord.setY(currY) coord.setValue(currValue) self.print_flag("MIN FOUND X:"+str(currX)+" Y:"+str(currY)+" VALUE:"+str(currValue)+"\n",self.HILL) return coord