def __init__(self, confobj): """ """ self._genotype = None self._column_descr = None self._subjects = None self._expanded_data = None self._gene_descr = dict() # my_source = confobj['genetics']['genofile'] my_annot = confobj['genetics']['platform'] my_resources = confobj['genetics']['resources'] if path.exists(path.dirname(my_source)): self._genotype = Genotype(my_source, annot=my_annot, resources=my_resources) else: raise ValueError('incorrect path name: ' + path.dirname(my_source)) # # Now re-concile potential diffs bw # list subjects info bw cfg and genotype data my_subject = set(confobj['genetics']['subjects']) my_subject = my_subject.intersection( set(self._genotype.getOrderedSubsetIndiv())) self._subjects = list(my_subject) self._genotype.setOrderedSubsetIndiv(self._subjects)
def __init__(self, data, inputData=None, chromosomeList=None ): self.lastPrice = 0 self.tradesNum = 0 self.gezahlt = 0 self.fees = 0.01 self.handleData = data self.inputData = inputData print('Agent: '+str(self.inputData)) if chromosomeList is None: self.genotype = Genotype(self.inputData) else: print('Agent chromosomeList:'+chromosomeList) self.genotype = Genotype(chromosomeList) self.alwaysInit()
def selectGenotypes(population, popSize, tour): selectedGenotypes = np.zeros(popSize * 2, dtype=Genotype) iteration = 0 while iteration < 2 * popSize: maxRank = Genotype(None, None, 0) indexes = [] while len(indexes) < tour: i = np.random.randint(0, popSize) if i not in indexes: p = population.population[i] indexes.append(i) if len(indexes) == 1: maxRank = p elif p.rank > maxRank.rank: maxRank = p selectedGenotypes[iteration] = maxRank iteration += 1 return selectedGenotypes
def generatePopulation(population, popSize, cityList, tour, px, pm): result = np.zeros(popSize, dtype=Genotype) selectedGenotypes = selectGenotypes(population, popSize, tour) parentIndex = 0 for i in range(0, popSize): toBreed = np.random.rand() toMutate = np.random.rand() if toBreed > px != 0: result[i] = selectedGenotypes[parentIndex] else: genotype = breed(selectedGenotypes[parentIndex], selectedGenotypes[parentIndex + 1]) result[i] = Genotype(cityList, genotype) if toMutate < pm != 0: mutate(result[i]) parentIndex += 2 i += 1 return Population(None, None, result)
def CreateFirstPopulation(self): for i in range(self.muValue): genotype = Genotype(self.chromoSize, self.bodySize) self.population.append(genotype)
class GenoData(object): """GenoData - Provides object/methods to build/access matrix of geno data for numerical use Available methods cover two/three kinds of use: get_xxx: to get data meta_get_xxx: to get meta information select_xxx: to select columns filter_xxx: unclear if ti really has to (should include subset_maf, etc...) """ def __init__(self, confobj): """ """ self._genotype = None self._column_descr = None self._subjects = None self._expanded_data = None self._gene_descr = dict() # my_source = confobj['genetics']['genofile'] my_annot = confobj['genetics']['platform'] my_resources = confobj['genetics']['resources'] if path.exists(path.dirname(my_source)): self._genotype = Genotype(my_source, annot=my_annot, resources=my_resources) else: raise ValueError('incorrect path name: ' + path.dirname(my_source)) # # Now re-concile potential diffs bw # list subjects info bw cfg and genotype data my_subject = set(confobj['genetics']['subjects']) my_subject = my_subject.intersection( set(self._genotype.getOrderedSubsetIndiv())) self._subjects = list(my_subject) self._genotype.setOrderedSubsetIndiv(self._subjects) def _get_genotype(self): """ """ return self._genotype def select_extract_snp(self, rs): """select_extract_snp : select function to select this speccif list of SNPs return : np.array_uint8 of snp data (0,1,2,128 for na) """ self._expanded_data = self._genotype.snpGenotypeByName(rs) self._column_descr = list(rs) def select_genelist_snp(self, gl): """select_genelist_snp : select function to select snp corresponding to the list of gene return : np.array_uint8 of snp data (0,1,2,128 for na) """ rslist = [] for i in gl: index_in_expand_data = 0 tmplist = self._genotype.getChipAnnot().getRsByGene(i) self._gene_descr[i] = list(index_in_expand_data + np.arange(len(tmplist))) index_in_expand_data += len(tmplist) rslist.extend(tmplist) self._expanded_data = self._genotype.snpGenotypeByName(rslist) self._column_descr = list(rslist) def select_all_snp(self): """select_all_snp : select function to select all available snps return : np.array_uint8 of snp data (0,1,2,128 for na) """ self._expanded_data = self._genotype.snpGenotypeAll() self._column_descr = self._genotype.snpList() def get_data(self): """get_data : return genotype data (0,1,2,128 for na) """ return self._expanded_data def get_col_idx_by_gene(self): """get_col_idx_by_gene: return dict keys are genes and val are lsit of index in the colname data """ return self._gene_descr def get_gene_names(self): """get_gene_names: returnlist of gene name currently selected """ return self._gene_descr.keys() def get_colnames(self): """get_colnames: return list of snp names """ return self._column_descr def get_subjects(self): """get_subjects : return the ordered subject list """ return self._genotype.getOrderedSubsetIndiv() def meta_get_rs_by_gene(self, gene): """meta_get_rs_by_genlist : return rsname list """ return self._genotype.getChipAnnot().getRsByGene(gene) def meta_get_rs_chrom(self, chrom): """meta_get_rs_by_genlist : return rsname list """ print 'to be implemented'
return fitness def main(): f = open('weights', 'w') weights = GA.Run() resetPos() weightsJSON = json.dumps(weights.tolist()) f.write(weightsJSON) emitter.send(weightsJSON) print "Best Weights: ", print weights #while sup.step(timestep) != -1: #pass GA = GA(Genotype(weightNum), fitnessFun) sup = Supervisor() timestep = int(sup.getBasicTimeStep()) emitter = sup.getEmitter("emitter") robot = sup.getFromDef("Robot") rpostion = robot.getField("translation") start_pos = rpostion.getSFVec3f() rrotation = robot.getField("rotation") start_rot = rrotation.getSFRotation() main()
def generatePopulation(popSize, cityList): population = np.zeros(popSize, dtype=Genotype) for i in range(0, popSize): gen = Genotype(cityList) population[i] = gen return population