Exemplo n.º 1
0
    def CreateNode(self, point, memObject):
        xNoise = Global.Randint(-Global.ELNodeAddNoise, Global.ELNodeAddNoise)
        yNoise = Global.Randint(-Global.ELNodeAddNoise, Global.ELNodeAddNoise)
        x = float(point.x + xNoise)
        y = float(point.y + yNoise)

        while not self.area.IsInside(Point(x, y)):
            xNoise = Global.Randint(-Global.ELNodeAddNoise,
                                    Global.ELNodeAddNoise)
            yNoise = Global.Randint(-Global.ELNodeAddNoise,
                                    Global.ELNodeAddNoise)
            x = float(point.x + xNoise)
            y = float(point.y + yNoise)

        newNode = EnergyLayerNode(self, x, y, self.nodeIndex)
        self.nodeIndex = self.nodeIndex + 1
        self.nodes.append(newNode)

        if Global.CreatePlaces:
            place = self.GetPlaceForNode(newNode)
            place.nodes.append(newNode)
            newNode.place = place

        memObject.AddLinkToNode(newNode)
        memObject.IntenseToNode(newNode, Global.MemObjIntenseToNewNode)
        self.stepELNodesCreated = self.stepELNodesCreated + 1
        return newNode
Exemplo n.º 2
0
    def CreateMap(self):
        areaArea = self.area.GetArea()
        nodeCount = areaArea / Global.ELDensity**2
        self.desiredNodeCount = nodeCount * 2
        self.minimalDesiredNodeCount = self.desiredNodeCount / 5
        self.maximalDesiredNodeCount = self.desiredNodeCount

        x = self.area.points[0].x
        y = self.area.points[0].y
        if Global.CreatePlaces:
            rootPlace = Place(self, self.placeIndex, self.area.width / 2 + x,
                              self.area.height / 2 + y)
            rootPlace.range = max(x + self.area.width,
                                  y + self.area.height) * sqrt(2) / 2
            rootPlace.range = ceil(rootPlace.range)
            rootPlace.startRange = rootPlace.range
            self.places.append(rootPlace)

        if Global.ELCreateNoise == -1:  #completely random distribution of nodes
            while len(self.nodes) < nodeCount:
                x = float(Global.Randint(0, self.area.width - 1))
                y = float(Global.Randint(0, self.area.height - 1))

                if self.area.IsInside(Point(x, y)):
                    node = EnergyLayerNode(self, x, y, self.nodeIndex)
                    self.nodeIndex = self.nodeIndex + 1
                    self.nodes.append(node)
                    if Global.CreatePlaces:
                        node.place = rootPlace
                        rootPlace.nodes.append(node)
        else:
            xCount = self.area.width / Global.ELDensity
            yCount = self.area.height / Global.ELDensity
            density = Global.ELDensity
            for y in range(yCount):
                for x in range(xCount):
                    xNoise = Global.Randint(-Global.ELCreateNoise,
                                            Global.ELCreateNoise)
                    yNoise = Global.Randint(-Global.ELCreateNoise,
                                            Global.ELCreateNoise)
                    xx = float(x * density + density / 2 + xNoise)
                    yy = float(y * density + density / 2 + yNoise)

                    if self.area.IsInside(Point(xx, yy)):
                        node = EnergyLayerNode(self, xx, yy, self.nodeIndex)
                        self.nodeIndex = self.nodeIndex + 1
                        self.nodes.append(node)
                        if Global.CreatePlaces:
                            node.place = rootPlace
                            rootPlace.nodes.append(node)
