Ejemplo n.º 1
0
def genotype_extractor():
    destination = address + 'chromloc.txt'
    ext_positions = np.genfromtxt(destination)
    ext_chromosomes = [{'z': z, 'I': 1, 'r': 0.25} for z in ext_positions]
    ext_genotype = gen.chrom2geno(ext_chromosomes)
    loop_number = len(ext_genotype)
    return ext_genotype
Ejemplo n.º 2
0
    def bestRealisticize(self):

        self.order(
        )  # Maybe unnecessary but I'm too lazy to check if it's redundant

        chromosomes = self.individuals[0].chromosomes

        mill_precision = myconst.mill_precision
        wire_width = myconst.wire_width
        wall_width = myconst.wall_width
        max_stack = myconst.max_stack
        r_min = myconst.r_min

        self.realistic_chromosomes = gen.chromRealisticize(
            chromosomes, mill_precision, wire_width, wall_width, max_stack,
            r_min)
        self.realistic_genotype = gen.chrom2geno(self.realistic_chromosomes)
        self.realistic_fitness = mag.fitness_function(self.realistic_genotype,
                                                      None)
Ejemplo n.º 3
0
 def genotype_update(self):
     """
     Takes chromosomes and converts to genotype.
     """
     #self.chromosomes    = gen.chromRealisticize(self.chromosomes, Coil.wire_width, self.mill_precision)
     self.genotype = gen.chrom2geno(self.chromosomes)
