コード例 #1
0
func (r SimplexCoupleFormation) CoupleFormation(generation *b.Generation, board *b.BlackBoard) {
	selRuleta := generation.Selection
	var padres []c.Parents
	var part2 []int
	var tam int = len(selRuleta) / 2

	for i := 0; i < tam; i++ {
		padres = append(padres, c.Parents{Parent1: selRuleta[i]})
		part2 = append(part2, selRuleta[i+tam])
	}

	fmt.Println("Selector", selRuleta)
	fmt.Println("padres", padres)
	fmt.Println("part2", part2)

	for i := 0; i < tam; i++ {
		var rdm int = rand.Intn(len(part2))

		p := c.Parents{
			Parent1: padres[i].Parent1,
			Parent2: part2[rdm],
		}
		var bol bool = true

		for _, pa := range padres {
			if pa.Parent1 == p.Parent1 &&
				pa.Parent2 == p.Parent2 {
				if len(part2) == 1 {
					bol = true
					break
				} else {
					i--
					bol = false
					break
				}
			}
		}
		if bol {
			padres[i].Parent2 = part2[rdm]
			fmt.Println("part2", part2)
			fmt.Println("rdm", rdm)
			part2 = append(part2[:rdm], part2[rdm+1:]...)
			fmt.Println("part2", part2)
		}
	}
	generation.ParentsLst = make([]c.Parents, len(padres))
	fmt.Println("part2", padres)
	copy(generation.ParentsLst, padres)

}
コード例 #2
0
ファイル: Roulette.go プロジェクト: martinre2/Evolution
func (r Roulette) Roulette(generation *b.Generation, board *b.BlackBoard) bool {

	//fmt.Println("Gen", generation)
	var ln int = len(generation.Conformations)
	var totalFitness int = 0
	for i := 0; i < ln; i++ {
		totalFitness += generation.Conformations[i].Fitness
	}

	fmt.Println("TotFit", totalFitness)

	totalFitness *= -1

	if totalFitness == 0 {
		return true
	}

	var seleccion []int

	if board.Params.OtherTechs[0] == 1 {
		var dest c.Conformations
		dest = make([]c.Conformation, len(generation.Conformations))
		copy(dest, generation.Conformations)

		sort.Sort(dest)
		//fmt.Println("____>>dest", dest[0])
		fmt.Println("____>>dest0", dest[0])
		fmt.Println("____>>dest1", dest[1])
		board.AddBest(dest[0])
		board.AddBest(dest[1])

		//fmt.Println("BOARD", board.Bests)

		dest = dest[:0]

		for len(seleccion) < ln-2 {
			valor := rand.Float32()
			valor *= float32(totalFitness)
			var partialSum float32 = 0

			for i := 0; i < ln; i++ {
				if partialSum <= valor {
					partialSum += float32(generation.Conformations[i].Fitness * -1)
				} else {
					if r.indexOf(seleccion, i-1) == r.lastIndexOf(seleccion, i-1) {
						seleccion = append(seleccion, i-1)
						break
					}
				}
			}
		}

		fmt.Println("____>>SElECT", seleccion)
	} else if board.Params.OtherTechs[0] == 2 {
		/*ArrayList<Conformation> dest = (ArrayList<Conformation>) generation.getConformations().clone();
		board.Params.ordenar(dest);
		board.Params.addBest(dest.get(0));
		dest.clear();
		dest = null;
		while (seleccion.size() < ln - 1) {
			double valor = rand.nextDouble();
			valor *= totalFitness;
			double partialSum = 0;
			for (int i = 0; i < ln; i++) {
				if (partialSum <= valor) {
					partialSum += generation.getConformations().get(i).getFitness() * -1;
				} else {
					if (seleccion.indexOf(i - 1) == seleccion.lastIndexOf(i - 1)) {
						seleccion.add(i - 1);
						break;
					}
				}
			}
		}*/
	} else {
		/*while (seleccion.size() < ln) {
			double valor = rand.nextDouble();
			valor *= totalFitness;
			double partialSum = 0;
			for (int i = 0; i < ln; i++) {
				if (partialSum <= valor) {
					partialSum += generation.getConformations().get(i).getFitness() * -1;
				} else {
					if (seleccion.indexOf(i - 1) == seleccion.lastIndexOf(i - 1)) {
						seleccion.add(i - 1);
						break;
					}
				}
			}
		}*/
	}
	sort.Ints(seleccion)
	generation.Selection = make([]int, len(seleccion))
	copy(generation.Selection, seleccion)
	fmt.Println("____>>SELECTION", generation.Selection)
	return false
}