def rod_swap_crossover(assembly_one_lattice, assembly_two_lattice,
                       assembly_two_rod_list, assembly_one_rod_list,
                       region_map):
    #Swaps a rod between the two assemblies. It doesn't account to make sure that the rod is not in common with the current assembly.
    chromosome_one = random.choice(assembly_one_rod_list)
    chromosome_two = chromosome_one
    while (chromosome_one == chromosome_two):
        chromosome_two = random.choice(assembly_two_rod_list)
    rod_count = 0
    for rod in region_map:
        if (rod == 1):
            if (assembly_one_lattice[rod_count] == chromosome_one):
                assembly_one_lattice[rod_count] = chromosome_two
            if (assembly_two_lattice[rod_count] == chromosome_two):
                assembly_two_lattice[rod_count] = chromosome_one
        rod_count += 1
    assembly_one_rod_list.remove(chromosome_one)
    assembly_one_rod_list.append(chromosome_two)
    assembly_two_rod_list.remove(chromosome_two)
    assembly_two_rod_list.append(chromosome_one)
    string_assembly_one_lattice = convert_to_string(assembly_one_lattice)
    string_assembly_two_lattice = convert_to_string(assembly_two_lattice)
    lattice_one_pin_map = metrics.return_pin_map(string_assembly_one_lattice)
    lattice_two_pin_map = metrics.return_pin_map(string_assembly_two_lattice)
    stowage = open('child_list.txt', 'a')
    stowage.write('Assembly\n')
    stowage.write(lattice_one_pin_map)
    stowage.write('What_Happened:  Rod_Swap_Crossover\n\n')
    stowage.write('Assembly\n')
    stowage.write(lattice_two_pin_map)
    stowage.write('What_Happened:  Rod_Swap_Crossover\n\n')
    stowage.close()
def double_mutation(lattice, rod_list, region_map, pin_list):
    # A function to alter two rod locations within the lattice. Does not introduce any new rod types.
    suitable_swap_locations = []
    for rod in xrange(len(region_map)):
        if (region_map[rod] == 1): suitable_swap_locations.append(rod)
    x1 = random.choice(suitable_swap_locations)
    x2 = random.choice(suitable_swap_locations)
    original_pin_one = lattice[x1]
    original_pin_two = lattice[x2]
    foo_one = original_pin_one
    foo_two = original_pin_two
    while (foo_one == original_pin_one):
        foo_one = random.randint(0, len(pin_list[1]) - 1)
    while (foo_two == original_pin_two):
        foo_two = random.randint(0, len(pin_list[1]) - 1)
    new_lattice = range(len(lattice))
    for i in xrange(len(lattice)):
        if (i == x1): new_lattice[i] = str(foo_one)
        elif (i == x2): new_lattice[i] = str(foo_two)
        else: new_lattice[i] = lattice[i]
    string_new_lattice = convert_to_string(new_lattice)
    pin_map = metrics.return_pin_map(string_new_lattice)
    stowage = open('child_list.txt', 'a')
    stowage.write('Assembly\n')
    stowage.write(pin_map)
    stowage.write('What_Happened:  Double_Mutation\n\n')
    stowage.close()
def single_mutation(lattice, rod_list, region_map, pin_list):
    #A function to alter a single rod location within the lattice. Does not introduce new rod types to the lattice.
    #I don't know why I didn't make this one so that it accepts the lattice. list. I think it would be
    #best to make an organized mutation function similar to what I did for crossover.
    suitable_swap_locations = []
    for rod in xrange(len(region_map)):
        if (region_map[rod] == 1): suitable_swap_locations.append(rod)
    x = random.choice(suitable_swap_locations)
    original_pin = lattice[x]
    foo = original_pin
    while (foo == original_pin):
        foo = random.randint(0, len(pin_list[1]) - 1)
    new_lattice = range(len(lattice))
    for i in xrange(len(lattice)):
        if (i == x): new_lattice[i] = str(foo)
        else: new_lattice[i] = lattice[i]
    string_new_lattice = convert_to_string(new_lattice)
    pin_map = metrics.return_pin_map(string_new_lattice)
    stowage = open('child_list.txt', 'a')
    stowage.write('Assembly\n')
    stowage.write(pin_map)
    stowage.write('What_Happened:  Single_Mutation\n\n')
    stowage.close()
