func (r RandomGenerationStage) Execute(board *b.BlackBoard) bool { numConf := board.Params.NumConformations //lattType := board.Params.LatticeType if numConf%2 != 0 { return false } grdm := gn.NewGenCubePoints(board) board.Generations = append(board.Generations, grdm.Generate(numConf)) return false }
func (mc MakeCubeChildren) MakeChildren(gen b.Generation, board *b.BlackBoard) b.Generation { mc.board = board var length int = len(board.Hplevel.HpString) var numConf int = len(gen.ParentsLst) fmt.Println("MC>", length, numConf) var conf []c.Conformation var corte []int cOver := NewCrossOver(board) for i := 0; i < numConf; i++ { var tempP c.Parents if board.Params.CrossOverOp == 0 { fmt.Println("OnePoint") corte = cOver.OnePointCrossOver(length) } else if board.Params.CrossOverOp == 1 { fmt.Println("twoPoint") corte = cOver.TwoPointCrossOver(length) } else if board.Params.CrossOverOp == 2 { fmt.Println("UniformPoint") corte = cOver.UniformPointCrossOver(length) } var padre1 int = 0 var padre2 int = 0 if board.Params.MutationOp == 0 { padre1 = gen.ParentsLst[i].Parent1 padre2 = gen.ParentsLst[i].Parent2 tempP = gen.ParentsLst[i] } else { fmt.Println("Mutation OP !=0") switch rand.Intn(2) { case 0: fmt.Println("MUT 0") padre1 = gen.ParentsLst[i].Parent1 padre2 = gen.ParentsLst[i].Parent2 tempP = gen.ParentsLst[i] break case 1: fmt.Println("MUT 1") padre1 = gen.ParentsLst[i].Parent2 padre2 = gen.ParentsLst[i].Parent1 tempP.Parent1 = padre1 tempP.Parent2 = padre2 break default: fmt.Println("MakeCubeChildrens") break } } var puntosPadre1 []c.Point puntosPadre1 = make([]c.Point, len(gen.Conformations[padre1].Conformations)) copy(puntosPadre1, gen.Conformations[padre1].Conformations) var puntosPadre2 []c.Point puntosPadre2 = make([]c.Point, len(gen.Conformations[padre2].Conformations)) copy(puntosPadre2, gen.Conformations[padre2].Conformations) var nuevoHijo1 []c.Point var nuevoHijo2 []c.Point for m := 0; m < length; m++ { puntosPadre1[m].ResetR() puntosPadre2[m].ResetR() } var puntosHijo_C []c.Point fP := c.Point{ XValue: 0.0, YValue: 0.0, ZValue: 0.0, MovVectValue: 0, Letter: puntosPadre1[0].Letter, Way0: true, Way1: true, Way2: true, Way3: true, Way4: true, Way5: true, } nuevoHijo1 = append(nuevoHijo1, fP) if board.Params.CrossOverOp == 0 { for j := 1; j < length; j++ { fmt.Println(" It j", j) if j < corte[0] { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo1, j) } else { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo1, j) } } var puntosHijo_C []c.Point c2 := c.Point{ XValue: 0.0, YValue: 0.0, ZValue: 0.0, MovVectValue: 0, Letter: puntosPadre2[0].Letter, Way0: true, Way1: true, Way2: true, Way3: true, Way4: true, Way5: true, } nuevoHijo2 = append(nuevoHijo2, c2) for j := 1; j < length; j++ { if j < corte[0] { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo2, j) } else { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo2, j) } } } else if board.Params.CrossOverOp == 1 { for j := 1; j < length; j++ { if j < corte[0] { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo1, j) } else if j < corte[1] { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo1, j) } else { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo1, j) } } var puntosHijo_C []c.Point c2 := c.Point{ XValue: 0.0, YValue: 0.0, ZValue: 0.0, MovVectValue: 0, Letter: puntosPadre2[0].Letter, Way0: true, Way1: true, Way2: true, Way3: true, Way4: true, Way5: true, } nuevoHijo2 = append(nuevoHijo2, c2) for j := 1; j < length; j++ { if j < corte[0] { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo2, j) } else if j < corte[1] { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo2, j) } else { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo2, j) } } } else if board.Params.CrossOverOp == 2 { for j := 1; j < length; j++ { if corte[j] == 0 { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo1, j) } else { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo1, j) } } var puntosHijo_C []c.Point c2 := c.Point{ XValue: 0.0, YValue: 0.0, ZValue: 0.0, MovVectValue: 0, Letter: puntosPadre2[0].Letter, Way0: true, Way1: true, Way2: true, Way3: true, Way4: true, Way5: true, } nuevoHijo2 = append(nuevoHijo2, c2) for j := 1; j < length; j++ { if corte[j] == 0 { j = mc.check(puntosHijo_C, puntosPadre2[j].MovVectValue, &nuevoHijo2, j) } else { j = mc.check(puntosHijo_C, puntosPadre1[j].MovVectValue, &nuevoHijo2, j) } } } hijo1 := c.Conformation{ Conformations: nuevoHijo1, Fitness: f.NewFitness(nuevoHijo1).GetFitness(), Parents: tempP, } hijo1.Init() conf = append(conf, hijo1) hijo2 := c.Conformation{ Conformations: nuevoHijo2, Fitness: f.NewFitness(nuevoHijo2).GetFitness(), Parents: tempP, } hijo2.Init() conf = append(conf, hijo2) } for _, b := range board.Bests { conf = append(conf, b) } board.Bests = board.Bests[:0] rGn := b.Generation{ Conformations: conf, DmaxP: board.GenDmaxP(conf), RadioGiroP: board.GenRadioGiroP(conf), } return rGn }
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 }
func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") r.ParseForm() fmt.Println(r.Form) fmt.Println(r.FormValue("hpseq")) l, _ := strconv.Atoi(r.FormValue("lattice")) c, _ := strconv.Atoi(r.FormValue("numberconf")) run, _ := strconv.Atoi(r.FormValue("run")) pcx, _ := strconv.ParseFloat(r.FormValue("crossprob"), 32) experiments, _ := strconv.Atoi(r.FormValue("manyexp")) crossop, _ := strconv.Atoi(r.FormValue("crossover")) fmt.Println("Params", l, c, run) var bestBoard *b.BlackBoard var board *b.BlackBoard //var ex_randomStage s.RandomGenerationStage //var ex_simpleStage s.SimpleGenerationStage params := b.BlackBoardParameters{ LatticeType: l, NumConformations: c, NumGenerations: run, NumExperiments: 1, OtherTechs: []int{1, 0, 0}, CrossOverProb: float32(pcx), CrossOverOp: crossop, MutationOp: 1, } bestBoard = b.NewBoard(params) for i := 0; i < experiments; i++ { board := b.NewBoard(params) board.Hplevel.HpString = r.FormValue("hpseq") ex_randomStage := new(s.RandomGenerationStage).Execute(board) ex_simpleStage := new(s.SimpleGenerationStage).Execute(board) _ = ex_randomStage _ = ex_simpleStage var bestFit int = 0 for _, c := range board.Generations[len(board.Generations)-1].Conformations { if c.Fitness < bestFit { bestFit = c.Fitness } } board.BestFitness = bestFit if board.BestFitness < bestBoard.BestFitness { bestBoard = board } } for i := 0; i < experiments; i++ { fmt.Println(">>>i", i) } _ = bestBoard _ = board fmt.Println("<End Simulation") var export []*ExportConf var rs ExportResponse for _, c := range bestBoard.Generations[len(bestBoard.Generations)-1].Conformations { //for _, c := range board.Generations[0].Conformations { e := new(ExportConf) e.Fitness = c.Fitness e.Radiogiro = c.RadioGiroRCDM e.Dmax = c.Dmax for _, p := range c.Conformations { iP := new(Points) iP.X = p.XValue iP.Y = p.YValue iP.Z = p.ZValue e.Proteins = append(e.Proteins, iP) } export = append(export, e) } //Stuff for charts var ch Charts //Fitness Chart //Build Series sum := make([]int, len(bestBoard.Generations)) average := make([]float32, len(bestBoard.Generations)) best := make([]int, len(bestBoard.Generations)) worts := make([]int, len(bestBoard.Generations)) gene := make([]int, len(bestBoard.Generations)) //Best for i, g := range bestBoard.Generations { gene[i] = i totalFit, totalBestFit, totalWortsFit := 0, 0, -100 for _, c := range g.Conformations { totalFit += c.Fitness //totalBestFit = (totalBestFit > c.Fitness) ? (c.Fitness) : (totalBestFit) if totalBestFit > c.Fitness { totalBestFit = c.Fitness } else { totalBestFit = totalBestFit } //totalWortsFit = (totalWortsFit < c.Fitness) ? (c.Fitness) : (totalWortsFit) if totalWortsFit < c.Fitness { totalWortsFit = c.Fitness } else { totalWortsFit = totalWortsFit } } sum[i] = totalFit average[i] = float32(totalFit) / float32(len(g.Conformations)) best[i] = totalBestFit worts[i] = totalWortsFit } //Radio Chart //Build Series radgiroP := make([]float64, len(bestBoard.Generations)) dmaxP := make([]float64, len(bestBoard.Generations)) for i, g := range bestBoard.Generations { radgiroP[i] = g.RadioGiroP dmaxP[i] = g.DmaxP } //end radio chart //Conformation Fitness //Build Serie conf := make([]int, len(bestBoard.Generations[len(bestBoard.Generations)-1].Conformations)) fitness := make([]int, len(bestBoard.Generations[len(bestBoard.Generations)-1].Conformations)) for i, c := range bestBoard.Generations[len(bestBoard.Generations)-1].Conformations { conf[i] = i + 1 fitness[i] = c.Fitness } //end chart ch.Sum = sum ch.Average = average ch.Best = best ch.Worts = worts ch.Gene = gene ch.RadioGiroP = radgiroP ch.DmaxP = dmaxP ch.Fitness = fitness ch.Conformation = conf fmt.Println(export) rs.ConfPlot = export //rs.Generations = bestBoard.Generations rs.Chart = ch //js, err := json.Marshal(export) js, err := json.Marshal(rs) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(js) //fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) //fmt.Println("___>Peticion") }) log.Fatal(http.ListenAndServe(":4005", nil)) }