Exemplo n.º 1
0
class Wanderer:
    def __init__(self, car, radius):

        self.car = car
        self.predict = Pvector(self.car.location.x, self.car.location.y)
        self.radius = radius

    def generate_target(self):

        if self.car.location.x < 0 or self.car.location.x > 800:
            self.car.velocity.x *= -1
        if self.car.location.y < 0 or self.car.location.y > 800:
            self.car.velocity.y *= -1
        self.predict = Pvector(self.car.location.x, self.car.location.y)
        self.predict.add(self.car.velocity)
        theta = uniform(self.car.velocity.heading() - np.pi / 2,
                        self.car.velocity.heading() + np.pi / 2)
        randvec = Pvector(np.cos(theta), np.sin(theta))
        randvec.mult(self.radius)
        self.predict.add(randvec)

    def display(self):

        self.generate_target()
        self.car.seek(self.predict)
        self.car.update()
        self.car.display()
Exemplo n.º 2
0
 def mutate(self, rate):
     for i in range(len(self.gene)):
         k = np.random.random()
         if k < rate:
             angle = np.random.uniform(-3.14, 3.14)
             vec = Pvector(np.cos(angle), np.sin(angle))
             vec.mult(np.random.uniform(0, self.maxforce))
             self.gene[i] = vec
Exemplo n.º 3
0
 def __init__(self,
              points=[
                  Pvector(0, 600),
                  Pvector(200, 300),
                  Pvector(400, 600),
                  Pvector(800, 600),
                  Pvector(1200, 0)
              ]):
     self.points = points
     self.rev = None
     self.fwd = None
Exemplo n.º 4
0
class Repeller:
    def __init__(self,x=400,y=400):
        self.loc=Pvector(x,y)
        self.dir=None
    def calculateForce(self,location):
        self.dir=Pvector(location.x,location.y)
        self.dir.sub(self.loc)
        self.dir.mult(150/(self.dir.magnitude()**3))
    def display(self):
        no_stroke()
        fill(0)
        ellipse(self.loc.x,self.loc.y,70,70)
Exemplo n.º 5
0
 def __init__(self, gene=None, lifetime=100):
     self.lifetime = lifetime
     self.maxforce = 4
     if gene == None:
         self.gene = []
         for _ in range(self.lifetime):
             angle = np.random.uniform(-np.pi, np.pi)
             vec = Pvector(np.cos(angle), np.sin(angle))
             vec.mult(np.random.uniform(0, self.maxforce))
             self.gene.append(vec)
     else:
         self.gene = gene
 def __init__(self,location=Pvector(0,0),velocity=Pvector(0,0),acceleration=Pvector(0,0),target=None,genes=None):
     self.location=location
     self.velocity=velocity
     self.acceleration=acceleration
     self.target=target
     self.r=10 
     self.lifetime=50
     if genes==None:
         self.genes=DNA(lifetime=self.lifetime)
     else:
         self.genes=genes
     print(self.genes.gene[2].x)
     self.gene_count=0
     self.fitness=0
     self.dead=False
Exemplo n.º 7
0
 def __init__(self, height, width, population):
     self.coordinates = []
     self.order = np.arange(population)
     for _ in range(population):
         self.coordinates.append(
             Pvector(random.randrange(0, width),
                     random.randrange(0, height / 2)))
 def __init__(self, population, target):
     self.pop_max = population
     self.rockets = []
     self.target = target
     for _ in range(self.pop_max):
         r = rocket(location=Pvector(400, 800), target=self.target)
         self.rockets.append(r)
 def seek_flow(self):
     if self.location.x<0:
         self.location.x=799
     if self.location.x>799:
         self.location.x=0
     if self.location.y<0:
         self.location.y=799
     if self.location.y>799:
         self.location.y=0
     desired=Pvector(self.lookup(self.location).x,self.lookup(self.location).y)
     desired.setMag(self.maxspeed)
     desired.sub(self.velocity)
     desired.limit(self.maxforce)
     self.applyForce(desired)
 def __init__(self,location=(400,400),velocity=(0,0),acceleration=(0,0),maxspeed=10,maxforce=3,field=None): # add field for field followers
     self.location=Pvector(location[0],location[1])
     self.velocity=Pvector(velocity[0],velocity[1])
     self.acceleration=Pvector(acceleration[0],acceleration[1])
     self.maxspeed=maxspeed
     self.maxforce=maxforce
     self.radius=8
     self.field=field
