public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); int[] assignment = TSPUtils.GreedySolution(instance); TSPUtils.LocalSearch2OptFirst(instance, assignment); TSPSolution solution = new TSPSolution(instance, assignment); solution.Write(fileOutput); }
public void Write(string file) { double cost = TSPUtils.Fitness(Instance, Path); using (StreamWriter writer = File.CreateText(file)) { writer.WriteLine(cost); writer.WriteLine(Instance.NumberCities); for (int i = 0; i < Instance.NumberCities; i++) { writer.WriteLine(Path[i] + 1); } } }
protected override int[] InitialSolution() { int[] solution; if (generatedSolutions == 0) { solution = TSPUtils.GreedySolution(Instance); } else { solution = TSPUtils.RandomSolution(Instance); } generatedSolutions++; return(solution); }
public MaxMinAntSystem2OptFirst4TSP(TSPInstance instance, int numberAnts, double rho, double alpha, double beta, int maxReinit, int candidateLength, double candidateWeight) : base(instance.NumberCities, TSPUtils.Fitness(instance, TSPUtils.RandomSolution(instance)), numberAnts, rho, alpha, beta, maxReinit) { Instance = instance; this.candidateWeight = candidateWeight; // Build the candidate list. this.candidateLists = new List <Tuple <double, int> > [Instance.NumberCities]; for (int i = 0; i < Instance.NumberCities; i++) { this.candidateLists[i] = new List <Tuple <double, int> >(); for (int j = 0; j < Instance.NumberCities; j++) { if (i != j) { this.candidateLists[i].Add(new Tuple <double, int>(Instance.Costs[i, j], j)); } } this.candidateLists[i].Sort(); this.candidateLists[i].RemoveRange(candidateLength, this.candidateLists[i].Count - candidateLength); } }
protected override double Fitness(int[] solution) { return(TSPUtils.Fitness(Instance, solution)); }
protected override double Fitness(int[] individual) { return(TSPUtils.Fitness(Instance, individual)); }
protected override void Repair(int[] individual) { TSPUtils.Repair(Instance, individual); }
protected override int[] GetNeighbor(int[] solution) { return(TSPUtils.GetNeighbor(Instance, solution)); }
protected override void LocalSearch(int[] individual) { TSPUtils.LocalSearch2OptFirst(Instance, individual); }
protected override int[] InitialSolution() { return(TSPUtils.GRCSolution(Instance, RclTreshold)); }
protected override double Distance(int[] a, int[] b) { return(TSPUtils.Distance(Instance, a, b)); }
protected override void Repair(int[] solution) { TSPUtils.Repair(Instance, solution); }
protected override void LocalSearch(int[] solution) { TSPUtils.LocalSearch2OptBest(Instance, solution); }
protected override int[] GRCSolution() { return(TSPUtils.GRCSolution(Instance, RCLThreshold)); }
protected override void PerturbateSolution(int[] solution, int perturbation) { TSPUtils.PerturbateSolution(solution, perturbation); }
protected override Tuple <int, int> GetTabu(int[] current, int[] neighbor) { return(TSPUtils.GetTabu(current, neighbor)); }
protected override void Improve(int[] solution) { TSPUtils.LocalSearch2OptFirst(Instance, solution); }