예제 #1
0
    def distance_to_opt(self):
        #magic trick by @MKitlas
        optimal_ph = 1410
        plant_ph = self.phenotype

        if p.location_mode:
            optimal_ph = Environment.default.optimal_phenotype_on_map(self.location)
        else:
            optimal_ph = Environment.default.optimal_phenotype_without_map()
        
        # TODO sum up TEs effect.
        for t in self.aut_transposons_list:
            if p.multidim_changes:
                plant_ph = Phenotype.add(plant_ph, t.mutation_rate)
            else:
                plant_ph[t.trait_no] += t.mutation_rate

        for t in self.nonaut_transposons_list:
            if p.multidim_changes:
                plant_ph = Phenotype.add(self.phenotype, t.mutation_rate)
            else:
                plant_ph[t.trait_no] += t.mutation_rate

        return Phenotype.distance(plant_ph, optimal_ph)
예제 #2
0
class Environment:
    environments = 0
    def __init__(self):
        
        #-----location part
        if parameters.location_mode:
            self.mesh_size = 1./parameters.LD0range/2
            f = lambda size: [[{} for x in xrange(int(size)+1)] for y in xrange(int(size)+1)]
            self.mesh = f(self.mesh_size)
            map_path = parameters.map_phenotype_image(parameters.maps)
            self.load_terrain(map_path+".info.tmp", map_path+".tmp")
        #------------------

        from plant import Plant
        from phenotype import Phenotype

        self.plants = {}
        self.allplantslist = []
        self.generation = 0
        self.__class__.default = self
        self.__class__.environments += 1
        debug.g("niche %d" % parameters.niche_size)
        for i in xrange(parameters.niche_size):
            if parameters.location_mode:
                Plant.new(parameters.get_start_point(parameters.maps))
            else:
                Plant.new((0,0))
        debug.g("*** %d" % len(self.plants))
        self.optimal_global_phenotype = Phenotype()
        self.base_phenotype = Phenotype()
        self.survivors = parameters.niche_size
        self.randomkiller = selectors.KillerRandom()
        (self.killer, self.reproducer) = selectors.getSelectors()
        self.phenotype_link = Phenotype
        self.history = History(self)
        self.history.update()

    def load_terrain(self, info_filename, data_filename):
        size = (0,0)
        with open(info_filename) as f:
            arrinfo = array.array('L')
            arrinfo.fromfile(f, 2)
            size = (arrinfo[0], arrinfo[1])
        self.map_size = size
        self.phenotype_map = [[(1,1,1) for y in xrange(size[1])] for x in xrange(size[0])]

        with open(data_filename) as f:
            scale = lambda x: (float(x-127)/128)*parameters.map_phenotype_amplitude/2

            arr = array.array('B')
            arr.fromfile(f, size[0]*size[1]*3)
            for y in xrange(size[1]):
                for x in xrange(size[0]):
                    self.phenotype_map[x][y] = map(scale, (lambda nr: arr[nr:nr+3])(3*x+3*size[0]*y))
                    #debug.g(size)
                    #for x in xrange(63):
                    #	debug.g(self.phenotype_map[x*10][0])

    def register_new_plant(self, number, plant):
        self.plants[number] = plant
        ##TODO
        if parameters.location_mode:
            try:
                self.mesh[plant.scalex()][plant.scaley()][number] = plant
            except:
                debug.g(plant.scalex())
                debug.g(plant.scaley())
                debug.g(number)
                0/0

    def unregister_plant(self, number):
        ##TODO
        if parameters.location_mode:
            plant = self.plants[number]
            del self.mesh[plant.scalex()][plant.scaley()][number]
        del self.plants[number]

    def advance_generation(self):
        from phenotype import Phenotype
        from plant import Plant
        from math import log, sqrt
        #self.avg_transpositions_in_this_generation = 0

    #if self.generation%10==0:
    #	debug.g("====") 
    #	debug.g(self.optimal_global_phenotype.properties)
    #	debug.g(self.base_phenotype.properties)

        if parameters.random_pressure > 0.0:
        #    for plant in self.plants.values():
        #if happened(random_pressure):
        #    plant.die()
            self.randomkiller.eliminate(self.plants.values())
        self.killer.eliminate(self.plants.values())

        #=========LOCATION
        if parameters.location_mode:
            r = parameters.LD0range
            r2 = r*r

            def important_fields_in_mesh(location):
                f = lambda (x,y): [(x,y),(x-r,y-r),(x,y-r),(x+r,y-r),(x-r,y),(x+r,y),(x-r,y+r),(x,y+r),(x+r,y+r)]
                g = lambda x: -1<x and x<self.mesh_size
                h = lambda (x,y): (Plant.scale(x),Plant.scale(y))
                i = lambda (x,y): g(x) and g(y)
            #debug.g(location)
            #debug.g(r)
                fields = set() 
                for x in filter(i, map(h, f(location))): fields.add(x)
                return fields

            def shuffle(l):
                random.shuffle(l)
                return l
          
            def maybe_neighbour_kill(p1, p2):
                fitness_val = p1.fitness()
            
                d2_fun = lambda ((x1,y1), (x2,y2)): (x2-x1)**2+(y2-y1)**2
                d2 = d2_fun((p1.location, p2.location))
            
            ##range_val = -2*log(d/r)
            #try: range_val = 2*logr-log(d2)
            #except: range_val = 1
                import math
                f = lambda (y,ymax,ymin): (y-ymin)/(ymax-ymin)
                range_val = f((math.e**(d2), math.e**(r2), 1.))
                if range_val <= 1.:
                    return not distributions.happened(range_val * fitness_val)
                else: return False
            #debug.g("%f, %f" %(range_val, math.sqrt(d2)))

            for plant in shuffle(self.plants.values()):
                if not plant.dead:
                    for (x,y) in important_fields_in_mesh(plant.location):
                        if plant.dead: break
                        for killer_plant in (self.mesh[x][y]).values():
                            if maybe_neighbour_kill(plant, killer_plant) and plant.id != killer_plant.id: 
                                plant.die()
                                break
        #=================

        #transpositions_itg = 0
        #for plant in self.plants.values():
        #    transpositions_itg += plant.transpositions
        #self.avg_transpositions_in_this_generation = float(transpositions_itg) / float(len(self.plants.values()))
    
        self.history.update()

        v = self.plants.values()
        self.survivors = len(v)
        self.reproducer.reproduce(self.plants)

        ##KG
