[PageD'Accueil] [IndexDesTitres] [IndexDesTermes

Exemple de programme en VPYTHON

Script python

-Installer le plugin- visual python:
http://www.vpython.org/VisualOverview.html
http://www.vpython.org/download.html

fichier : copier le source suivant dans le fichier Environnement.py

#!/usr/bin/python
from visual import *
#décors
from random import Random

class Position:
   def __init__(self,x,y,z):
      self.theArray= array([y,z,x])
      self.x = self.theArray[2]
      self.z = self.theArray[0]
      self.y = self.theArray [1]


class Environnement:
      def __init__(self,tailleMonde=20):
         self.longueurMonde = tailleMonde
         self.limitesMonde = Position(self.longueurMonde,self.longueurMonde,self.longueurMonde)
         self.theScene = scene #VPYTHON ENTRY
         self.theRate = rate  #VPYTHON ENTRY
         self.theRateValue = 100
         self.sourceRandom = Random()
         self.listeAnimal     = []
         self.listeRessources = []
         self.nbIteration = 0
         self.nbIterationMax = 2000000
         self.g = 9.8
         self.dt = 0.1
         
         self.initScene()
         self.createDecors()
         
      def initScene(self):
         self.theScene.x =0
         self.theScene.y =0
         self.theScene.forward = array((0.00001,-10,0))
         self.theScene.scale =(0.1,0.1,0.1)
         self.theScene.title = "Fourmis!"
         self.theScene.fullscreen = 1
               
      def createDecors(self):
         hauteurCone = 1
         for numcone in range(1,self.longueurMonde* 15):
            if(self.sourceRandom.choice([-1,1]) == 1):
               coneTemp= sphere(pos=(self.longueurMonde *self.sourceRandom.random()*self.sourceRandom.choice([-1,1]),0,self.longueurMonde*self.sourceRandom.random()*self.sourceRandom.choice([-1,1])),
                               radius = 0.25, 
                               color = [0.5*self.sourceRandom.random() ,0.1,0.5])
            else:
               coneTemp = cone(pos=(self.longueurMonde *self.sourceRandom.random()*self.sourceRandom.choice([-1,1]),0,self.longueurMonde*self.sourceRandom.random()*self.sourceRandom.choice([-1,1])),
                               axis = (0,hauteurCone*self.sourceRandom.random()+0.5,0),
                               radius = 0.5, 
                               color = [0.5*self.sourceRandom.random() ,0.1,0.5*self.sourceRandom.random()])
      
            self.listeRessources.append(coneTemp)
            coneTemp.velocity = vector(0,0,0)
         
         coneReference = cone(pos=(0,0,0), axis=(0,3,0), radius=0.5, color=color.red)
         self.drawEarth ()
         theGround = box(pos=(0,0,0), length = 2*self.longueurMonde, height=0.01, width = 2* self.longueurMonde, color =[1.0,1.0,0.2])

      def drawEarth(self):
         #CONVEX
         from random import uniform
         L = []
         c = convex(color=(0,0,0.5))
         for i in range(100):
             L.append(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1))))
         c.pos = L
      
         L = []
         d = convex(color=(0,0.5,0.5))
         for i in range(100):
             L.append(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1))))
         d.pos = L
      
         L = []
         e = convex(color=(0.5,0,0.5))
         for i in range(100):
             L.append(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1))))
         e.pos = L
      
      def addAnimals(self,TypeOfAnimal,number):
         #creation des fourmis
         for numeroAnimal in range(0,number):
            position=  (self.longueurMonde*self.sourceRandom.random()*self.sourceRandom.choice([-1,1]),
                        0.5,
                        self.longueurMonde*self.sourceRandom.random()*self.sourceRandom.choice([-1,1]))
                        
            animalTemp = TypeOfAnimal(position)
            self.listeAnimal.append(animalTemp)
            
      def  addDefaultAnimals(self):
         pass

      def goWorld(self):
            self.theRate(self.theRateValue)
            
            while self.nbIteration < self.nbIterationMax:
               self.nbIteration += 1

               #Pour chaque fourmi, deplacer la fourmi
               #si une fourmi rencontre un grain de sable, elle le déplace en hauteur et 
               #~ du mouvement qu'elle a réalisé pour le heurter
               
               for currentAnimal in self.listeAnimal:
                  currentAnimal.move(self)

               #~ for currentRessource in self.listeRessources:
                  #~ if currentRessource.y > 0.25:
                     #~ currentRessource.pos = currentRessource.pos + currentRessource.velocity*self.dt
                     #~ currentRessource.velocity.y = currentRessource.velocity.y - self.g*self.dt 
         






