Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package fr.univ.dblp.simulation;
import fr.univ.dblp.utils.RandomSampler;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import java.util.*;
/**
* Stratégies d'immunisation pour contrôler la propagation virale.
*/
public class ImmunizationStrategy {
/**
* Aucune immunisation - tous les nœuds sont susceptibles
*/
public static Set<String> noImmunization(Graph graph) {
return new HashSet<>();
}
/**
* Immunisation aléatoire : sélectionne une fraction aléatoire de nœuds
*
* @param graph Le graphe
* @param fraction Fraction de nœuds à immuniser (entre 0 et 1)
* @return Ensemble des IDs de nœuds immunisés
*/
public static Set<String> randomImmunization(Graph graph, double fraction) {
int numToImmunize = (int) Math.round(graph.getNodeCount() * fraction);
List<Node> immunizedNodes = RandomSampler.sampleNodes(graph, numToImmunize);
Set<String> immuneIds = new HashSet<>();
for (Node node : immunizedNodes) {
immuneIds.add(node.getId());
}
return immuneIds;
}
/**
* Immunisation sélective (acquaintance immunization) :
* Chaque nœud sélectionné aléatoirement convainc un de ses voisins de s'immuniser.
* Cette stratégie cible préférentiellement les nœuds à haut degré.
*
* @param graph Le graphe
* @param fraction Fraction de nœuds qui participent à la campagne (chacun immunise un voisin)
* @return Ensemble des IDs de nœuds immunisés
*/
public static Set<String> acquaintanceImmunization(Graph graph, double fraction) {
int numParticipants = (int) Math.round(graph.getNodeCount() * fraction);
List<Node> participants = RandomSampler.sampleNodes(graph, numParticipants);
Set<String> immuneIds = new HashSet<>();
Random random = new Random();
for (Node participant : participants) {
// Obtenir un voisin aléatoire
List<Node> neighbors = new ArrayList<>();
participant.edges().forEach(edge -> {
Node neighbor = edge.getOpposite(participant);
neighbors.add(neighbor);
});
if (!neighbors.isEmpty()) {
Node randomNeighbor = neighbors.get(random.nextInt(neighbors.size()));
immuneIds.add(randomNeighbor.getId());
}
}
return immuneIds;
}
/**
* Immunisation ciblée des hubs : immunise les nœuds avec le plus haut degré
*
* @param graph Le graphe
* @param fraction Fraction de nœuds à immuniser
* @return Ensemble des IDs de nœuds immunisés
*/
public static Set<String> hubImmunization(Graph graph, double fraction) {
int numToImmunize = (int) Math.round(graph.getNodeCount() * fraction);
// Trier les nœuds par degré décroissant
List<Node> sortedNodes = new ArrayList<>();
graph.nodes().forEach(sortedNodes::add);
sortedNodes.sort((n1, n2) -> Integer.compare(n2.getDegree(), n1.getDegree()));
Set<String> immuneIds = new HashSet<>();
for (int i = 0; i < Math.min(numToImmunize, sortedNodes.size()); i++) {
immuneIds.add(sortedNodes.get(i).getId());
}
return immuneIds;
}
/**
* Calcule le degré moyen d'un ensemble de nœuds
*
* @param graph Le graphe
* @param nodeIds IDs des nœuds à analyser
* @return Degré moyen
*/
public static double getAverageDegree(Graph graph, Set<String> nodeIds) {
if (nodeIds.isEmpty()) {
return 0.0;
}
int totalDegree = 0;
for (String nodeId : nodeIds) {
Node node = graph.getNode(nodeId);
if (node != null) {
totalDegree += node.getDegree();
}
}
return (double) totalDegree / nodeIds.size();
}
/**
* Calcule le degré moyen des nœuds NON immunisés
*
* @param graph Le graphe
* @param immuneIds IDs des nœuds immunisés
* @return Degré moyen des non-immunisés
*/
public static double getAverageDegreeNonImmune(Graph graph, Set<String> immuneIds) {
Set<String> nonImmuneIds = new HashSet<>();
graph.nodes().forEach(node -> {
if (!immuneIds.contains(node.getId())) {
nonImmuneIds.add(node.getId());
}
});
return getAverageDegree(graph, nonImmuneIds);
}
}