#        for plant in self.plants.values():
#            print " >> after reproduction part" + str(plant.aut_transposons) + "==" + str(len(plant.aut_transposons_list))
#            for x in plant.aut_transposons_list : 
#                print "TE #" + str(x.id) + ", parent: " + str(x.parent) + ", aut= " + str(x.is_aut)

        for plant in self.plants.values():            
            plant.evolve()

        self.allplantslist = self.plants.values()

        if parameters.expected_horiz_transfers > 0.0:
            for plant in self.plants.values():
                plant.perform_horizontal_transfers()

        if self.generation >= parameters.stability_period:
            if parameters.is_drift_directed:
                for _unused in range(parameters.number_of_mutations):
                    self.base_phenotype[distributions.runifint(0, parameters.no_phenotype_properties-1)] += parameters.expected_mutation_shift
            else:
                for _unused in range(parameters.number_of_mutations):
                    self.base_phenotype.mutate_once(stdev = parameters.expected_mutation_shift)

        self.optimal_global_phenotype = None
        if parameters.fluctuations_magnitude > 0.0:
            self.optimal_global_phenotype = self.base_phenotype.add(Phenotype.new_random(parameters.fluctuations_magnitude))
        else:
            self.optimal_global_phenotype = self.base_phenotype

        allpl = sorted(self.plants.values(), key = lambda p: p.ord_counter)

        i = 0
        for p in allpl:
            p.ord_counter = i
            i += 1

        #self.history.update()
        self.generation += 1

    def optimal_phenotype_on_map(self, (x,y)):
        scale = lambda (v, length): int(round((length-1)*(v+1)/2))
        xp = scale((x, self.map_size[0]))
        yp = scale((y, self.map_size[1]))
        return self.optimal_global_phenotype.get_map_phenotype(self.phenotype_map[xp][yp])