static void Main(string[] args) { const int maxEFOS = 1500; while (true) { try { var myService = new ServiceClient(); var myDistributedTask = myService.GetTask(); myService.Close(); if (myDistributedTask is null) { return; // There is no tasks to be processed } Console.WriteLine("\nAleatorio CON LSGEN Tarea en proceso:"); Console.WriteLine(myDistributedTask.Problem + "-" + myDistributedTask.Seed + "-" + myDistributedTask.Algorithm); Algorithm theAlgorithm = null; switch (myDistributedTask.Algorithm) { case "HSOS": theAlgorithm = new HSOS() { MaxEFOs = maxEFOS }; break; case "SBHS": theAlgorithm = new SBHS() { MaxEFOs = maxEFOS }; break; } var the_problem = new PMediana(myDistributedTask.Problem + ".txt"); var myRandom = new Random(myDistributedTask.Seed); Console.WriteLine("\nEjecutando algoritmo..... "); theAlgorithm.Ejecutar(the_problem, myRandom); myDistributedTask.Result_Best = theAlgorithm.Best.Fitness; Console.WriteLine("\nResultado: " + theAlgorithm.Best.Fitness); myDistributedTask.Status = "S"; var miServicio2 = new ServiceClient(); Console.WriteLine("\nGuardando resultados....."); miServicio2.SaveResults(myDistributedTask); // miServicio2.Close(); } catch (Exception e1) { Console.WriteLine(e1.Message); } } }
public abstract void Ejecutar(PMediana theProblem, Random myRandom);
public override void Ejecutar(PMediana theProblem, Random myRandom) { MyProblem = theProblem; MyRandom = myRandom; //Ajuste de los parametros double HMCR = 0.95; EFOs = 0; //Inicializar la Poblacion //Population = InitializeFixedPopulation(PopulationSize); Population = InitializeControlledPopulation(PopulationSize); Population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness)); Best = new Solution(Population[0]); while ((EFOs < MaxEFOs) && (Best.Fitness > theProblem.OptimalLocation)) { var Xnew = new Solution(this); //Se genera la armonia de tamaño NumVertices for (int i = 0; i < theProblem.NumVertices; i++) { if (myRandom.NextDouble() <= HMCR) { //Console.Write("r1->"+r3); var posr1 = myRandom.Next(0, PopulationSize - 1); var posr2 = MyRandom.Next(PopulationSize); while (posr1 == posr2) { posr2 = myRandom.Next(PopulationSize); } //Simplifican la tasa de consideracion de memoria y el pitch adjusment por medio de la formula var state = (int)(Population[posr1].Vertices[i] + Math.Pow((-1), (Population[posr1].Vertices[i])) * Math.Abs(Best.Vertices[i] - Population[posr2].Vertices[i])); if (state == 1) { Xnew.Activar(i); //Xnew.Vertices[i] = 1; } else { //el 25% de las medianas deben estar prendidas if (Xnew.PosInstalaciones.Count < Math.Floor(theProblem.PMedianas * .25)) { Xnew.Activar(i); } } } else { //se crea una nota con un valor al azar para ser diversificado if (MyRandom.NextDouble() < 0.5) { if (Xnew.PosInstalaciones.Count < Math.Floor(theProblem.PMedianas * .25)) { Xnew.Activar(i); } } else { Xnew.Activar(i); } } } //Xnew.RepararSolutionAwareness(); Xnew.RepairSolutionRandomly(); Xnew.Evaluate(); var worstFitness = Population.Max(x => x.Fitness); var posworstFitness = Population.FindIndex(x => Math.Abs(x.Fitness - worstFitness) < 1e-10); //if (Xnew.MyAlgorithm.MyRandom.NextDouble() < 0.1) {Xnew = Utils.LocalSearchGen(Xnew, Best); } //Evalua la evaluacion de la nueva armonia con la evaluacion de la peor solucion de la memoria if (Xnew.Fitness < Population[posworstFitness].Fitness) { //Si se cumple la condicion se remplaza la peor armonia por la nueva armonia Population[posworstFitness] = Xnew; } Population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness)); Best = new Solution(Population[0]); //Best.Imprimir(); } }
/// <summary> /// Ejecutart el algoritmo HSOS /// </summary> /// <param name="theProblem, myRandom"></param> public override void Ejecutar(PMediana theProblem, Random myRandom) { MyProblem = theProblem; MyRandom = myRandom; EFOs = 0; //Inicializar la Population //Population = InitializeFixedPopulation(PopulationSize); Best = Utils.Getbest(Population); //Iteracion del algoritmo while (EFOs < MaxEFOs && Best.Fitness > theProblem.OptimalLocation) { //Recorrido de la poblacon for (var i = 0; i < PopulationSize; i++) { //MutualismoJ var j = myRandom.Next(PopulationSize); while (i == j) { j = myRandom.Next(PopulationSize); } var m1 = Population[i].Mutualism(Population[j], Best); // if (m1.MyAlgorithm.MyRandom.NextDouble() < 0.1) m1 = Utils.LocalSearchGen(m1, Best); if (m1.Fitness < Population[i].Fitness) { Population[i] = m1; } if (EFOs >= MaxEFOs) { break; } //MutualismoI var m2 = Population[j].Mutualism(Population[i], Best); // if (m2.MyAlgorithm.MyRandom.NextDouble() < 0.1) m2 = Utils.LocalSearchGen(m2, Best); if (m2.Fitness < Population[j].Fitness) { Population[j] = m2; } if (EFOs >= MaxEFOs) { break; } //Comensalimo j = myRandom.Next(PopulationSize); while (i == j) { j = myRandom.Next(PopulationSize); } var m3 = Population[i].Commensalism(Population[j], Best); //if (m3.MyAlgorithm.MyRandom.NextDouble() < 0.1) m3 = Utils.LocalSearchGen(m3, Best); if (m3.Fitness < Population[i].Fitness) { Population[i] = m3; } if (EFOs >= MaxEFOs) { break; } //Parasitimo j = myRandom.Next(PopulationSize); while (i == j) { j = myRandom.Next(PopulationSize); } var m4 = Population[i].Parasitism(); // if (m4.MyAlgorithm.MyRandom.NextDouble() < 0.1) m4 = Utils.LocalSearchGen(m4, Best); if (m4.Fitness < Population[j].Fitness) { Population[j] = m4; } if (EFOs >= MaxEFOs) { break; } //Improisación de una nueva armonia var m5 = Improvisation(i); //if (m5.MyAlgorithm.MyRandom.NextDouble() < 0.1) m5 = Utils.LocalSearchGen(m5, Best); ; var worstFitness = Population.Max(x => x.Fitness); var posworstFitness = Population.FindIndex(x => Math.Abs(x.Fitness - worstFitness) < 1e-10); if (m5.Fitness < Population[posworstFitness].Fitness) { Population[posworstFitness] = m5; } if (m5.Fitness < Population[i].Fitness) { Population[i] = m5; } if (EFOs >= MaxEFOs) { break; } } Best = Utils.Getbest(Population); } }