Ejemplo n.º 4
0
class Coil:
    """
    Coil object. Contains coil's genotype and fitness.

    Constructed so that the chromosomes define the behaviour of all update
    functions.

    ##FIXME: All perfect coils should have loops at std_radius. Otherwise stack at r_min.
    """

    ### Instance Attributes:
    ## Evolutionary traits:
    genotype = []  # All values
    chromosomes = []  # Just positive values
    field = []
    ppm = []
    fitness = None

    ### Class Attributes:
    ## Mutation Scheme
    MutationScheme = myconst.MutationScheme

    ## Range of coil:
    z_min = myconst.z_min
    z_max = myconst.z_max

    ## Range of current:
    I_min = myconst.I_min
    I_max = myconst.I_max

    ## Range of radii:
    std_radius = myconst.r_0
    r_min = myconst.r_min
    r_max = myconst.r_max

    ## Realism Parameters:
    div_width = myconst.div_width
    gap_precision = myconst.gap_precision
    mill_precision = myconst.mill_precision  # The
    wire_width = myconst.wire_width
    wall_width = myconst.wall_width

    ## Range of Fitness Calculation:
    calc_z_min = myconst.calc_z_min
    calc_z_max = myconst.calc_z_max
    calc_points = myconst.calc_points
    zlist = np.linspace(calc_z_min, calc_z_max,
                        calc_points)  # Calculation points
    field_points = zlist

    walk_limit = myconst.walk_limit
    div_width = myconst.div_width

    ## Initial values
    loop_number = myconst.loop_number
    radius = myconst.r_0
    epsilon = myconst.epsilon
    I_epsilon = myconst.I_epsilon
    r_epsilon = myconst.r_epsilon
    # wire_width      = myconst.wire_width
    gap_precision = myconst.gap_precision

    chromosomes = gen.random_ChromGen(loop_number, z_min, z_max, I_min, I_max,
                                      r_min, r_max)
    genotype = gen.chrom2geno(chromosomes)

    # gen.lw_ChromGen(genotype, radius)
    # gen.sol_ChromGen(genotype, loop_z_max, radius)
    # gen.lwb_ChromGen(genoptype, radius)
    # gen.hh_ChromGen(genotype, radius)
    # gen.gap_ChromGen(genotype, precision, radius)

    ## Standards:
    # Building Solenoid:
    sol_chromosomes = np.array(gen.sol_ChromGen(genotype, z_max, std_radius))
    sol_genotype = np.array(gen.chrom2geno(sol_chromosomes))
    sol_homogeneity = mag.fitness_function(sol_genotype, field_points)

    # Building Lee-Whiting:
    lw_chromosomes = np.array(gen.lw_ChromGen(genotype, std_radius))
    lw_genotype = np.array(gen.chrom2geno(lw_chromosomes))
    lw_homogeneity = mag.fitness_function(lw_genotype, field_points)

    # Building 9/4 Lee-Whiting:
    lwb_chromosomes = np.array(gen.lwb_ChromGen(genotype, std_radius))
    lwb_genotype = np.array(gen.chrom2geno(lwb_chromosomes))
    lwb_homogeneity = mag.fitness_function(lwb_genotype, field_points)

    # Building Helmholtz:
    hh_chromosomes = np.array(gen.hh_ChromGen(genotype, std_radius))
    hh_genotype = np.array(gen.chrom2geno(hh_chromosomes))
    hh_homogeneity = mag.fitness_function(hh_genotype, field_points)

    # Building Gapped Solenoid:
    gap_chromosomes = np.array(
        gen.gap_ChromGen(genotype, std_radius, z_max, mill_precision))
    gap_genotype = np.array(gen.chrom2geno(gap_chromosomes))
    gap_homogeneity = mag.fitness_function(gap_genotype, field_points)

    def __init__(self):
        """Coil initialization. Creates random genotype and sets fitness to 0."""

        self.chromosomes_init()
        self.genotype_update()
        self.fitness_update()
        self.field_update()

    def manual_chromosomes_update(self, chromosomes):
        """Manually sets the chromosomes for an individual."""

        self.chromosomes = chromosomes
        self.genotype_update()
        self.fitness_update()
        self.field_update()

    def chromosomes_init(self):
        """
        Genotype initialization. Randomly chooses loop positions along z-axis.
        Positions act as individual chromosomes.

        ## NOTE: Currently note actually generating loops at different radii. The range is [r_min, r_min]
        """
        self.chromosomes = gen.random_ChromGen(Coil.loop_number, Coil.z_min,
                                               Coil.z_max, Coil.I_min,
                                               Coil.I_max, Coil.r_min,
                                               Coil.r_max)

    def genotype_update(self):
        """
        Takes chromosomes and converts to genotype.
        """
        #self.chromosomes    = gen.chromRealisticize(self.chromosomes, Coil.wire_width, self.mill_precision)
        self.genotype = gen.chrom2geno(self.chromosomes)

    def fitness_update(self):
        """Updates fitness of self"""
        self.fitness = mag.fitness_function(self.genotype, Coil.zlist)

    def field_update(self):
        """Updates the fields along the axis."""
        args = [self.genotype, Coil.zlist]

        self.field = mag.field_along_axis(*args)
        self.ppm = mag.ppm_field(*args)

    def mutate(self):
        """
        Mutates chromosomes randomly with probability m.

        Note: Currently mutates ALL chromosomes, maybe change later.
        FIXME: Mutation probability must be handled externally.
        """
        mutated_positions, mutated_currents, mutated_radii = gen.chrom_mutate(
            self.chromosomes, Coil.epsilon, Coil.I_epsilon, Coil.r_epsilon)

        args = [
            mutated_positions, mutated_currents, mutated_radii, Coil.z_min,
            Coil.z_max, Coil.I_min, Coil.I_max, Coil.r_min, Coil.r_max
        ]

        if (Coil.MutationScheme == 'PILEUP'):
            mutated_chromosomes = gen.pileupCorrect(*args)

        elif (Coil.MutationScheme == 'REFLECTION'):
            mutated_chromosomes = gen.reflectCorrect(*args)

        elif (Coil.MutationScheme == 'REDRAW'):
            mutated_chromosomes = gen.redrawCorrect(*args)

        elif (Coil.MutationScheme == 'SPREADOUT'):
            pass

        else:
            print('INVALID MUTATION SCHEME')
            exit()

        # Assigning changes to instance:
        self.chromosomes = gen.coil_order(mutated_chromosomes)
        self.genotype_update()