Exemplo n.º 3
0
    def Step(self):
        action = self.intelligence.GetAction()
        map = Global.Map

        self.x = self.newX
        self.y = self.newY
        self.viewCones = self.viewConesNormal  #fake else-than-Explore/LookForObject-action branch
        self.viewConeMaxDist = self.viewConeNormalMaxDist

        #execute action - world/agent-impacting part of atomic process
        if action.process.name == "ExecuteReal":
            action.sources = action.parent.parent.process.sources
            Global.Log("AGENT is doing " + action.data['process'].name +
                       " for " + str(action.duration) + " seconds")
            self.intelligence.UseObjects(action.parent.parent)
            #map.UseObjects(self, action.parent) done in PF.UseObjects
        elif action.process.name == "Execute":
            pass  #never happen - done as ExecuteReal or MoveTo(Partial)

        elif action.process.name == "SearchRandom":
            pass  #never happens - done as MoveTo or Explore child process
        elif action.process.name == "LookUpInMemory":
            pass  #never happens - done as Remember, MoveTo or LookForObject child process

        elif action.process.name == "Remember":
            action.duration = Global.Randint(1, 10)
            action.data["phantom"] = self.intelligence.RememberObjectsFor(
                action.data["affordance"])
            if action.data["phantom"] != None:
                Global.Log("AGENT is remembering for " +
                           action.data["affordance"].name +
                           "(there should be " +
                           action.data["phantom"].object.type.name + " at " +
                           str(action.data["phantom"].object.x) + "," +
                           str(action.data["phantom"].object.y) + ")  for " +
                           str(action.duration) + " seconds")
            else:
                Global.Log("AGENT is remembering for " +
                           action.data["affordance"].name +
                           "(nothing in SM/MA) for " + str(action.duration) +
                           " seconds")

        elif action.process.name == "LookForObject":
            self.viewCones = self.viewConesForExplore
            self.viewConeMaxDist = self.viewConeForExploreMaxDist
            action.duration = Global.Randint(1, 5)
            action.data["object"] = self.intelligence.LookForObject(
                action.data["phantom"])
            if action.data["object"] != None:
                Global.Log("AGENT is looking for " +
                           action.data["phantom"].object.type.name +
                           "(Found) for " + str(action.duration) + " seconds")
            else:
                Global.Log("AGENT is looking for " +
                           action.data["phantom"].object.type.name +
                           "(NotFound) for " + str(action.duration) +
                           " seconds")

        elif action.process.name == "MoveTo":
            pass  #never happens - done as MoveToPartial
        elif action.process.name == "MoveToPartial":
            dx = action.data['newx'] - self.newX
            dy = action.data['newy'] - self.newY
            angle = atan2(dx, dy)
            self.dirAngle = angle

            action.duration = map.MoveAgent(self, action.data['newx'],
                                            action.data['newy'])
            self.intelligence.UpdatePhantomsBecauseOfMove()
            Global.Log("AGENT is moving to " + str(action.data['newx']) + "," +
                       str(action.data['newy']) + " for " +
                       str(action.duration) + " seconds")

        elif action.process.name == "Explore":
            self.viewCones = self.viewConesForExplore
            self.viewConeMaxDist = self.viewConeForExploreMaxDist
            action.duration = Global.Randint(5, 20)
            action.sources = [action.data['affordance']]
            Global.Log("AGENT is exploring for " +
                       action.data['affordance'].name + " for " +
                       str(action.duration) + " seconds")
        else:
            Global.Log("AGENT is a bit CONFUSED doing " + action.process.name)

        #sees object around
        visibleObjects = map.GetVisibleObjects(self)
        self.intelligence.NoticeObjects(visibleObjects, action)
        self.intelligence.perceptionField.Update(action)
        self.intelligence.memoryArea.Update(action)

        self.paText = self.intelligence.processArea.GetText()
        Global.Time.AddSeconds(action.duration)
        self.intelligence.ActionDone()

        self.intelligence.spaceMap.StepUpdate(action)
