private void DoInferenceGA(int distance) { bool cicloPais = true; bool AvaliarTodaPopulacao = false; CircuitoChromosome current = null; if (ctControleParent == _ga.PopulationSize) cicloPais = false; if (cicloPais) { //Atribuir os valores medidos para o circuito _ga.GetCurrentChromosome((ctControleParent)).Distance = distance; _ga.GetCurrentChromosome((ctControleParent)).Time = 140; _ga.GetCurrentChromosome((ctControleParent)).GetFitness(); current = _ga.GetCurrentChromosome(ctControleParent); lblNumeroPortasAlteradas.Text = "P" + ctControleParent + " -"; } else if (ctControleClones == 0) { _ga.CalculaNormFit(); //_ga.InitializeClones((ctControleParent - ctPaisDecrescente), sensors); _ga.InitializeClone((ctControleParent - ctPaisDecrescente), sensors); current = _ga.GetCurrentCloneChromosome((ctControleParent - ctPaisDecrescente), 0); } else { _ga.GetCurrentCloneChromosome((ctControleParent - ctPaisDecrescente), (ctControleClones - 1)).Distance = distance; _ga.GetCurrentCloneChromosome((ctControleParent - ctPaisDecrescente), (ctControleClones - 1)).Time = 140; //Mostra o numero de portas alteradas lblNumeroPortasAlteradas.Text = "C" + ctControleClones + " : " + _ga.GetCurrentCloneChromosome((ctControleParent - ctPaisDecrescente), (ctControleClones - 1)).N.ToString(); if ((ctControleClones - 1) < (_ga.Clonepopulation + 1)) current = _ga.GetCurrentCloneChromosome((ctControleParent - ctPaisDecrescente), (ctControleClones - 1)); _ga.InitializeClone((ctControleParent - ctPaisDecrescente), sensors); } if (ctControleParent == _ga.PopulationSize && (cicloPais)) { //ctPaisDecrescente = _ga.PopulationSize; ctPaisDecrescente = _ga.PopulationSize; cicloPais = false; } if ((!cicloPais) && (ctControleClones < (_ga.Clonepopulation + 1))) ctControleClones++; if (ctControleClones > (_ga.Clonepopulation)) { ctPaisDecrescente--; } if (ctControleClones > (_ga.Clonepopulation) && (ctPaisDecrescente == 0)) { AvaliarTodaPopulacao = true; } if (ctControleClones > (_ga.Clonepopulation)) { ctControleClones = 0; } if (cicloPais && (ctControleClones <= _ga.Clonepopulation)) ctControleParent++; if (AvaliarTodaPopulacao) { cicloPais = true; //Fazer o processo de seleção //Manter apenas o melhor individuo (População de Pais) _ga.FindSolution(); //ctPaisDecrescente = _ga.PopulationSize; ctPaisDecrescente = _ga.PopulationSize; int intTemp = Convert.ToInt32(lblGeneration.Text); intTemp++; DataRow drResultado = dtResultados.NewRow(); drResultado["Bestfitness"] = (int)_ga.BestCircuit.Fitness; drResultado["Averagefitness"] = (averagefitness / (_ga.PopulationSize + (_ga.PopulationSize * _ga.Clonepopulation))); drResultado["geracao"] = Convert.ToInt32(lblGeneration.Text.Trim()); dtResultados.Rows.Add(drResultado); lblGeneration.Text = intTemp.ToString(); lblFitness4.Text = _ga.BestCircuit.Fitness.ToString(); bestIndividualLatGeneration = _ga.BestCircuit; current = _ga.BestCircuit; ctControleParent = 0; if (intTemp % 50 == 0) { intTemp = intTemp; } } if (current != null) { //{Cartesian genetic programming”, ShowChromosome(current, lblFitness4); lblGeneration.Text = _ga.Generation.ToString(); AtualizaDirecao(current); } }
/// <summary> /// Mostra o fitness e desenha o quadrado resposta (cromossomo) /// </summary> /// <param name="circuitChromo"></param> /// <param name="lFitness"></param> private void ShowChromosome(CircuitoChromosome circuitChromo, Label lFitness) { if (circuitChromo == null) return; lblGeneration.Text = (_ga.Generation + 1).ToString(); lFitness.Text = circuitChromo.GetFitness().ToString(); lblFitness4.Text = circuitChromo.GetFitness().ToString(); //DrawTable(eqc.Valores); txtGeneAgv.Text = String.Empty; foreach (int i in circuitChromo.Cgp.Genotype) txtGeneAgv.Text = txtGeneAgv.Text + i + ";"; //Remove o ultimo ';' txtGeneAgv.Text = txtGeneAgv.Text.Remove(txtGeneAgv.Text.Length - 1); //DataRow drResultado = dtResultados.NewRow(); //drResultado["Bestfitness"] = -1; //drResultado["Averagefitness"] = (int)circuitChromo.Fitness; //drResultado["geracao"] = Convert.ToInt32(lblGeneration.Text.Trim()); //dtResultados.Rows.Add(drResultado); averagefitness += (int)circuitChromo.Fitness; }
public void AtualizaDirecao(CircuitoChromosome chromoRobo) { double NewAngle = -1; _virou = false; if ((!chromoRobo.OutputBits[0].Output && !chromoRobo.OutputBits[1].Output) || (chromoRobo.OutputBits[0].Output && chromoRobo.OutputBits[1].Output)) pbMotor1.Visible = pbMotor2.Visible = true; else { pbMotor1.Visible = pbMotor2.Visible = false; } if ((chromoRobo.OutputBits[0].Output == false && chromoRobo.OutputBits[1].Output == false) || (chromoRobo.OutputBits[0].Output == true && chromoRobo.OutputBits[1].Output == true)) { NewAngle = 0; _traz = false; } //vira esquerda else if (chromoRobo.OutputBits[0].Output == true && chromoRobo.OutputBits[1].Output == false) { pbMotor1.Visible = true; double vlTmp = Convert.ToDouble(lblCtViraEsq.Text); vlTmp++; lblCtViraEsq.Text = vlTmp.ToString(); //NewAngle = -7; NewAngle = -15; _virou = true; } else if (chromoRobo.OutputBits[0].Output == false && chromoRobo.OutputBits[1].Output == true) { pbMotor2.Visible = true; double vlTmp = Convert.ToDouble(lblCtViraDir.Text); vlTmp++; lblCtViraDir.Text = vlTmp.ToString(); //NewAngle = +7; NewAngle = +15; _virou = true; } Angle += NewAngle; }
// Moving the AGV private void MoveAGV() { bestIndividualLatGeneration = null; //Passado 140 milisegundos, coletar a distancia percorrida pelo robo if (_bCiclo) { _ponto1 = true; p2 = new Point(pbRobot.Left - pbTerrain.Left + pbRobot.Width / 2, pbRobot.Top - pbTerrain.Top + pbRobot.Height / 2); _bCiclo = false; //Passa a distancia ao controle para avaliação do mesmo DoInferenceGA(GetDistance(p1, p2)); } Speed = Convert.ToDouble(txtSpeed.Text); if (_traz)//Se for p/ traz; { if (Speed > 0) Speed = Speed * -1; txtSpeed.Text = Speed.ToString(); } else { //Volta para velocidade positiva if (Speed < 0) Speed = Speed * -1; txtSpeed.Text = Speed.ToString(); } double rad = ((Angle + 90) * Math.PI) / 180; int Offset = 0; int Inc = (-1 * Convert.ToInt32(Speed / 5)); Offset += Inc; int IncX = Convert.ToInt32(Offset * Math.Cos(rad)); int IncY = Convert.ToInt32(Offset * Math.Sin(rad)); if (_traz) { pbRobot.Top = (int)(pbRobot.Top - IncY); pbRobot.Left = (int)(pbRobot.Left - IncX); } else { pbRobot.Top = (int)(pbRobot.Top + IncY); pbRobot.Left = (int)(pbRobot.Left + IncX); } if (_ponto1) { p1 = new Point(pbRobot.Left - pbTerrain.Left + pbRobot.Width / 2, pbRobot.Top - pbTerrain.Top + pbRobot.Height / 2); _ponto1 = false; } // Leaving the track if (cbTrajeto.Checked) { Graphics g = Graphics.FromImage(OriginalMap); Point p1 = new Point(pbRobot.Left - pbTerrain.Left + pbRobot.Width / 2, pbRobot.Top - pbTerrain.Top + pbRobot.Height / 2); Point p2 = new Point(p1.X + IncX, p1.Y + IncY); g.DrawLine(new Pen(new SolidBrush(Color.Blue)), p1, p2); g.DrawImage(OriginalMap, 0, 0); g.Dispose(); } }