Esempio n. 1
0
    def start(self):
        print "Starting..." 

        gp.global_variable.register("fact1", bool, [True], True)
        gp.global_variable.register("fact2", bool, [True], True)
        gp.global_variable.register("lie", bool, [False], True)
        gp.global_variable.register("dummy", dummy_class, None, True)
        gp.literal.register("Kaljaa!!")
        gp.literal.register("Kebabbia??")
        gp.literal.register("Vodaa...?")
        gp.literal.register("Jaloviinaa?")
        gp.literal.register(1001)        
        gp.literal.register(-50)
        gp.equation.register("<", int)
        gp.equation.register(">", int)
        gp.equation.register("==", int)
        gp.if_statement.register(int);
        gp.command.register("do_stuff", int, dummy_class, [str], False)
        gp.command.register("get_state", int, dummy_class, [int], True)
        gp.command.register("print", None, None, [str], False)
        gp.command.register("print", None, None, [int], False)

        print "Generating code for generation one:"
        generator = gp.code.generate()
        code = generator.to_s()
        print "Code:"
        print "-" * 40
        print code 
        print "-" * 40

        print "Executing code:"
        print "-" * 40
        exec code
        print "-" * 40

        generation = gp.generation()
        generation.add_code(generator)
        generation.get_next_generation()
Esempio n. 2
0
    def start(self):
        print "Starting..."

        gp.global_variable.register("fact1", bool, [True], True)
        gp.global_variable.register("fact2", bool, [True], True)
        gp.global_variable.register("lie", bool, [False], True)
        gp.global_variable.register("dummy", dummy_class, None, True)
        gp.literal.register("Kaljaa!!")
        gp.literal.register("Kebabbia??")
        gp.literal.register("Vodaa...?")
        gp.literal.register("Jaloviinaa?")
        gp.literal.register(1001)
        gp.literal.register(-50)
        gp.equation.register("<", int)
        gp.equation.register(">", int)
        gp.equation.register("==", int)
        gp.if_statement.register(int)
        gp.command.register("do_stuff", int, dummy_class, [str], False)
        gp.command.register("get_state", int, dummy_class, [int], True)
        gp.command.register("print", None, None, [str], False)
        gp.command.register("print", None, None, [int], False)

        print "Generating code for generation one:"
        generator = gp.code.generate()
        code = generator.to_s()
        print "Code:"
        print "-" * 40
        print code
        print "-" * 40

        print "Executing code:"
        print "-" * 40
        exec code
        print "-" * 40

        generation = gp.generation()
        generation.add_code(generator)
        generation.get_next_generation()
Esempio n. 3
0
def main(): 
    intialize_code_generator() 


    generation = gp.generation()
    generation.clear_logs()
    
    """
    TODO: Loading of old best code should be made with serialize/unserialize methods
    
    if (save_load_best_code):
        old_best_file = "best_code.txt"
        if path.exists(old_best_file) and path.isfile(old_best_file) and access(old_best_file, R_OK):
            fileHandle = open (old_best_file, 'r' )
            old_best_code = fileHandle.read()
            fileHandle.close()
            #print "Loaded and executing previous King!"
            #best_score = do_simulation(old_best_code)
            #best_code = old_best_code
        else:
            print "Previous best code file could not be opened"
    """ 
    global stop_after_next_generation

    for i in range(number_of_generations):
        best_score = None
        best_code = None
        
        if (i == 0):      
            #create the first generation
            for h in range(number_of_codes): 
                generation.add_code(gp.code.generate())
        else:
            generation = generation.get_next_generation()
            """
            next_generation = list()
            next_generation.append(best_code)
            #create 50% new codes and rest of the codes with the king :)
            for h in range(number_of_codes / 2):
                next_generation.append(code_generator.generate())
            for h in range(number_of_codes / 2):
                next_generation.append(code_merger.merge(generation[i], best_code))
            generation = next_generation
            """


        #execute generation
        for code in generation.get_codes(): 
            #The game is always the same. Do not execute codes that were moved directly from the
            #previous generation
            if (code.get_moved_unchanged()):
                score = code.get_result()
                print "Directly moved code not executed, score: " + str(score)
            else:
                score = do_simulation(code.to_s())
                code.set_result(score)

            if (best_score == None or score > best_score):
                best_score = score
                best_code = code
        print "=" * 40
        print "Best score in generation " + str(i) + " was: " + str(best_score)
        #print "Best code was"
        print "=" * 40
        #print code.to_s()
        if (stop_after_next_generation):
            break

    
    """
    if (save_load_best_code):
        try:
            fileHandle = open ( 'best_code.txt', 'w' )
            fileHandle.write(best_code)
            fileHandle.close()
        except IOError as e:
            print 'Could not open previous best code file'
    """      
    print "=" * 40
    print "=" * 40
    print best_code.to_s()
    print "=" * 40

    if (show_best_graphics):
        print "showing best code graphically!"
        global use_graphics
        use_graphics = True
        do_simulation(best_code.to_s())
        while loop_best_graphics:
            do_simulation(best_code.to_s())