def crossover(full_lattice_list, region_map, pin_list):
    remaining_assemblies = full_lattice_list[:]
    number_of_pairs = len(remaining_assemblies) / 2
    for foo in xrange(number_of_pairs):
        current_assembly = remaining_assemblies[0][:]
        current_rod_list = return_active_rod_list(current_assembly, region_map)
        mate_lattice_number = 0
        mate_score = 0
        for assembly in range(1, len(remaining_assemblies)):
            matching_rod_count = 0
            current_examine_list = return_active_rod_list(
                remaining_assemblies[assembly], region_map)
            if (len(current_examine_list) > len(current_rod_list)):
                shorter_list = len(current_rod_list)
            else:
                shorter_list = len(current_examine_list)
            for rod in xrange(shorter_list):
                if (current_rod_list[rod] == current_examine_list[rod]):
                    matching_rod_count += 1
            if (matching_rod_count > mate_score):
                mate_score = matching_rod_count
                mate_lattice_number = assembly
                mate_rod_list = current_examine_list[:]
        mate_lattice = remaining_assemblies[mate_lattice_number]
        suitable_swap_locations = []
        rod_number = 0
        for rod in xrange(len(current_assembly)):
            if (mate_lattice[rod] == current_assembly[rod]):
                pass
            else:
                suitable_swap_locations.append(rod)
        if (len(suitable_swap_locations) < 4):
            double_mutation(current_assembly, current_rod_list, region_map,
                            pin_list)
            double_mutation(mate_lattice, mate_rod_list, region_map, pin_list)
        else:
            number_swaps = len(suitable_swap_locations) / 2
            actual_swap_spots = []
            for i in xrange(number_swaps):
                x = random.choice(suitable_swap_locations)
                actual_swap_spots.append(x)
            new_assembly_one = range(len(current_assembly))
            new_assembly_two = range(len(mate_lattice))
            for i in xrange(len(current_assembly)):
                not_swap_location = True
                for foo in actual_swap_spots:
                    if (foo == i):
                        not_swap_location = False
                if (not_swap_location == True):
                    new_assembly_one[i] = current_assembly[i]
                    new_assembly_two[i] = mate_lattice[i]
                else:
                    new_assembly_one[i] = mate_lattice[i]
                    new_assembly_two[i] = current_assembly[i]
            string_new_assembly_one = convert_to_string(new_assembly_one)
            string_new_assembly_two = convert_to_string(new_assembly_two)
            lattice_one_pin_map = metrics.return_pin_map(
                string_new_assembly_one)
            lattice_two_pin_map = metrics.return_pin_map(
                string_new_assembly_two)
            stowage = open('child_list.txt', 'a')
            stowage.write('Assembly\n')
            stowage.write(lattice_one_pin_map)
            stowage.write('What_Happened:  Pin_Swap_Crossover\n\n')
            stowage.write('Assembly\n')
            stowage.write(lattice_two_pin_map)
            stowage.write('What_Happened:  Pin_Swap_Crossover\n\n')
            stowage.close()
        if (current_assembly in remaining_assemblies):
            remaining_assemblies.remove(current_assembly)
        if (mate_lattice in remaining_assemblies):
            remaining_assemblies.remove(mate_lattice)
