Skip to content

Instantly share code, notes, and snippets.

@yasincidem
Created March 25, 2018 12:10
Show Gist options
  • Save yasincidem/084e8bafd7387d0291978f58d1249145 to your computer and use it in GitHub Desktop.
Save yasincidem/084e8bafd7387d0291978f58d1249145 to your computer and use it in GitHub Desktop.
mTSP W/ Random
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by yasincidem on 19.3.2018.
*/
public class mTSP {
private int numDepots;
private int numSalesmen;
private int cost = 0;
private static final Random rand = new Random();
private static final String[] cities = TurkishNetwork.cities;
private final int[][] distance = TurkishNetwork.distance;
private List<Integer> depots;
private List<List<Integer>> lists;
private final List<Integer> numbers0To80 = IntStream.range(0, 81).boxed().collect(Collectors.toList());
public mTSP(int numDepots, int numSalesmen) {
this.numDepots = numDepots;
this.numSalesmen = numSalesmen;
depots= rand.ints(0, cities.length)
.distinct()
.limit(numDepots)
.boxed()
.collect(Collectors.toList());
}
void randomSolution() {
lists = toSublists(numbers0To80);
for (final List<Integer> list : lists) {
for (int j = 0; j < list.size() - 1; j++) {
cost += distance[list.get(j)][list.get(j + 1)];
}
}
}
boolean validate() {
final List<Integer> collect = lists.stream().flatMap(List::stream).distinct().collect(Collectors.toList());
Collections.sort(collect);
return collect.equals(IntStream.range(0, 81).boxed().collect(Collectors.toList()));
}
int cost() {
return cost;
}
void print(boolean verbose) {
if (verbose) {
for (int i = 0; i < lists.size(); i++) {
final List<Integer> list = lists.get(i);
if ((i%numSalesmen) == 0) {
System.out.println( "Depot" + (i / numSalesmen + 1 )+ ": " + depots.get(i / numSalesmen));
}
System.out.print(" Route:" + (i%numSalesmen + 1) + " ");
for (int j = 0; j < list.size(); j++) {
if (j != 0 && j != list.size() - 1)
System.out.print(list.get(j) + (j != list.size() - 2 ? ", " : " " ));
}
System.out.println();
}
} else {
for (int i = 0; i < lists.size(); i++) {
final List<Integer> list = lists.get(i);
if ((i%numSalesmen) == 0) {
System.out.println("Depot" + (i / numSalesmen + 1)+ ": " + cities[depots.get(i / numSalesmen)]);
}
System.out.print(" Route:" + (i%numSalesmen + 1) + " ");
for (int j = 0; j < list.size(); j++) {
if (j != 0 && j != list.size() - 1)
System.out.print(cities[list.get(j)] + (j != list.size() - 2 ? ", " : " " ));
}
System.out.println();
}
}
}
private List<List<Integer>> toSublists(List<Integer> lists) {
Collections.shuffle(lists);
return partition(lists, (numDepots * numSalesmen));
}
private List<List<Integer>> partition(List<Integer> iterable, int partitions){
iterable.removeAll(depots);
List<List<Integer>> result = new ArrayList<>(partitions);
for(int i = 0; i < partitions; i++){
result.add(new ArrayList<>());
}
Iterator<Integer> iterator = iterable.iterator();
for(int i = 0; iterator.hasNext(); i++)
result.get(i % partitions).add(iterator.next());
for (int i = 0; i < result.size(); i++) {
result.get(i).add(0, depots.get(i / numSalesmen));
result.get(i).add(result.get(i).size() , depots.get(i / numSalesmen));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment