Exemplo n.º 1
0
Arquivo: evolve.py Projeto: jtcb/flock
    def random_plan(self):
        p = Plan()

        # Choose start condition AND INSTANTIATE IT:
        start_condition = random.choice(p.Conditionals)()
        start_condition.height = 1

        p.action = start_condition
        p.this_conditionals.append(start_condition)


        done = False
        while not done:
            done = True

            # Update conditionals if not marked (marked = already dealt with)
            # and stop when hitting the max depth
            for c in p.this_conditionals:
                if c.marked == 0:
                    done = False
                    c.marked = 1

                    # Add a random expression:
                    expression = random.choice(p.Expressions)()
                    expression.obs1 = random.choice(p.Observations)
                    expression.obs2 = random.choice(p.Observations)
                    expression.depth = c.depth + 1
                    expression.marked = 1
                    expression.set_map(p)
                    c.expression = expression

                    #  Must add terminal actions if we hit the max depth (minus 2)
                    if c.depth == self.max-2:
                        for key in c.options.keys():
                            axn = random.choice(p.Actions)
                            axn.depth = c.depth + 1
                            c.options[key] = axn

                    # Otherwise, we can add either a terminal action OR another conditional:
                    else:
                        for key in c.options.keys():
                            condition_or_action = random.random()
                            if condition_or_action < .5:
                                new_c = random.choice(p.Conditionals)()
                                new_c.depth = c.depth + 1
                                c.options[key] = new_c
                                p.this_conditionals.append(new_c)
                            else:
                                c.options[key] = random.choice(p.Actions)

        return p