def roulette(previous_generation, current_generation):

    #A function for determining which assemblies survive to breed in the next generation.
    #Uses a roulette method to select the survivors. All assemblies are drawn into single rank list. Then
    #ranks are randomly picked until the population list is full.
    #previous_generation is the name of the previous generation survivor list.

    #rent_file = open(previous_generation, 'r')
    parent_lines = parent_file.readlines()
    parent_file.close()
    child_file = open(current_generation, 'r')
    child_lines = child_file.readlines()
    child_file.close()
    lattice_list = []
    lattice_count = 0
    cost_list = []
    rod_list = []
    what_happened_list = []
    line_count = 1000
    for line in parent_lines:
        elems = line.strip().split()
        if (len(elems) > 0):
            if (elems[0] == 'Assembly'):
                line_count = 0
                lattice_count += 1
                temp_lattice = []
            if (line_count > 0 and line_count < 11):
                for el in elems:
                    temp_lattice.append(el)
            elif (line_count == 11):
                lattice_list.append(temp_lattice)
                what_happened_list.append(elems[1])
            elif (line_count == 12):
                cost_list.append(float(elems[1]))
        line_count += 1

    line_count = 1000
    for line in child_lines:
        elems = line.strip().split()
        if (len(elems) > 0):
            if (elems[0] == 'Assembly'):
                line_count = 0
                lattice_count += 1
                temp_lattice = []
            if (line_count > 0 and line_count < 11):
                for el in elems:
                    temp_lattice.append(el)
            elif (line_count == 11):
                lattice_list.append(temp_lattice)
                what_happened_list.append(elems[1])
            elif (line_count == 12):
                cost_list.append(float(elems[1]))
        line_count += 1

    average_cost = metrics.calculate_average_cost(cost_list)
    variance = metrics.calculate_cost_variance(cost_list, average_cost)
    (min_cost, max_cost, best_lattice, worst_lattice, best_what_happened,
     worst_what_happened) = metrics.return_max_and_min(cost_list, lattice_list,
                                                       what_happened_list)
    pin_average = metrics.return_average_pins_used(lattice_list)
    metric_file = open('metrics_file.txt', 'a')

    metric_file.write('Average Lattice Cost:   ' + str(average_cost) + '\n')
    metric_file.write('Variance in lattice score:    ' + str(variance) + '\n')
    metric_file.write('Minimum lattice score: ' + str(min_cost))
    metric_file.write('  Maximum lattice score:  ' + str(max_cost) + '\n')
    metric_file.write('Average number of pins per assembly:  ' +
                      str(pin_average))
    metric_file.write('Highest scoring Pin Lattice \n' + best_lattice)
    metric_file.write('Worst Scoring Pin Lattice \n' + worst_lattice +
                      '\n\n\n')

    print('Average Lattice Cost:   ' + str(average_cost) + '\n')
    print('Variance in lattice score:    ' + str(variance) + '\n')
    print('Minimum lattice score: ' + str(min_cost))
    print('  Maximum lattice score:  ' + str(max_cost) + '\n')
    print('Average number of pins per assembly:  ' + str(pin_average))
    print('Highest scoring Pin Lattice \n' + best_lattice)
    print('Worst Scoring Pin Lattice \n' + worst_lattice + '\n\n\n')

    metric_file.close()

    new_parent = open('parent_list.txt', 'w')
    number_of_rounds = lattice_count / 2
    rank_list = 0
    cost_sum = 0
    for cost in cost_list:
        cost_sum += cost

    rank_sum = 0
    for cost in cost_list:
        temp_rank = (cost / cost_sum) + rank_sum
        rank_list.append(temp_rank)
        rank_sum = temp_rank

    for i in xrange(number_of_rounds):
        temp = random.random()
        match_cost_not_found = True
        assembly_count = 0
        for rank in rank_list:
            if (match_cost_not_found == True):
                if (rank > temp):
                    assembly_number = assembly_count
                    match_cost_not_found = False
            assembly_count += 1

        new_parent.write('Assembly\n')
        pin_map = metrics.return_pin_map(lattice_list[assembly_number])
        new_parent.write(pin_map)
        new_parent.write('What_Happened: ' +
                         what_happened_list[assembly_number] + '\n')
        new_parent.write('Lattice_Cost:  ' + str(cost_list[assembly_number]) +
                         '\n\n')
        new_parent.close()
