Example #1
0
COAL_INDEX = 13  # coal
WASTE_INDEX = 36  # waste

import simworld

# Alias to save some typing
_t = simworld.translate


def PRINT_SEP():
    print '-' * 80  # separator


#
# Load the world for searching
welt = simworld.World()
print 'Loading world...'
welt.load(savefile_path)

fabs_in_welt = [
    welt.get_factory_at(i) for i in range(welt.get_factory_count())
]

assert len(fabs_in_welt) == welt.get_factory_count()


def is_producer_of_good(fab, indx=-1):
    if indx == -1:
        if filter_catg == -1: return len(fab.get_ausgang()) > 0
        return filter_catg in [
            g.get_typ().catg_index for g in fab.get_ausgang()
Example #2
0
    def simulation(self):
        # initial genotypes (clonal but different for each group)
        self.initial_population()
        # for each generation: create world
        for n in range(self.n_gen):
            print("\ngeneration = {}/{}".format(n + 1, self.n_gen))
            world = simworld.World(self.xmax, self.ymax, self.n_walls,
                                   self.n_trees)
            best_e = 0
            best_group = None
            # for each group (same world for all for each group in the same generation)
            for ng in range(self.n_groups):
                print("group: {}/{}".format(ng + 1, self.n_groups))
                # create new agents and introduce them in the simworld
                genotype = self.genotypes[ng]
                world.allocate_agents(self.n_agents, genotype)
                # simulate group lifetime
                for tx in tqdm(range(self.lifetime)):
                    world.update()
                # get and print results
                agents_e = [ag.energy for ag in world.agents]
                av_e = sum(agents_e) / len(world.agents)
                print("agents energy: {}, average = {}".format(agents_e, av_e))
                # save if best
                if av_e > best_e:
                    best_e = av_e
                    best_group = world.agents
                    print("new best = {}".format(best_e))
                # reset world for next group
                world.reset()
            # print best data for past generation
            print("\nbest agents:")
            for ag in best_group:
                print("agent energy = {}".format(ag.energy))
            print("average energy = {}".format(best_e))
            # check and save if better than previous best (all generations)
            if best_e > self.best_e:
                self.best_cases.append([best_e, best_group, world])
                self.best_e = best_e
                print("best over all generations")
                if self.anim_best:
                    simanimation.world_animation(self.lifetime, best_group,
                                                 world)
            # animation for fixed steps
            if self.anim_step:
                if (n + 1) % self.anim_step == 0:
                    simanimation.world_animation(self.lifetime, best_group,
                                                 world)

            # breeding
            best_genotype = best_group[0].genotype
            # copy the identic best genotype for one group
            self.genotypes = [best_genotype]
            for ng in range(self.n_groups - 1):
                # mutation of the nnet only for now
                # thresholds
                ut = best_genotype.ut + np.random.randint(-1,
                                                          2) * self.mut_rate
                lt = best_genotype.lt + np.random.randint(-1,
                                                          2) * self.mut_rate
                vt = best_genotype.vt + np.random.randint(-1,
                                                          2) * self.mut_rate
                # weights
                W = self.mut_weights(best_genotype.W)
                V = self.mut_weights(best_genotype.V)
                # create and save new genotype
                new_genotype = simgenotype.Genotype(ut=ut,
                                                    lt=lt,
                                                    vt=vt,
                                                    W=W,
                                                    V=V)
                self.genotypes.append(new_genotype)