def get_normal(a,b,v):
    global points
    predict=v.velocity.get()
    predict.setMag(50)
    predict.add(v.location)
    predict.sub(a)
    alongpath=Pvector(b.x-a.x,b.y-a.y)
    alongpath.norm()
    mag=predict.dot(alongpath)
    alongpath.mult(mag)
    alongpath.add(a)
    if  points.points[-1].x>points.points[0].x:
        if alongpath.x>b.x or alongpath.x<a.x:
            alongpath=b.get()
    else:
        if alongpath.x<b.x or alongpath.x>a.x:
            alongpath=b.get()
    return alongpath
Exemplo n.º 12
0
    def generate_target(self):

        if self.car.location.x < 0 or self.car.location.x > 800:
            self.car.velocity.x *= -1
        if self.car.location.y < 0 or self.car.location.y > 800:
            self.car.velocity.y *= -1
        self.predict = Pvector(self.car.location.x, self.car.location.y)
        self.predict.add(self.car.velocity)
        theta = uniform(self.car.velocity.heading() - np.pi / 2,
                        self.car.velocity.heading() + np.pi / 2)
        randvec = Pvector(np.cos(theta), np.sin(theta))
        randvec.mult(self.radius)
        self.predict.add(randvec)
def draw():
    global points,vehicles
    #gf.record()
    for v in vehicles:
        worldRecord=100000
        bestPath=None
        if points.points[0].x<points.points[-1].x:    
            if v.location.x>points.points[-1].x:
                v.location.x=points.points[0].x
                v.location.y=points.points[0].y+(v.location.y-points.points[-1].y)
        else:
            if v.location.x<points.points[-1].x:
                v.location.x=points.points[0].x
                v.location.y=points.points[0].y+(v.location.y-points.points[-1].y)
        for i in range(len(points.points)-1):
            a=points.points[i].get()
            b=points.points[i+1].get()
            if points.points[0].x<points.points[-1].x:    
                if b.x<v.location.x:
                    continue
            else:
                if v.location.x<b.x:
                    continue   
            norm=get_normal(a,b,v)
            dist=get_distance(norm.get(),v.location)
            if dist<worldRecord:
                worldRecord=dist
                bestNorm=norm
                bestPath=Pvector(b.x-a.x,b.y-a.y)
        bestPath.setMag(50)
        if worldRecord>50:
            bestNorm.add(bestPath)
            seek=v.seek(bestNorm)
        else:
            bestPath.add(v.location)
            seek=v.seek(bestPath)
        seperate=v.seperate(vehicles)
        seperate.mult(0.8)
        v.applyForce(seek)
        v.applyForce(seperate)
        v.update()
        v.display()
    background(255)
    points.display()
 def create_children(self):
     self.normalize_fitness()
     mating_pool = []
     for i in self.rockets:
         n = int(i.fitness)
         for _ in range(n):
             mating_pool.append(i)
     next_gen = []
     while len(next_gen) < self.pop_max:
         flag = True
         while flag:
             a = np.random.randint(0, len(mating_pool))
             b = np.random.randint(0, len(mating_pool))
             for i in range(len(mating_pool[a].genes.gene)):
                 if mating_pool[a].genes.gene[i] != mating_pool[
                         b].genes.gene[i]:
                     flag = False
                     break
         child = DNA.crossover(mating_pool[a], mating_pool[b])
         child = rocket(location=Pvector(400, 800),
                        target=self.target,
                        genes=child)
         next_gen.append(child)
         self.rockets = next_gen
Exemplo n.º 15
0
 def calculateForce(self,location):
     self.dir=Pvector(location.x,location.y)
     self.dir.sub(self.loc)
     self.dir.mult(150/(self.dir.magnitude()**3))
Exemplo n.º 16
0
 def __init__(self,x=400,y=400):
     self.loc=Pvector(x,y)
     self.dir=None
