Wiki Python Fr   CodesVpython UserPreferences
 
HelpContents FindPage Diffs Info Edit Subscribe XML Print View

1. Exemple de programme en VPYTHON

1.1. 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

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
#!/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()

1.2. 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


1.3. Résultat



PythonPowered