Skip to content

Commit

Permalink
Муравьиный алгоритм
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Nov 29, 2024
1 parent 20df342 commit 03a0059
Showing 1 changed file with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -72,16 +73,16 @@ private double random() {
return random.nextDouble(1.);
}

void step(Matrix pheromones) {
void step(Matrix pheromones, Parameters parameters) {
if (path.isEmpty()) {
path.add(current);
visited.add(current);
}
Set<String> neighbours = new HashSet<>();
List<Graph.Vertex<T, W>> neighbours = new ArrayList<>();
List<Graph.Edge<T, W>> edges = graph.getEdges(current);
edges.forEach(edge -> {
if (!visited.contains(edge.target())) {
neighbours.add(edge.target().label());
neighbours.add(edge.target());
}
});
if (neighbours.isEmpty()) {
Expand All @@ -92,8 +93,48 @@ void step(Matrix pheromones) {
});
return;
}
Map<String, Double> choosing = new HashMap<>();
Map<Graph.Vertex<T, W>, Double> choosing = new HashMap<>();
{
List<Double> wish = new LinkedList<>();
double summary = 0;
for (Graph.Vertex<T, W> v : neighbours) {
var tau = pheromones.values[current.index()][v.index()];
var weight = edges.stream().filter(e -> e.target().equals(v)).findAny()
.map(e -> e.weight().doubleValue()).orElse(0.);
var n = 1. / weight;
double w = Math.pow(tau, parameters.kAlpha.doubleValue()) * Math.pow(n, parameters.kBeta.doubleValue());
wish.add(w);
summary += w;
}

Graph.Vertex<T, W> prev = null;
for (int i = 0; i < neighbours.size(); i++) {
var vertex = neighbours.get(i);
var v = wish.get(i);
double p = v / summary;
if (i == 0) {
choosing.put(vertex, p);
} else {
choosing.put(vertex, choosing.get(prev) + p);
}
prev = vertex;
}
}
double v = random();
Graph.Vertex<T, W> next = null;
for (Graph.Vertex<T, W> vertex : neighbours) {
Double d = choosing.get(vertex);
if (v <= d) {
next = vertex;
break;
}
}
path.add(next);
visited.add(next);
Graph.Vertex<T, W> finalNext = next;
distance += graph.getEdges(current).stream().filter(e -> e.target().equals(finalNext))
.findAny().map(e -> e.weight().doubleValue()).orElse(0.);
current = finalNext;
}
}

Expand Down

0 comments on commit 03a0059

Please sign in to comment.