Esempio n. 1
0
def get_ref():
    reference = []
    for l in GLOB.reference:
        gene = GA.Gene_Info(None)
        gene.set_eval = np.array(l)
        reference.append(gene)
    return reference
Esempio n. 2
0
def new_crossover(CA, DA, cr, mr, dim):
    ratio = GLOB.CA_DA_RATIO
    new_pop = []

    if len(CA + DA) < 2:
        new_pop += GA.initial_genes(dim, GLOB.POP)
        return new_pop
    elif len(CA) < 2 or len(DA) < 2:
        new_pop += GA.crossover(CA + DA, mr=mr)
        return new_pop

    for i in range(GLOB.POP):
        child = None

        # consider crossover rate
        if random.random() < cr:
            p1, p2 = None, None

            # randomly choose parents between CA and DA
            rand_num = random.random()
            if rand_num < ratio * ratio:
                p1, p2 = random.sample(CA, 2)
            elif rand_num < 2 * ratio - ratio * ratio:
                if random.random() < 0.5:
                    p1 = random.sample(CA, 1)[0]
                    p2 = random.sample(DA, 1)[0]
                else:
                    p1 = random.sample(DA, 1)[0]
                    p2 = random.sample(CA, 1)[0]
            else:
                p1, p2 = random.sample(DA, 2)

            child = GA.PMX(p1, p2)
        else:
            seq = random.sample(CA + DA, 1)[0].get_seq().copy()
            child = GA.Gene_Info(seq)

        # mutation
        if random.random() < mr:
            seq = child.get_seq()
            idx1, idx2 = random.sample(list(seq), 2)
            seq[idx2], seq[idx1] = seq[idx1], seq[idx2]

        #check overlap
        overlapped = False
        for parent in CA + DA:
            if (child.get_seq() == parent.get_seq()).all():
                overlapped = True
                break
        if overlapped:  # if overlap, do not add child at new_pop
            continue

        new_pop.append(child)

    return new_pop


#run_TAEA('sed',1,360)
def read_file(SIR_name, version):
    '''
    Read files in Result directory and get data
    return data by each MOEA and trial
    return reference points for the specific SIR program and version

    data: dictionary, key = MOEA value = 2D array.
          first dimension for each trial
          second dimension for data
          ex) dict['NSGA2'] = [[pt1,pt2, ...], [pt100,pt101, ...], ...]
              each pt = [evaluation metric1, metric2, metric3, ...]

    ref_pt: array.
            ex) ref_pt = [pt1, pt2, pt3, ...]
            each pt = [evaluation metric1, metric2, metric3, ...]

    :param SIR_name:  String, SIR program name
    :param version:   int, version of SIR program
    :return:
    '''
    directory = GLOB.RESULT_DIRECTORY + SIR_name + '/'
    data = dict()
    ref_pt = []

    for MOEA in GLOB.TRY_ALGORITHM:
        data_moea = [[] for i in range(GLOB.TRIALS_PER_VERSION)]
        for i in range(GLOB.TRIALS_PER_VERSION):
            fname = 'eval_' + SIR_name + '_version' + str(version) + '_' + MOEA + '_trial' + str(i) + '.csv'
            with open(directory + fname, 'r') as f:
                f.readline()
                rdr = csv.reader(f)
                for line in rdr:
                    for j in range(len(line)):
                        line[j] = float(line[j])
                    data_moea[i].append(np.array(line))
                    gene = GA.Gene_Info(None)
                    gene.set_eval(np.array(line))
                    ref_pt.append(gene)

            ref_pt = GA.get_pareto(ref_pt)[0] #update reference point
        data[MOEA] = data_moea

    for i in range(len(ref_pt)):
        ref_pt[i] = ref_pt[i].get_eval()

    return data, ref_pt