Ejemplo n.º 5
0
import Modules.genetix as gen
import Modules.magnetix as mag
import Modules.myconstants as myconst

import matplotlib.pyplot as plt
import numpy as np

show_points_number = 1000  # Number of points on +ve z axis (double this to get total)
show_points = np.linspace(0, 1.0, show_points_number)
loop_number = 378

radius = myconst.radius

# init_genotype= Population.initial_best.genotype
lw_genotype = gen.chrom2geno(gen.lw_perfectChromGen(loop_number,
                                                    radius=radius))

# init_error= mag.ppm_field(init_genotype, show_points, a=radius)
lw_error = mag.ppm_field(lw_genotype, show_points, a=radius)

lw_y = lw_error[:, 1]

x = lw_error[:, 0]

plt.plot(x, lw_y, label='Lee-Whiting')

plt.title('PPM Error Field of L-W Coils')
plt.xlabel('z [m]')
plt.ylabel('PPM Error')
plt.legend()
plt.grid()
Ejemplo n.º 6
0
def field_plot(address, genotype):
    loop_number = len(genotype)
    # loop_z_max = genotype[-1]['z']
    # r_min      = sorted(genotype, key=lambda k: k['r'])[0]['r']
    r_0 = myconst.r_0
    r_min = myconst.r_min
    r_max = myconst.r_max
    mill_precision = myconst.mill_precision
    loop_z_max = myconst.z_max

    ext_genotype = genotype

    # init_genotype= Population.initial_best.genotype
    # lw_genotype  = gen.chrom2geno(gen.lw_ChromGen(loop_number,radius=r_min))
    # lwb_genotype = gen.chrom2geno(gen.lwb_ChromGen(loop_number,radius=r_min))
    # hh_genotype  = gen.chrom2geno(gen.hh_ChromGen(loop_number,radius=r_min))
    # sol_genotype = gen.chrom2geno(gen.sol_ChromGen(loop_number,loop_z_max=loop_z_max, radius=r_min))
    # gap_genotype = gen.chrom2geno(gen.gap_ChromGen(r_min, loop_z_max, loop_number, gap_precision))
    lw_genotype = gen.chrom2geno(gen.lw_ChromGen(genotype, r_0))
    lwb_genotype = gen.chrom2geno(gen.lwb_ChromGen(genotype, r_0))
    hh_genotype = gen.chrom2geno(gen.hh_ChromGen(genotype, r_0))
    sol_genotype = gen.chrom2geno(
        gen.sol_ChromGen(genotype, loop_z_max=loop_z_max, std_radius=r_0))
    gap_genotype = gen.chrom2geno(
        gen.gap_ChromGen(genotype,
                         r_0,
                         length=loop_z_max,
                         precision=mill_precision))

    # init_field  = mag.field_along_axis(init_genotype,show_points, a=radius)
    ext_field = mag.field_along_axis(ext_genotype, show_points)
    lw_field = mag.field_along_axis(lw_genotype, show_points)
    lwb_field = mag.field_along_axis(lwb_genotype, show_points)
    hh_field = mag.field_along_axis(hh_genotype, show_points)
    sol_field = mag.field_along_axis(sol_genotype, show_points)
    gap_field = mag.field_along_axis(gap_genotype, show_points)

    ext_y = ext_field[:, 1]
    lw_y = lw_field[:, 1]
    lwb_y = lw_field[:, 1]
    hh_y = hh_field[:, 1]
    sol_y = sol_field[:, 1]
    gap_y = gap_field[:, 1]
    # init_y = init_field[:,1]

    x = lw_field[:, 0]

    plt.plot(x, lw_y, label='Lee-Whiting')
    plt.plot(x, lwb_y, label='9/4 Lee-Whiting')
    plt.plot(x, hh_y, label='Helmholtz')
    plt.plot(x, ext_y, label='External')
    plt.plot(x, sol_y, label='Solenoid')
    plt.plot(x, gap_y, label='Gapped Solenoid')
    # plt.plot(x, init_y, label = 'Initial')

    plt.title('Magnetic Field Strengths of Competing Coils')
    plt.xlabel('z [m]')
    plt.ylabel('|B| [$\\mu$T]')
    plt.legend()

    plt.grid()
    plt.savefig(address + '/Fields.png', dpi=400)
    plt.show()
    plt.clf()
    plt.close()

    return
