def __init__(self): self.directions = [] self.gooddirections = [] self.critters = {} self.plants = [] #Gets Mapsize, PlantBites and PlantPercent Setting From Algorithm self.size = ppa.getSetting("mapsize") self.plantbites = ppa.getSetting("plantbites") self.plantpercent = ppa.getSetting("plantpercent") #Fill The Map With Plants Using Percentage for _ in range(int(self.plantpercent*self.size*self.size)): #Puts Plant In Random Location loc = (random.randint(0, self.size-1), random.randint(0, self.size-1),self.plantbites) while loc in self.plants: loc = (random.randint(0, self.size-1), random.randint(0, self.size-1),self.plantbites) self.plants.append(loc)
def createmask(): ranges = predpreyalgorithm.getSetting("inputranges") inputranges = numpy.array(ranges) #Ensures that the highest number will occur pdfsize = int(inputranges.prod() * predpreyalgorithm.getSetting("pdfpercent")) rangecount = len(inputranges) choices = predpreyalgorithm.getSetting("choices") increment = predpreyalgorithm.getSetting("mutationincrement") #Generate the random input random_inputs = (numpy.random.random((pdfsize, rangecount))*inputranges).astype(numpy.uint8) #Generate the historgrams to go with them histograms = numpy.random.random_integers(low=1,high=255,size=(pdfsize,7)).astype(numpy.uint8) #print("Number of histograms: %d "% len(histograms) ) #print("Entry count: %d" % len(histograms[0])) mask = {} for ri,histogram in zip(random_inputs, histograms): mask[tuple(ri)] = histogram return mask
def predMakeMove(pred, world): for directionMove in pred.getMoves(world.getSensoryData(pred, ppa.getSetting("sight"))): destinationTile = world.getCritterDest(pred, directionMove) if destinationTile == None: continue critterOnTile = world.getCritterAt(destinationTile) if critterOnTile == None: world.moveCritter(pred, directionMove) return elif critterOnTile.type == critter.PREY: world.removeCritter(critterOnTile) pred.setStatus("hunger", 0) #print("Pred Did Work") return elif critterOnTile.type == critter.PREDATOR: continue else: raise Exception("There is a predator case that is not accounted for: " + critterOnTile)
def calcscore(pred_mask={}, prey_mask={}, hooker=None): mapsize = ppa.getSetting("mapsize") vegpercent = ppa.getSetting("plantpercent") plantbites = ppa.getSetting("plantbites") preypercent = ppa.getSetting("preypercent") predpercent = ppa.getSetting("predpercent") maxhunger = ppa.getSetting("maxhunger") sight = ppa.getSetting("sight") world = Map() for _ in range(int((mapsize**2)*predpercent)): world.setCritterAt(world.getRandomUntakenTile(), critter.Critter(pred_mask, critter.PREDATOR)) for _ in range(int((mapsize**2)*preypercent)): world.setCritterAt(world.getRandomUntakenTile(), critter.Critter(prey_mask, critter.PREY)) score = 0 while len(world.getPredators()) > 0 and len(world.getPreys()) > 0: score += 1 critters = world.getPredators() + world.getPreys() random.shuffle(critters) if critters == None: raise Exception("Critter cannot be none!!") for c in critters: if world.getCritterXY(c) == None: continue c.incrementStatus("hunger", 1) if c.type == critter.PREY: preyMakeMove(c, world) #print("Prey Move") elif c.type == critter.PREDATOR: predMakeMove(c, world) #print("Pred Move") else: raise Exception("Something that is not a critter is in the map: " + c) if c.getStatus("hunger") >= ppa.getSetting("maxhunger") and world.getCritterXY(c) != None: world.removeCritter(c) if hooker != None: hooker(world, score) #print("Next") if hooker != None: hooker(world, score) return score
def preyMakeMove(prey, world): for directionMove in prey.getMoves(world.getSensoryData(prey, ppa.getSetting("sight"))): destinationTile = world.getCritterDest(prey, directionMove) if destinationTile == None: continue critterOnTile = world.getCritterAt(destinationTile) if critterOnTile == None: world.moveCritter(prey, directionMove) if world.isPlant(destinationTile): #isPlant bites for me prey.setStatus("hunger", 0) return elif critterOnTile.type == critter.PREY: continue elif critterOnTile.type == critter.PREDATOR: world.removeCritter(prey) critterOnTile.setStatus("hunger", 0) #print("Prey Dumb Jumped Inside") return else: raise Exception("There is a prey case that is not accounted for: " + critterOnTile)
def _getHungerChunk(self): hunger = self.status["hunger"] for i, chunk in enumerate(ppa.getSetting("hungerchunks")): if hunger <= chunk: return i return len(ppa.getSetting("hungerchunks"))
def __init__(self, mask, type="No type defined"): self.choices = ppa.getSetting("choices") self.status = {"hunger":0} self.type = type self.pdfmatrix = ppa.best_pred if self.type == PREDATOR else ppa.best_prey self.mask = mask
def getChunk(self, number): chunks = ppa.getSetting("distancechunks") for i,val in enumerate(chunks): if number < val: return i return len(chunks)