public override T[][] SortMatrix(T[][] matrix) { const int popSize = 200; const float breederRation = 0.2f; const float mutationRate = 0.1f; const int breedersCnt = (int)(popSize * breederRation); var testVectors = VectorUtils <T> .InitVectors(matrix); var pop = PositionPopulationHandler.GeneratePopulation(testVectors, popSize); while (true) { var scoredPop = PositionPopulationHandler.EvaluatePopulation(pop, testVectors); var breeders = PositionPopulationHandler.SelectBreeders(scoredPop, breedersCnt); var bestEntity = breeders.First(); VectorUtils <T> .SetPositionsToVectors(bestEntity, testVectors); if (ErrorTester.GetError(testVectors) == 0) { break; } var newPop = PositionPopulationHandler.CrossBreed(breeders, popSize); PositionPopulationHandler.MutatePop(newPop, mutationRate); pop = newPop; } return(VectorUtils <T> .ConvertVectorsToMatrix(testVectors)); }
public void ConvertVectorsToAutoSizedMatrix() { var vectors = new List <TagVector <string> > { new TagVector <string>(new[] { 0, 0 }, "A"), new TagVector <string>(new[] { 0, 1 }, "A"), new TagVector <string>(new[] { 1, 0 }, "B"), new TagVector <string>(new[] { 1, 1 }, "B") }; var matrix = VectorUtils <string> .ConvertVectorsToMatrix(vectors); Assert.IsTrue(matrix.SelectMany(v => v).ToList().SequenceEqual(vectors.Select(t => t.Tag).ToList())); }
public override T[][] SortMatrix(T[][] matrix) { var tagVectors = VectorUtils <T> .InitVectors(matrix); var temperature = 7f; var error = 0; var stagnation = 0; var rand = new Random(); do { TagVector <T> tagVector1, tagVector2; var previousError = error; error = ErrorTester.GetError(tagVectors); do { tagVector1 = tagVectors[rand.Next() % tagVectors.Count]; tagVector2 = tagVectors[rand.Next() % tagVectors.Count]; } while (!NeighbourTester.AreNeighbours(tagVector1.Pos, tagVector2.Pos)); VectorUtils <T> .SwapVectorPos(tagVector1, tagVector2); var newError = ErrorTester.GetError(tagVectors); if (rand.NextDouble() <= HeuristicUtils.MetropolisCriterion(error, newError, temperature)) { error = newError; } else { VectorUtils <T> .SwapVectorPos(tagVector1, tagVector2); } stagnation = error == previousError ? stagnation + 1 : 0; if (stagnation > 600) { temperature = 7f; } temperature = TemperatureUpdater.UpdateTemperature(temperature, 0.999f); temperature = MathF.Max(temperature, 0.0f); } while (error > 0); return(VectorUtils <T> .ConvertVectorsToMatrix(tagVectors)); }
public override T[][] SortMatrix(T[][] matrix) { var tagVectors = VectorUtils <T> .InitVectors(matrix); var switchCnt = 7000; var error = 0; var rand = new Random(); do { error = ErrorTester.GetError(tagVectors); TagVector <T> tagVector1; TagVector <T> tagVector2; do { tagVector1 = tagVectors[rand.Next() % tagVectors.Count]; tagVector2 = tagVectors[rand.Next() % tagVectors.Count]; } while (!NeighbourTester.AreNeighbours(tagVector1.Pos, tagVector2.Pos)); VectorUtils <T> .SwapVectorPos(tagVector1, tagVector2); var newError = ErrorTester.GetError(tagVectors); if (newError > error) { VectorUtils <T> .SwapVectorPos(tagVector1, tagVector2); } else { error = newError; } switchCnt--; } while (error > 0 && switchCnt >= 0); return(VectorUtils <T> .ConvertVectorsToMatrix(tagVectors)); }