def tournament(previous_generation, current_generation):
    #A function for determining which assemblies survive to breed in the next generation.
    #Two assemblies are randomly drawn from the total list of assemblies.
    #The assembly with the highest cost survives to the next generation while the lower
    #cost assembly is killed.
    #previous_generation is the name of the previous generation survivor list.
    parent_file = open(previous_generation, 'r')
    parent_lines = parent_file.readlines()
    parent_file.close()
    child_file = open(current_generation, 'r')
    child_lines = child_file.readlines()
    child_file.close()
    lattice_list = []
    lattice_count = 0
    cost_list = []
    rod_list = []
    what_happened_list = []
    line_count = 1000
    for line in parent_lines:
        elems = line.strip().split()
        if (len(elems) > 0):
            if (elems[0] == 'Assembly'):
                line_count = 0
                lattice_count += 1
                temp_lattice = []
            if (line_count > 0 and line_count < 11):
                for el in elems:
                    temp_lattice.append(el)
            elif (line_count == 11):
                lattice_list.append(temp_lattice)
                what_happened_list.append(elems[1])
            elif (line_count == 12):
                cost_list.append(float(elems[1]))
        line_count += 1

    line_count = 1000
    for line in child_lines:
        elems = line.strip().split()
        if (len(elems) > 0):
            if (elems[0] == 'Assembly'):
                line_count = 0
                lattice_count += 1
                temp_lattice = []
            if (line_count > 0 and line_count < 11):
                for el in elems:
                    temp_lattice.append(el)
            elif (line_count == 11):
                lattice_list.append(temp_lattice)
                what_happened_list.append(elems[1])
            elif (line_count == 12):
                cost_list.append(float(elems[1]))
        line_count += 1

    average_cost = metrics.calculate_average_cost(cost_list)
    variance = metrics.calculate_cost_variance(cost_list, average_cost)
    (min_cost, max_cost, best_lattice, worst_lattice, best_what_happened,
     worst_what_happened) = metrics.return_max_and_min(cost_list, lattice_list,
                                                       what_happened_list)
    pin_average = metrics.return_average_pins_used(lattice_list)
    metric_file = open('metrics_file.txt', 'a')

    metric_file.write('Average Lattice Cost:   ' + str(average_cost) + '\n')
    metric_file.write('Variance in lattice score:    ' + str(variance) + '\n')
    metric_file.write('Minimum lattice score: ' + str(min_cost))
    metric_file.write('  Maximum lattice score:  ' + str(max_cost) + '\n')
    metric_file.write('Average number of pins per assembly:  ' +
                      str(pin_average))
    metric_file.write('Highest scoring Pin Lattice \n' + best_lattice)
    metric_file.write('Worst Scoring Pin Lattice \n' + worst_lattice +
                      '\n\n\n')

    print('Average Lattice Cost:   ' + str(average_cost) + '\n')
    print('Variance in lattice score:    ' + str(variance) + '\n')
    print('Minimum lattice score: ' + str(min_cost))
    print('  Maximum lattice score:  ' + str(max_cost) + '\n')
    print('Average number of pins per assembly:  ' + str(pin_average))
    print('Highest scoring Pin Lattice \n' + best_lattice)
    print('Worst Scoring Pin Lattice \n' + worst_lattice + '\n\n\n')

    metric_file.close()

    new_parent = open('parent_list.txt', 'w')
    number_of_rounds = lattice_count / 2
    for i in xrange(number_of_rounds):
        foo_one = random.randint(0, len(lattice_list) - 1)
        foo_two = foo_one
        while (foo_one == foo_two):
            foo_two = random.randint(0, len(lattice_list) - 1)
        contestant_one_lattice = lattice_list[foo_one]
        contestant_two_lattice = lattice_list[foo_two]
        contestant_one_cost = cost_list[foo_one]
        contestant_two_cost = cost_list[foo_two]
        contestant_one_what_happened = what_happened_list[foo_one]
        contestant_two_what_happened = what_happened_list[foo_two]

        if (contestant_one_cost > contestant_two_cost):
            new_parent.write('Assembly\n')
            pin_map = metrics.return_pin_map(contestant_one_lattice)
            new_parent.write(pin_map)
            new_parent.write('What_Happened: ' + contestant_one_what_happened +
                             '\n')
            new_parent.write('Lattice_Cost:  ' + str(contestant_one_cost) +
                             '\n\n')
        else:
            new_parent.write('Assembly\n')
            pin_map = metrics.return_pin_map(contestant_two_lattice)
            new_parent.write(pin_map)
            new_parent.write('What_Happened: ' + contestant_two_what_happened +
                             '\n')
            new_parent.write('Lattice_Cost:  ' + str(contestant_two_cost) +
                             '\n\n')
        lattice_list.remove(contestant_one_lattice)
        lattice_list.remove(contestant_two_lattice)
        cost_list.remove(contestant_one_cost)
        cost_list.remove(contestant_two_cost)
        what_happened_list.remove(contestant_one_what_happened)
        what_happened_list.remove(contestant_two_what_happened)