Ejemplo n.º 7
0
def err_zoom_plot(address, genotype):
    r_0 = myconst.r_0
    mill_precision = myconst.mill_precision
    loop_z_max = myconst.z_max

    loop_number = len(genotype)
    # loop_z_max = genotype[-1]['z']
    r_min = sorted(genotype, key=lambda k: k['r'])[0]['r']

    ext_genotype = genotype
    # init_genotype= Population.initial_best.genotype
    # lw_genotype  = gen.chrom2geno(gen.lw_ChromGen(loop_number,radius=r_min))
    # lwb_genotype = gen.chrom2geno(gen.lwb_ChromGen(loop_number, r_min))
    # hh_genotype  = gen.chrom2geno(gen.hh_ChromGen(loop_number,radius=r_min))
    # sol_genotype = gen.chrom2geno(gen.sol_ChromGen(loop_number,loop_z_max=loop_z_max, radius=r_min))
    # gap_genotype = gen.chrom2geno(gen.gap_ChromGen(r_min, loop_z_max, loop_number, gap_precision))
    lw_genotype = gen.chrom2geno(gen.lw_ChromGen(genotype, r_0))
    lwb_genotype = gen.chrom2geno(gen.lwb_ChromGen(genotype, r_0))
    hh_genotype = gen.chrom2geno(gen.hh_ChromGen(genotype, r_0))
    sol_genotype = gen.chrom2geno(
        gen.sol_ChromGen(genotype, loop_z_max=loop_z_max, std_radius=r_0))
    gap_genotype = gen.chrom2geno(
        gen.gap_ChromGen(genotype,
                         r_0,
                         length=loop_z_max,
                         precision=mill_precision))

    # init_error= mag.ppm_field(init_genotype, show_points, a=radius)
    lw_error = mag.ppm_field(lw_genotype, show_points)
    lwb_error = mag.ppm_field(lwb_genotype, show_points)
    hh_error = mag.ppm_field(hh_genotype, show_points)
    ext_error = mag.ppm_field(ext_genotype, show_points)
    sol_error = mag.ppm_field(sol_genotype, show_points)
    gap_error = mag.ppm_field(gap_genotype, show_points)

    # init_y= init_error[:,1]
    lw_y = lw_error[:, 1]
    lwb_y = lwb_error[:, 1]
    hh_y = hh_error[:, 1]
    ext_y = ext_error[:, 1]
    sol_y = sol_error[:, 1]
    gap_y = gap_error[:, 1]

    y_max = max(ext_y)
    x_max = 0.6

    x = lw_error[:, 0]

    plt.plot(x, lw_y, label='Lee-Whiting')
    plt.plot(x, lwb_y, label='9/4 Lee-Whiting')
    plt.plot(x, hh_y, label='Helmholtz')
    plt.plot(x, ext_y, label='External')
    plt.plot(x, sol_y, label='Solenoid')
    plt.plot(x, gap_y, label='Gapped Solenoid')
    # plt.plot(x, init_y, label = 'Initial')

    plt.ylim(-y_max, y_max)
    plt.xlim(-x_max, x_max)

    plt.title('PPM Error Fields of Competing Coils (zoomed)')
    plt.xlabel('z [m]')
    plt.ylabel('PPM Error')
    plt.legend()
    plt.grid()

    plt.savefig(address + '/PPM_zoom.png', dpi=600)

    plt.show()
    plt.clf()
    plt.close()

    return