fichier : copier le source suivant dans le fichier Fourmi.py

#!/usr/bin/python
#UTILISER LES trois boutons de la souris pour zoomer, rotation, ...
#~ NB: array((Y,Z,X))
from visual import *
#décors
from random import Random
 

from Environnement import Environnement
class Fourmi:
   ID=0
   sourceRandom = Random()
   def __init__(self,position):
      #CREATION DE LA FOURMI

      self.forme = frame(name ="fourmi"+str(Fourmi.ID))
      Fourmi.ID+=1
      self.seuilCapteur = 0.05
      self.facteurDepl  = 0.05
         
      self.pasDeplacement = 1*Fourmi.sourceRandom.random()+0.1
      self.deplacememtPrivilegie =  array((self.pasDeplacement*Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1]),
                                    0,
                                    self.pasDeplacement*Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1])))
                                    
      #MOUVEMENT DE LA FOURMI
      self.forme.pos = position
                        
      self.forme.color = (Fourmi.sourceRandom.random(),Fourmi.sourceRandom.random(),Fourmi.sourceRandom.random())
      self.trajectoireFourmi = curve(radius = 0 ,color = self.forme.color)

      #corps
      sphere(frame =  self.forme, pos=(1,0,0), radius=0.28, color=color.white)
      sphere(frame =  self.forme, pos=(0.25,0,0), radius=0.25, color=self.forme.color)
      sphere(frame =  self.forme, pos=(0.60,0), radius=0.20, color=color.white)
      #yeux
      sphere(frame =  self.forme, pos=(0,0.1,-0.15), radius=0.05, color=color.red)
      sphere(frame =  self.forme, pos=(0,0.1,0.15), radius=0.05, color=color.red)
      
      #pattes
      
      oscillationPatte = 0.25
      leg1 = cylinder(frame =  self.forme  , pos = (0.4,0,0.15),  radius = 0.01, axis = (-oscillationPatte,-0.5,0), color = color.blue)
      leg2 = cylinder(frame =  self.forme ,  pos = (0.4,0,-0.15), radius = 0.01, axis = (oscillationPatte,-0.5,0), color = color.blue)
      
      leg3 = cylinder(frame =  self.forme  , pos = (1,0,0.15),    radius = 0.01, axis = (-oscillationPatte,-0.5,0), color = color.blue)
      leg4 = cylinder(frame =  self.forme ,  pos = (1,0,-0.15),   radius = 0.01, axis = (oscillationPatte,-0.5,0), color = color.blue)
      
      leg5 = cylinder(frame =  self.forme  , pos = (0.6,0,0.15),  radius = 0.01, axis = (-oscillationPatte,-0.5,0), color = color.blue)
      leg6 = cylinder(frame =  self.forme,   pos = (0.6,0,-0.15),  radius = 0.01, axis = (oscillationPatte,-0.5,0), color = color.blue)
      
   #faire bouger les pattes
   def changeDirection(self):
      #faire bouger les pattes
      self.pasDeplacement = 0.5*Fourmi.sourceRandom.random()+0.01
      self.deplacememtPrivilegie =  array((self.pasDeplacement*Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1]),
                                    0,
                                    self.pasDeplacement*Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1])))
      self.forme.axis = - (self.deplacememtPrivilegie)
      self.moveLegs()
      
   def estHorsLimites(self,limitMonde):
            reponse = 0
            formeFourmi = self.forme
            
            if (formeFourmi.x >0):
                  if(formeFourmi.x + self.seuilCapteur >limitMonde.x ):
                     reponse =1
                     formeFourmi.x  = limitMonde.x
            if (formeFourmi.x <0):
                  if(formeFourmi.x - self.seuilCapteur < -limitMonde.x ):
                     reponse =1
                     formeFourmi.x  = -limitMonde.x
                     
            if (formeFourmi.z >0): #Z est Y
                  if(formeFourmi.z + self.seuilCapteur >limitMonde.z ):
                     formeFourmi.z  = limitMonde.z
                     reponse =1
            if (formeFourmi.z <0):
                  if(formeFourmi.z - self.seuilCapteur < -limitMonde.z):
                     formeFourmi.z  = -limitMonde.z
                     reponse =1
            return reponse

   def estdans(self, forme1):
            reponse =0
            formeFourmi = self.forme
            from math import fabs

            formeFourmi = self.forme
            if ((formeFourmi.x - forme1.x)*(formeFourmi.x - forme1.x) + \
               (formeFourmi.y - forme1.y)*(formeFourmi.y - forme1.y)  +\
               (formeFourmi.z - forme1.z)*(formeFourmi.z - forme1.z) < 4):
                reponse =1
                #~ print("estdans!")
                #~ print("formeFourmi.x - forme1.x! ")+str(formeFourmi.x - forme1.x)
                #~ print("formeFourmi.z - forme1.z! ")+str( formeFourmi.z - forme1.z )
            return reponse
      
   #faire bouger les pattes
   def moveLegs(self):
      #faire bouger les pattes
      def flip(aForme):
         aForme.axis [0] = - aForme.axis[0]
         aForme.radius = aForme.radius 
      
      for patte in self.forme.objects:
         flip(patte)
         
   def move(self,world):
         depl = array((Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1])*self.facteurDepl,
                        0,
                        Fourmi.sourceRandom.random()*Fourmi.sourceRandom.choice([-1,1])*self.facteurDepl
                      ))

         self.forme.pos = self.forme.pos + self.deplacememtPrivilegie + depl
         #positionnement orientation fourmi
         self.forme.axis = - (depl + self.deplacememtPrivilegie)
         self.moveLegs()
         #~ self.trajectoireFourmi.append(pos = self.forme.pos + array((0,-0.4,0)) )
         
         #Detection des ressources
         #~ for object in world.listeRessources:
            #~ if object.__class__  == sphere:
               #~ if self.estdans(object):
                  #~ object.pos = object.pos + array((0,0.5,0))
               
         if(self.estHorsLimites(world.limitesMonde)):
             #modifier deplacement priviligié
             self.changeDirection()
         else:
            #gestion des chocs avec autres fourmis
            for otherAnimal in world.listeAnimal:
               if(otherAnimal != self):
                  if(self.estdans(otherAnimal.forme)):
                     self.changeDirection()



def main():
   env = Environnement(tailleMonde = 15)
   antsNumber = 15
   env.addAnimals(Fourmi,antsNumber)
   env.goWorld()

main()

Page HTML

Vpython

la puissance de Vpython 


Ceci est un exemple de programme qui utilise le module d'extension Vpython
( http://www.vpython.org/ ).Le site contient un tutorial et l'installation est intuitive.

Voici un exemple d'agents (fourmis) qui évoluent 
dans un environnement: le but n'est pas de surcharger le code d'intelligence artificielle distribuée (IAD)
 mais de vous montrer la rapidité avec laquelle on peut effectuer une représentation 3D d'un système multiAgent avec VPython.

pour toutes suggestions:   fabricedefougerolles@yahoo.fr

Résultat


2016-06-05 21:42