def pin_swap_crossover(current_assembly, mate_lattice, current_rod_list,
                       mate_rod_list):
    #Swaps Rod Locations between two assemblies. Only rods that already exist within the bundle may be switched.
    initial_lattice_rods = [0]
    for rod in current_assembly:
        doesnt_exist = True
        for foo in initial_lattice_rods:
            if (rod == foo):
                doesnt_exist = False
        if (doesnt_exist == True):
            initial_lattice_rods.append(rod)
    in_common_rod_list = [0]
    for rod in mate_lattice:
        for foo in initial_lattice_rods:
            if (rod == foo):
                doesnt_exist = True
                for thing in in_common_rod_list:
                    if (thing == rod):
                        doesnt_exist = False
                if (doesnt_exist == True):
                    in_common_rod_list.append(rod)

    rod_number = 0
    suitable_swap_locations = []
    for rod in current_assembly:
        for foo in in_common_rod_list:
            if (rod == foo):
                for footoo in in_common_rod_list:
                    if (mate_lattice[rod_number] == footoo):
                        if (mate_lattice[rod_number] == rod):
                            pass
                        else:
                            suitable_swap_locations.append(rod_number)
        rod_number += 1
    number_swaps = len(suitable_swap_locations) / 2
    actual_swap_spots = []
    for i in xrange(number_swaps):
        x = random.choice(suitable_swap_locations)
        actual_swap_spots.append(x)
    new_assembly_one = range(len(current_assembly))
    new_assembly_two = range(len(mate_lattice))
    for i in xrange(len(current_assembly)):
        not_swap_location = True
        for foo in actual_swap_spots:
            if (foo == i):
                not_swap_location = False
        if (not_swap_location == True):
            new_assembly_one[i] = current_assembly[i]
            new_assembly_two[i] = mate_lattice[i]
        else:
            new_assembly_one[i] = mate_lattice[i]
            new_assembly_two[i] = current_assembly[i]
    lattice_one_pin_map = metrics.return_pin_map(new_assembly_one)
    lattice_two_pin_map = metrics.return_pin_map(new_assembly_two)
    assembly_one_rod_list = []
    assembly_two_rod_list = []
    for rod in new_assembly_one:
        if (rod == '0'):
            pass
        else:
            rod_not_used = True
            for foo in assembly_one_rod_list:
                if (foo == rod):
                    rod_not_used = False
            if (rod_not_used == True):
                assembly_one_rod_list.append(rod)
    for rod in new_assembly_two:
        if (rod == '0'):
            pass
        else:
            rod_not_used = True
            for foo in assembly_two_rod_list:
                if (foo == rod):
                    rod_not_used = False
            if (rod_not_used == True):
                assembly_two_rod_list.append(rod)
    stowage = open('child_list.txt', 'a')
    stowage.write('Assembly\n')
    stowage.write(lattice_one_pin_map)
    stowage.write('What_Happened:  Pin_Swap_Crossover\n\n')
    stowage.write('Assembly\n')
    stowage.write(lattice_two_pin_map)
    stowage.write('What_Happened:  Pin_Swap_Crossover\n\n')
    stowage.close()