class vehicle:

    def __init__(self,location=(400,400),velocity=(0,0),acceleration=(0,0),maxspeed=10,maxforce=3,field=None): # add field for field followers
        self.location=Pvector(location[0],location[1])
        self.velocity=Pvector(velocity[0],velocity[1])
        self.acceleration=Pvector(acceleration[0],acceleration[1])
        self.maxspeed=maxspeed
        self.maxforce=maxforce
        self.radius=8
        self.field=field
    def seek(self,target):
        desired=Pvector(target.x,target.y)
        desired.sub(self.location)        
        mag=desired.magnitude()
        if mag<100:
            setmag=mag*self.maxspeed/100
            desired.setMag(setmag)
        else:
            desired.setMag(self.maxspeed)
        desired.sub(self.velocity)
        desired.limit(self.maxforce)
        return desired
    def seperate(self,others):
        sum=Pvector(0,0)
        count=0
        for v in others:
            loc=self.location.get()
            loc.sub(v.location)
            d=loc.magnitude()
            if d>0 and d<50:
                loc.mult(1/d)
                sum.add(loc)
                count=count+1
        if count!=0:    
            sum.mult(1/count)
            sum.norm()
            sum.mult(self.maxspeed)
            sum.sub(self.velocity)
            sum.limit(self.maxforce)
        return sum
    def seek_flow(self):
        if self.location.x<0:
            self.location.x=799
        if self.location.x>799:
            self.location.x=0
        if self.location.y<0:
            self.location.y=799
        if self.location.y>799:
            self.location.y=0
        desired=Pvector(self.lookup(self.location).x,self.lookup(self.location).y)
        desired.setMag(self.maxspeed)
        desired.sub(self.velocity)
        desired.limit(self.maxforce)
        self.applyForce(desired)
    def lookup(self,location):
        k=800/self.field.shape[0]
        return self.field[m.floor(location.x/k)][m.floor(location.y/k)]
    def update(self):
        self.velocity.add(self.acceleration)
        self.velocity.limit(self.maxspeed)
        self.location.add(self.velocity)
        self.acceleration.mult(0)
    def applyForce(self,force):
        self.acceleration.add(force)
    def display(self):
        theta=self.velocity.heading()+m.pi/2
        stroke_weight(1)
        stroke(0)
        fill(0)
        push_matrix()
        translate(self.location.x,self.location.y)
        rotate(theta)
        begin_shape()
        vertex(-self.radius,2*self.radius)
        vertex(0,-2*self.radius)
        vertex(self.radius,2*self.radius)
        end_shape(CLOSE)
        pop_matrix()
Exemplo n.º 18
0
def setup():
    global agent, target
    size(800, 800)
    agent = vehicle()
    target = Pvector(400, 400)
 def seperate(self,others):
     sum=Pvector(0,0)
     count=0
     for v in others:
         loc=self.location.get()
         loc.sub(v.location)
         d=loc.magnitude()
         if d>0 and d<50:
             loc.mult(1/d)
             sum.add(loc)
             count=count+1
     if count!=0:    
         sum.mult(1/count)
         sum.norm()
         sum.mult(self.maxspeed)
         sum.sub(self.velocity)
         sum.limit(self.maxforce)
     return sum
Exemplo n.º 20
0
    def __init__(self, car, radius):

        self.car = car
        self.predict = Pvector(self.car.location.x, self.car.location.y)
        self.radius = radius
Exemplo n.º 21
0
 def generate_flowfield(self):
     fields=np.empty((self.row,self.column),dtype='object')
     if self.type=='random':
         for i in range(self.row):
             for j in range(self.column):
                 theta=uniform(0,2)*np.pi
                 temp=Pvector(np.cos(theta),np.sin(theta))
                 temp.mult(self.magnitude)
                 fields[i][j]=temp
     elif self.type=='horizontal':
         for i in range(self.row):
             for j in range(self.column):
                 theta=0
                 temp=Pvector(np.cos(theta),np.sin(theta))
                 temp.mult(self.magnitude)
                 fields[i][j]=temp
     elif self.type=='perlin':
         for i in range(self.row):
             for j in range(self.column):
                 theta=pnoise2((i+1)/5,(j+1)/5,octaves=6,persistence=0.5,lacunarity=2.0,repeatx=self.row,repeaty=self.column,base=0)
                 theta*=(2*np.pi)
                 temp=Pvector(np.cos(theta),np.sin(theta))
                 temp.mult(self.magnitude)
                 fields[i][j]=temp
     return fields              
Exemplo n.º 22
0
def setup():
    global p
    size(1200, 800)
    p = path(Pvector(1200, 500), Pvector(0, 200))
Exemplo n.º 23
0
 def __init__(self, position):
     self.position = Pvector(position[0], position[1])
Exemplo n.º 24
0
def setup():
    global target#,gif
    size(1200,800)
    target=Pvector(400,400)
 def seek(self,target):
     desired=Pvector(target.x,target.y)
     desired.sub(self.location)        
     mag=desired.magnitude()
     if mag<100:
         setmag=mag*self.maxspeed/100
         desired.setMag(setmag)
     else:
         desired.setMag(self.maxspeed)
     desired.sub(self.velocity)
     desired.limit(self.maxforce)
     return desired