-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQMigration.java
73 lines (58 loc) · 2.83 KB
/
QMigration.java
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
import org.cloudsimplus.qlearning.QLearning;
import org.cloudsimplus.qlearning.StateActionPair;
public class InterDatacenterMigration1 {
// Existing code...
private final QLearning<Integer> qLearning;
private final double learningRate = 0.3;
private final double discountFactor = 0.9;
private final double explorationRate = 0.2;
public static void main(final String[] args) {
new InterDatacenterMigration1();
}
private InterDatacenterMigration1() {
// Existing code...
// Q-learning setup
qLearning = new QLearning<>(learningRate, discountFactor, explorationRate);
// Start simulation
simulation.start();
printResults();
System.out.printf("%n%s finished!%n", getClass().getSimpleName());
}
private void createAndSubmitVms(final DatacenterBroker broker, final int[] vmPesArray) {
final var vmList = Arrays.stream(vmPesArray).mapToObj(this::createVm).toList();
broker.submitVmList(vmList);
vmList.forEach(vm -> {
vm.addOnMigrationStartListener(this::startMigration);
vm.addOnHostAllocationListener(evt -> {
int currentState = calculateState(evt.getHost().getPeList(), evt.getVm().getTotalAllocatedMips());
int action = qLearning.chooseAction(currentState, getAvailableActions(evt.getHost()));
performAction(action, evt.getVm(), evt.getHost());
});
});
}
private int calculateState(List<Pe> peList, long vmMips) {
double totalAvailableMips = peList.stream().mapToDouble(Pe::getCapacity).sum();
double utilization = vmMips / totalAvailableMips;
return (int) (utilization * 100); // Simplified state representation (utilization percentage)
}
private int[] getAvailableActions(Host host) {
// Example actions: placing VM on different PEs, or migrating VM to another host
return IntStream.range(0, host.getPeList().size()).toArray();
}
private void performAction(int action, Vm vm, Host host) {
// Example: action = index of PE where VM is placed
vm.setHost(host).setPeNumber(action);
// Example reward calculation (adjust as per your use case)
double reward = calculateReward(host);
// Update Q-table
int currentState = calculateState(host.getPeList(), vm.getTotalAllocatedMips());
qLearning.learn(currentState, action, reward);
}
private double calculateReward(Host host) {
// Example: Reward based on CPU utilization improvement or migration action
double currentUtilization = host.getUtilizationOfCpu();
// Compute reward based on current and previous utilization
return currentUtilization < HOST_UNDER_UTILIZATION_THRESHOLD_FOR_VM_MIGRATION ? 1.0 : -1.0;
}
// Existing methods...
}