Скачиваний:
0
Добавлен:
26.10.2025
Размер:
133.02 Кб
Скачать

3.2. Корректировка весов персептрона gaEvolutionManager.Cs

namespace PerceptronSymbolsHybrid

{

public class GAEvolutionManager

{

List<GAPerceptronPopulation> P;

public GAEvolutionManager(int ps,int cd,double xmin,double xmax,double mp,double a,double b,string ts,List<TrainingSample>t,double[]bw)

{

populationSize=ps;chromosomeDimension=cd;this.xmin=xmin;this.xmax=xmax;mp=mutationProbability;this.crossoverA=a;this.crossoverB=b;targetSymbol=ts;trainingSet=t;baseWeights=bw;P=new();rnd=new();

}

public GAPerceptronIndividual Evolve(int mg,double ft)

{

Init();var gen=0;var best=P.Last().GetBest();

while(gen<mg && best.Fitness>ft){Next();best=P.Last().GetBest();gen++;}

return best;

}

void Init(){var pop=new GAPerceptronPopulation();for(int i=0;i<populationSize;i++){var genes=baseWeights.Select(w=>(Math.Max(xmin,Math.Min(w+(rnd.NextDouble()-0.5)*0.1,xmax)))).ToList();pop.Individuals.Add(new GAPerceptronIndividual(genes,xmin,xmax,targetSymbol,trainingSet));}P.Add(pop);}

void Next(){var c=P.Last();var n=new GAPerceptronPopulation();while(n.Individuals.Count<c.Individuals.Count){var (p1,p2)=c.SelectPair(rnd);var (c1,c2)=GAPerceptronIndividual.BLXabCrossover(p1,p2,crossoverA,crossoverB,xmin,xmax,rnd);c1.UniformMutation(mutationProbability,rnd);n.Individuals.Add(c1);if(n.Individuals.Count<c.Individuals.Count){c2.UniformMutation(mutationProbability,rnd);n.Individuals.Add(c2);} }P.Add(n);}

}

public class GAPerceptronPopulation{public List<GAPerceptronIndividual>Individuals=new();public (GAPerceptronIndividual,GAPerceptronIndividual)SelectPair(Random r){var s=Individuals.OrderBy(i=>i.Fitness).ToList();var p1=s[r.Next(s.Count)];var p2=s[r.Next(s.Count)];return(p1,p2);}public GAPerceptronIndividual GetBest()=>Individuals.OrderBy(i=>i.Fitness).First();}

}

3.3. Структура обучающего примера TrainingSample.Cs

namespace PerceptronSymbolsHybrid

{

public class TrainingSample

{

public int[] Input; // одномерный массив длины 25 (5x5)

public string Label; // метка символа

public TrainingSample(int[] input, string label)

{

Input = input;

Label = label;

}

}

}

3.4. Вывод информации OutputInformation.Cs

public static class OutputInformation

{

public static void PrintWeights(string symbol, List<double> weights)

{

Console.WriteLine($"Весовая матрица для символа {symbol}:");

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 5; j++)

{

Console.Write($"{weights[i * 5 + j],8:F3}");

}

Console.WriteLine();

}

}

public static void PrintTestResult(int[] inputMatrix, string recognizedSymbol, Dictionary<string, GAPerceptronIndividual> models)

{

Console.WriteLine("\nМатрица ввода:");

PrintMatrix(inputMatrix);

if (recognizedSymbol != "None" && models.ContainsKey(recognizedSymbol))

{

PrintWeights(recognizedSymbol, models[recognizedSymbol].Weights);

}

else

{

}

Console.WriteLine(new string('-', 40));

}

private static void PrintMatrix(int[] matrix)

{

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 5; j++)

{

Console.Write(matrix[i * 5 + j] + " ");

}

Console.WriteLine();

}

Соседние файлы в предмете Методы и модели искусственного интеллекта в управлении техническими системами