/** * Crea un objeto ConfiguracionTabuSearch con valores por defecto */ public ConfiguracionTabuSearch() { tipoAspiracion = CriteriosAspiracionEnum.POR_OBJETIVO; tipoParada = CriteriosParadaEnum.NUM_ITERACIONES; listaTabu = new TabuListMovimientos(); maximizacion = true; objetivo = 0; }
public Tuple <float, double> Execute(Matrix matrix, int iterations, int cadency) { _watch = new Stopwatch(); _matrix = matrix; _problemSize = _matrix.Rows; _numberOfIterations = iterations; _tabuList = new TabuList(_problemSize); SetupCurrentSolution(); SetupBestSolution(); int l = 0; _watch.Start(); for (int i = 0; i < _numberOfIterations; i++) { int from = 0; int to = 0; float iterationBestCost = float.MaxValue; for (int j = 1; j < _currentSolution.Count - 1; j++) { for (int k = 1; k < _currentSolution.Count - 1; k++) { if (j != k && _tabuList.GetField(j, k) == 0) { SwapCurrentSolution(j, k); float currentCost = _matrix.CalculateDistance(_currentSolution); if ((currentCost < iterationBestCost)) { from = j; to = k; iterationBestCost = currentCost; l = 0; } else { _currentSolution = new List <int> (_bestSolution); l++; } } if (l == 5) { _currentSolution = this.ShuffleList(_currentSolution); l = 0; } } } SwapBestSolution(from, to); _bestCost = _matrix.CalculateDistance(_bestSolution); if (from != 0) { _tabuList.Decrement(); _tabuList.Move(from, to, cadency); } } _watch.Stop(); ShowResult(_bestSolution.Reverse().ToList()); return(new Tuple <float, double> (_bestCost, _watch.Elapsed.TotalMilliseconds)); }
private bool AddElementsAndCheck(IList <EdgesAdded> elementsToAdd, int tabuListSize, EdgesAdded elementToCheck) { var tabuList = new TabuList(tabuListSize); foreach (var element in elementsToAdd) { tabuList.AddChange(element); } return(tabuList.IsOntabuList(elementToCheck)); }
private void test() { Graph1 = WMH.Model.GraphGenerator.GraphGenerator.generateGraph(4); Graph2 = WMH.Model.GraphGenerator.GraphGenerator.generateGraph(4); ltm = new LongTermMemory(10); tl = new TabuList(10); isc = new IterationStopCriteria(100); ncsc = new NoChangesStopCriteria(10); csc = new CostStopCriteria(1000); }
public void ScorePatch(Board board, TabuList tabulist) { ApplyPatch(); if (tabulist.Exist(board)) { Score = 0x1ffff; } else { Score = board.GetErrorCount(); } UndoPatch(); }
private void parametersAlg(object sender, EventArgs e) { try { AlgorithmParametersView apv = new AlgorithmParametersView(); apv.ShowDialog(); ltm = new LongTermMemory(apv.longTermMemory); tl = new TabuList(apv.tabuList); isc = new IterationStopCriteria(apv.IterationStopCrit); ncsc = new NoChangesStopCriteria(apv.NoChangeStopCrit); csc = new CostStopCriteria(apv.CostCrit); } catch (Exception) { throw; } }
private void StartAlgorithm(int vertexNumber, int ltmLenght, int stmLenght, int endCryt, int noChangeFor, double costLessThan, int?numberOfLoop) { string currentDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string pathG1, pathG2, pathResult; if (numberOfLoop != null) { pathG1 = currentDir + "\\g1\\graph" + numberOfLoop; pathG2 = currentDir + "\\g2\\graph" + numberOfLoop; pathResult = currentDir + "\\result\\res" + numberOfLoop; } else { pathG1 = currentDir + "\\g1\\graph"; pathG2 = currentDir + "\\g2\\graph"; pathResult = currentDir + "\\result\\res"; } Graph Graph1 = GraphGenerator.generateGraph(vertexNumber); WMH.Model.FileOperation.FileOperationGraph.SaveToFile(pathG1, Graph1.Vertexes); Graph Graph2 = GraphGenerator.generateGraph(vertexNumber); WMH.Model.FileOperation.FileOperationGraph.SaveToFile(pathG2, Graph2.Vertexes); LongTermMemory ltm = new LongTermMemory(ltmLenght); TabuList tl = new TabuList(stmLenght); IterationStopCriteria isc = new IterationStopCriteria(endCryt); NoChangesStopCriteria ncsc = new NoChangesStopCriteria(noChangeFor); CostStopCriteria csc = new CostStopCriteria(costLessThan); WMH.TabuSearch.CostFinder cf = new TabuSearch.CostFinder(); WMH.TabuSearch.NeighbourFinder nf = new TabuSearch.NeighbourFinder(cf, ltm); WMH.TabuSearch.Implementation.AspirationCriteria ac = new TabuSearch.Implementation.AspirationCriteria(cf); WMH.TabuSearch.TabuSearch alg = new TabuSearch.TabuSearch(nf, tl, ltm, cf, ac, isc, ncsc, csc); System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew(); IList <Edge> result = alg.FindSolution(Graph1, Graph2); stopWatch.Stop(); WMH.Model.FileOperation.FileOperationResult.SaveResultToFile(result, pathResult, stopWatch); }
public AllDifferentWithTabuSearch() { _tabu_list = new TabuList(); _constraints = new List <ConstraintCollection>(); _rnd = new Random(); }
/** * Permite fijar la lista tabú que va a ser usada * @param listaTabu es la lista tabú que será usada por el algoritmo */ public void setListaTabu(TabuList listaTabu) { this.listaTabu = listaTabu; }
public override List <BeeParams> Regeration(List <BeeParams> Source, double Border1, int Repeat1, double Border2, int Repeat2, double Border3, int Repeat3, int DividenSizeHive) { TabuList.Clear(); Source.Sort(sorterBees); double maxError = Source.Sum(x => x.Goods); List <BeeParams> tempResult = new List <BeeParams>(); tempResult.Add(Source[0]); object Locker = new object(); Parallel.For(0, Source.Count, (int i) => { lock (Source[i]) { int tempCounter = 0; double relativeGoods = Source[i].Goods / maxError; if ((relativeGoods > 0) && (relativeGoods < Border1)) { while (tempCounter < Repeat1) { lock (Locker) { tempResult.Add(Source[i]); } tempCounter++; } } else { if ((relativeGoods > Border1) && (relativeGoods < Border2)) { while (tempCounter < Repeat2) { lock (Locker) { tempResult.Add(Source[i]); } tempCounter++; } } else { if ((relativeGoods > Border2) && (relativeGoods < Border3)) { while (tempCounter < Repeat2) { lock (Locker) { tempResult.Add(Source[i]); } tempCounter++; } } } } } }); if (DividenSizeHive < tempResult.Count) { tempResult.RemoveRange(DividenSizeHive, tempResult.Count - DividenSizeHive); } return(tempResult); }
/** * Permite fijar el tipo de lista tabú que va a ser usada * @param listaTabu es la lista tabú que se va a usar en el algoritmo de * Búsqueda Tabú * @param tabuTenure es el Tabu Tenure que va a ser usado, es decir, el número * de iteraciones que un elemento va a permanecer en la lista tabú */ public void setListaTabu(TabuList listaTabu, int tabuTenure) { this.listaTabu = listaTabu; this.listaTabu.setTabuTenure(tabuTenure); }
/** * Permite obtener el Individuo aceptado en caso de que se use el criterio de * aspiración por defecto * @param currentSolution es la solución a partir de la cual se generó el vecindario * @param neighbourhood es el conjunto de individuos (vecindario) generado en * la iteración actual del algoritmo de Búsqueda Tabú * @param listaTabu es la lista tabú actual * @return el Individuo del vecindario al cual le queda menos tiempo para salir * de la lista tabú */ public Individual getIndividuoAceptado(Individual currentSolution, List <Individual> neighbourhood, TabuList listaTabu) { int i = 0, posMenor, auxTiempo, menorTiempo = int.MaxValue; foreach (Individual neighbour in neighbourhood) { auxTiempo = listaTabu.tiempoTabu(currentSolution, neighbour); if (auxTiempo < menorTiempo) { menorTiempo = auxTiempo; posMenor = i; } i++; } return(neighbourhood[i]); }
private bool CompareEdges(Edge firstEdge, Edge secondEdge) // Funkcja sprawdzajaca czy zamiana dwoch krawedzi polepszy rozwiazanie { var cost = CalculateCost(BestSolution); // Zapamietanie kosztu dotychczas najlepszego rozwiazania var list = CurrentSolution.ToList(); // Zapamietanie obecnego rozwiazania var edge = new Edge(list[firstEdge.Start], list[firstEdge.End]); // Zapamietanie krawedzi ktora jest pod tymi indeksami, aby pozniej mozna bylo ja zamienic list[firstEdge.Start] = list[secondEdge.Start]; // Zamiana krawedzi list[firstEdge.End] = list[secondEdge.End]; list[secondEdge.Start] = edge.Start; list[secondEdge.End] = edge.End; var cost1 = CalculateCost(list); // Obliczenie kosztu otrzymanego rozwiazania var list1 = CurrentSolution.ToList(); // Zapamietanie obecnego rozwiazania var edge1 = new Edge(list1[firstEdge.Start], list1[firstEdge.End]); // Inna mozliwozc zamiany krawedzi list1[firstEdge.Start] = list1[secondEdge.End]; list1[firstEdge.End] = list1[secondEdge.Start]; list1[secondEdge.Start] = edge.End; list1[secondEdge.End] = edge.Start; var cost2 = CalculateCost(list1); // Obliczenie kosztu otrzymanego rozwiazania if (cost1 < cost2) // Jezeli pierwsze otrzymane rozwiazanie jest lepsze od drugiego { if (cost1 < cost) // Jezeli rozwiazanie jest lepsze od dotychczas najlepszego { if (TabuList.Count != 0) // Jezeli tabu lista nie jest pusta zmniejsz kadencje kazdego elementu { for (int k = TabuList.Count - 1; k >= 0; k--) { TabuList[k].Cadency--; if (TabuList[k].Cadency == 0) { TabuList.RemoveAt(k); } } } var tabu = new TabuElement(Cadency, edge); // Dodanie pierwszej krawedzi z ruchu do tabu listy TabuList.Add(tabu); CurrentSolution = list; // Zapisanie otzrymanego rozwiazania jako najlepsze i aktualne BestSolution.Clear(); foreach (var j in CurrentSolution) { BestSolution.Add(j); } return(true); } } else // Jezeli drugie otrzymane rozwiazanie jest lepsze od pierwszego { if (cost2 < cost) // Jezeli rozwiazanie jest lepsze od dotychczas najlepszego { if (TabuList.Count != 0) // Jezeli tabu lista nie jest pusta zmniejsz kadencje kazdego elementu { for (int k = TabuList.Count - 1; k >= 0; k--) { TabuList[k].Cadency--; if (TabuList[k].Cadency == 0) { TabuList.RemoveAt(k); } } } var tabu = new TabuElement(Cadency, edge1); // Dodanie pierwszej krawedzi z ruchu do tabu listy TabuList.Add(tabu); CurrentSolution = list1; // Zapisanie otzrymanego rozwiazania jako najlepsze i aktualne BestSolution.Clear(); foreach (var j in CurrentSolution) { BestSolution.Add(j); } return(true); } } return(false); }