Beispiel #1
0
def test_create_wait_children():
    #TO RUN, MUST SEND 2 IN FOR THE NUM_INDIVIDUALS PARAMETER. NO OTHER PARAMETER MATTERS.
    ind = individual.Ind(size=5,
                         genes={
                             "initial-number-sheep": 100,
                             "initial-number-wolves": 25,
                             "sheep-gain-from-food": 5,
                             "sheep-reproduce": 10,
                             "wolf-reproduce": 20
                         },
                         fitness=0)
    individual.write_file_NetLogo(ind, "0test.txt")
    ind = individual.Ind(size=5,
                         genes={
                             "initial-number-sheep": 50,
                             "initial-number-wolves": 5,
                             "sheep-gain-from-food": 5,
                             "sheep-reproduce": 10,
                             "wolf-reproduce": 20
                         },
                         fitness=0)
    individual.write_file_NetLogo(ind, "1test.txt")

    create_wait_children("test.txt")
    print("Experiments complete.", NUM_INDIVIDUALS,
          "modelresults files should exist.")
Beispiel #2
0
def test_fitness_calculation():
    ind = individual.Ind(size=5,
                         genes={
                             "initial-number-sheep": 100,
                             "initial-number-wolves": 25,
                             "sheep-gain-from-food": 5,
                             "sheep-reproduce": 10,
                             "wolf-reproduce": 20
                         },
                         fitness=0)
    individual.write_file_NetLogo(ind, "0test.txt")
    #Fork and run the process
    pid = os.fork()
    if pid < 0:
        print("fork failed for process ", i)
        sys.exit(-1)
    # child process
    elif pid == 0:
        print("Child process is now starting")
        child_process(0, "test.txt")

    else:  # if process was started, wait for it
        status = os.waitpid(pid, 0)
        print("status: ", status)

    #calculate fitness
    ind = individual.compute_fitness(ind, "modelresults.txt")
    print("Calculated fitness is ", ind.fitness)
Beispiel #3
0
def write_input_files(population):
    # name each file based on the number of the individual
    filename_common = "inputfile.txt"
    # for every individual, write a file with parameters and other relevant starting information for NetLogo
    # need index of population array to match the filename for later calculation of fitness
    for index in range(len(population)):
        filename = PREFIX + str(index) + filename_common
        individual.write_file_NetLogo(population[index], filename)
    return filename_common
Beispiel #4
0
def test_NetLogo_Run():
    ind = individual.Ind(size=5,
                         genes={
                             "initial-number-sheep": 100,
                             "initial-number-wolves": 25,
                             "sheep-gain-from-food": 5,
                             "sheep-reproduce": 10,
                             "wolf-reproduce": 20
                         },
                         fitness=0)
    individual.write_file_NetLogo(ind, "0test.txt")
    child_process(0, "test.txt")
Beispiel #5
0
def test_file_write():
    print("--------\nTesting writing to file for model input...")
    individual_1 = individual.Ind(size=5,
                                  genes={
                                      "a": 1,
                                      "b": 2,
                                      "c": 3,
                                      "d": 4,
                                      "e": 5
                                  },
                                  fitness=0)
    individual.write_file_NetLogo(individual_1, "test_file_output.txt")

    fd = open("test_file_output.txt", "r")
    file_genes = {}
    for line in fd:
        split = line.split()
        if split[0] != "set":
            print("ERROR: set is missing at start of line")
        file_genes[split[1]] = int(split[2])
    fd.close()
    if len(individual_1.genes.keys()) != len(file_genes.keys()):
        print(
            "number of variables from individual do not match number of variables in file"
        )
    elif not ("a" in file_genes.keys() and "b" in file_genes.keys()
              and "c" in file_genes.keys() and "d" in file_genes.keys()
              and "e" in file_genes.keys()):
        print("one or more variables is missing in the file")
        print("variables in individual: ", individual_1.genes.keys())
        print("variables in file: ", file_genes.keys())
    elif individual_1.genes["a"] != file_genes["a"] or individual_1.genes["b"] != file_genes["b"] \
        or individual_1.genes["c"] != file_genes["c"] or individual_1.genes["d"] != file_genes["d"] \
        or individual_1.genes["e"] != file_genes["e"]:
        print("values in the file do not match values from individual")
        print("values in individual: ", individual_1.genes)
        print("values in file: ", file_genes)
    else:
        print("File writing is correct")