Exemplo n.º 8
0
number_batches = population / batch_size
#number_batches = 5
#population = number_batches*batch_size
final_generation = number_of_generations - 1  #not sure this line of code does anything
########################################################################
#This batch of code writes the initial Casmo input files for the population.
parent = open('parent_list.txt', 'w+')
for batch in xrange(number_batches):
    for people in xrange(batch_size):
        lattice_list = []
        (temp_lattice, temp_rod_list) = input_data.make_input_lattices()
        temp_str_map = []
        for t in temp_lattice:
            temp_str_map.append(str(t))
        lattice_list.append(temp_str_map)
        temp_rod_map = metrics.return_pin_map(temp_str_map)
        parent.write('Assembly\n')
        parent.write(temp_rod_map)
        parent.write('What Happened: Original_file\n\n')

        #    print temp_lattice
        (lower_lattice, upper_lattice) = reproduction.decode_rod_lattice(
            temp_lattice, input_data.region_map, input_data.rod_list)
        casmo = open(
            'batch_' + str(batch) + '.lower_assembly.' + str(people + 1) +
            '.inp', 'w')
        pin_map = metrics.return_pin_map(lower_lattice)
        line_number = 1
        for line in lower_casmo_lines:
            if (line_number == lower_skip):
                casmo.write(pin_map)
Exemplo n.º 9
0
  next_gen_file.write('final_generation       '+str(final_generation)+'\n')
  next_gen_file.write('lower_skip             '+str(lower_skip)+'\n')
  next_gen_file.write('upper_skip             '+str(upper_skip)+'\n')
  next_gen_file.close()


casmo_functions.evaluate_bundles(batch_size,generation,population)
reproduction.merge_files('cost_list.txt','child_list.txt')
reproduction.tournament('parent_list.txt','child_list.txt')
lattice_list = reproduction.extract_lattices('parent_list.txt')
lattice_count = 0
for batch in xrange(number_batches):
  for people in xrange(batch_size):
    (lower_lattice,upper_lattice) = reproduction.decode_rod_lattice(new_lattice_list[lattice_count], input_data.region_map, input_data.rod_list)
    casmo = open('final_batch_'+str(batch)+'.upper_assembly.'+str(people+1)+'.inp','w')
    pin_map = metrics.return_pin_map(upper_lattice)
    line_number = 1
    for line in upper_casmo_lines:
      if(line_number == lower_skip):
        casmo.write(pin_map)
      elif(line_number > lower_skip and line_number < upper_skip):
        pass
      else: casmo.write(line)
      line_number+=1
    casmo.close()
    casmo = open('final_batch_'+str(batch)+'.lower_assembly.'+str(people+1)+'.inp','w')
    for line in lower_casmo_lines:
      if(line_number == lower_skip):
        casmo.write(pin_map)
      elif(line_number > lower_skip and line_number < upper_skip):
        pass
Exemplo n.º 10
0
casmo.close()

number_batches = population / batch_size

new_lattice_list = reproduction.extract_lattices('parent_list.txt')
lattice_count = 0
batch_number = 0
for lattice in new_lattice_list:
    (lower_lattice,
     upper_lattice) = reproduction.decode_rod_lattice(lattice,
                                                      input_data.region_map,
                                                      input_data.rod_list)
    casmo = open(
        'batch_' + str(batch_number) + '.lower_assembly.' +
        str(lattice_count + 1) + '.inp', 'w')
    pin_map = metrics.return_pin_map(lower_lattice)
    line_number = 1
    for line in lower_casmo_lines:
        if (line_number == lower_skip):
            casmo.write(pin_map)
        elif (line_number > lower_skip and line_number <= upper_skip):
            pass
        else:
            casmo.write(line)
        line_number += 1
    casmo.close()
    lattice_count += 1
    if (lattice_count == batch_size):
        lattice_count = 0
        batch_number += 1