Exemplo n.º 4
0
    def GetAtomicActionforSmartProcess(self, emotion, excProcess):
        if (excProcess.process.name == "SearchRandom"):

            if (excProcess.data["step"] == "MoveTo"):
                
                if excProcess.data["waypoints"] == None:
                    map = Global.Map
                    ws = copy(map.wayPoints)
                    ws.sort(lambda a,b: cmp(a.lastVisited,b.lastVisited))
                    excProcess.data["waypoints"] = ws
                    
                if len(excProcess.data["waypoints"]) < 1:
                    self.processArea.TerminateProcess(emotion, False)
                    return self.GetAction(emotion)
                
                wayPointToGo = Global.Choice(excProcess.data["waypoints"])
                excProcess.data["waypoints"].remove(wayPointToGo)
                excProcess.data["step"] = "Explore"
                
                canMove = False
                map = Global.Map
                while not canMove:
                    newX = Global.Randint(-Global.WayPointNoise, Global.WayPointNoise) + wayPointToGo.x
                    newY = Global.Randint(-Global.WayPointNoise, Global.WayPointNoise) + wayPointToGo.y
                    canMove = map.IsInside( Point(newX,newY) )
                    
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["MoveTo"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["process"] = excProcess.process
                atomicProcess.data["newx"] = newX
                atomicProcess.data["newy"] = newY
                return self.GetAtomicActionforSmartProcess(emotion, atomicProcess)                                
                
            elif (excProcess.data["step"] == "Explore"):
                excProcess.data["step"] = "MoveTo"
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["Explore"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["parent"] = "process"
                atomicProcess.data["process"] = excProcess.excParentIntention.parentExcProcess
                atomicProcess.data["affordance"] = excProcess.data["affordance"]
                return atomicProcess
            
        elif (excProcess.process.name == "LookUpInMemory"):
            
            if (excProcess.data["step"] == "Remember"):
                excProcess.data["step"] = "MoveTo"
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["Remember"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["process"] = excProcess.process
                atomicProcess.data["affordance"] = excProcess.data["affordance"]
                return atomicProcess
            
            elif (excProcess.data["step"] == "MoveTo"):
                excProcess.data["step"] = "LookForObject"
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["MoveTo"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["process"] = excProcess.process
                
                memObj = excProcess.data["phantom"].object
                atomicProcess.data["newx"] = memObj.x
                atomicProcess.data["newy"] = memObj.y
                return self.GetAtomicActionforSmartProcess(emotion, atomicProcess)
            
            elif (excProcess.data["step"] == "LookForObject"):
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["LookForObject"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["process"] = excProcess.process
                atomicProcess.data["affordance"] = excProcess.data["affordance"]
                atomicProcess.data["phantom"] = excProcess.data["phantom"]
                return atomicProcess    
            
            return None    
        elif (excProcess.process.name == "MoveTo"):
            if excProcess.data["path"] == None:
                map = Global.Map
                path = map.GetPath(self.agent, excProcess.data["newx"], excProcess.data["newy"])
                if path == None:
                    self.processArea.TerminateProcess(emotion, False)
                    return self.GetAction(emotion)
                excProcess.data["path"] = path[1:]
            #we have path (without start)
            nextPoint = excProcess.data["path"].pop(0)
            atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["MoveToPartial"], excProcess.excParentIntention, excProcess)
            atomicProcess.data["process"] = excProcess.process
            atomicProcess.data["newx"] = nextPoint.x
            atomicProcess.data["newy"] = nextPoint.y
            return atomicProcess
        elif (excProcess.process.name == "Execute"):
            
            if 'phantom' not in excProcess.data: 
                #expecting enough E-phantoms, we choose one and let go the rest
                #Future: for |process.sources|>1 add selection of phantoms regarding theirs affs
                if len(excProcess.parent.resources)> 1:
                    phantoms = filter(lambda x:x.GetType()=="e", excProcess.parent.resources)
                    phToDist = {}
                    map = Global.Map
                    for p in phantoms:
                        phToDist[p] = map.DistanceObj(self.agent.newX, self.agent.newY, p.object) #Future: use object.attractivity, .visibility in sort/cmp etc.
                    phantoms.sort(lambda a,b: cmp(phToDist[a], phToDist[b]))
                    phantom = phantoms[0]
                else:
                    phantom = excProcess.parent.resources[0]
                excProcess.data['phantom'] = phantom            #Future: list instead of one phantom

            #following test of in range is paranoid - could be done only in first iteration
            object = excProcess.data['phantom'].object
            map = Global.Map
            dist = map.DistanceObj(self.agent.newX, self.agent.newY, object)
            if dist < Global.MapPickUpDistance:
                 atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["ExecuteReal"], excProcess.excParentIntention, excProcess)
                 atomicProcess.data["process"] = excProcess.data["process"]
                 atomicProcess.data["phantom"] = excProcess.data["phantom"]
                 atomicProcess.duration = excProcess.duration
                 excProcess.parent.resources = [excProcess.data["phantom"]]
                 return atomicProcess
            else:
                atomicProcess = self.processArea.ActivateProcess(emotion, self.processes.atomic["MoveTo"], excProcess.excParentIntention, excProcess)
                atomicProcess.data["newx"] = object.x
                atomicProcess.data["newy"] = object.y
                return self.GetAtomicActionforSmartProcess(emotion, atomicProcess)
        else:
            return excProcess