|
| 1 | +package com.thealgorithms.scheduling; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +/** |
| 8 | + * The LotteryScheduling class implements the Lottery Scheduling algorithm, which is |
| 9 | + * a probabilistic CPU scheduling algorithm. Processes are assigned tickets, and |
| 10 | + * the CPU is allocated to a randomly selected process based on ticket count. |
| 11 | + * Processes with more tickets have a higher chance of being selected. |
| 12 | + */ |
| 13 | +public final class LotteryScheduling { |
| 14 | + private LotteryScheduling() { |
| 15 | + } |
| 16 | + |
| 17 | + private List<Process> processes; |
| 18 | + private Random random; |
| 19 | + |
| 20 | + /** |
| 21 | + * Constructs a LotteryScheduling object with the provided list of processes. |
| 22 | + * |
| 23 | + * @param processes List of processes to be scheduled using Lottery Scheduling. |
| 24 | + */ |
| 25 | + public LotteryScheduling(final List<Process> processes) { |
| 26 | + this.processes = processes; |
| 27 | + this.random = new Random(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructs a LotteryScheduling object with the provided list of processes and a Random object. |
| 32 | + * |
| 33 | + * @param processes List of processes to be scheduled using Lottery Scheduling. |
| 34 | + * @param random Random object used for generating random numbers. |
| 35 | + */ |
| 36 | + public LotteryScheduling(final List<Process> processes, Random random) { |
| 37 | + this.processes = processes; |
| 38 | + this.random = random; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Schedules the processes using the Lottery Scheduling algorithm. |
| 43 | + * Each process is assigned a certain number of tickets, and the algorithm randomly |
| 44 | + * selects a process to execute based on ticket count. The method calculates the |
| 45 | + * waiting time and turnaround time for each process and simulates their execution. |
| 46 | + */ |
| 47 | + public List<Process> scheduleProcesses() { |
| 48 | + int totalTickets = processes.stream().mapToInt(Process::getTickets).sum(); |
| 49 | + int currentTime = 0; |
| 50 | + List<Process> executedProcesses = new ArrayList<>(); |
| 51 | + |
| 52 | + while (!processes.isEmpty()) { |
| 53 | + int winningTicket = random.nextInt(totalTickets) + 1; |
| 54 | + Process selectedProcess = selectProcessByTicket(winningTicket); |
| 55 | + |
| 56 | + if (selectedProcess == null) { |
| 57 | + // This should not happen in normal circumstances, but we'll handle it just in case |
| 58 | + System.err.println("Error: No process selected. Recalculating total tickets."); |
| 59 | + totalTickets = processes.stream().mapToInt(Process::getTickets).sum(); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + selectedProcess.setWaitingTime(currentTime); |
| 64 | + currentTime += selectedProcess.getBurstTime(); |
| 65 | + selectedProcess.setTurnAroundTime(selectedProcess.getWaitingTime() + selectedProcess.getBurstTime()); |
| 66 | + |
| 67 | + executedProcesses.add(selectedProcess); |
| 68 | + processes.remove(selectedProcess); |
| 69 | + |
| 70 | + totalTickets = processes.stream().mapToInt(Process::getTickets).sum(); |
| 71 | + } |
| 72 | + |
| 73 | + return executedProcesses; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Selects a process based on a winning ticket. The method iterates over the |
| 78 | + * list of processes, and as the ticket sum accumulates, it checks if the |
| 79 | + * current process holds the winning ticket. |
| 80 | + * |
| 81 | + * @param winningTicket The randomly generated ticket number that determines the selected process. |
| 82 | + * @return The process associated with the winning ticket. |
| 83 | + */ |
| 84 | + private Process selectProcessByTicket(int winningTicket) { |
| 85 | + int ticketSum = 0; |
| 86 | + for (Process process : processes) { |
| 87 | + ticketSum += process.getTickets(); |
| 88 | + if (ticketSum >= winningTicket) { |
| 89 | + return process; |
| 90 | + } |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * The Process class represents a process in the scheduling system. Each process has |
| 97 | + * an ID, burst time (CPU time required for execution), number of tickets (used in |
| 98 | + * lottery selection), waiting time, and turnaround time. |
| 99 | + */ |
| 100 | + public static class Process { |
| 101 | + private String processId; |
| 102 | + private int burstTime; |
| 103 | + private int tickets; |
| 104 | + private int waitingTime; |
| 105 | + private int turnAroundTime; |
| 106 | + |
| 107 | + public Process(String processId, int burstTime, int tickets) { |
| 108 | + this.processId = processId; |
| 109 | + this.burstTime = burstTime; |
| 110 | + this.tickets = tickets; |
| 111 | + } |
| 112 | + |
| 113 | + public String getProcessId() { |
| 114 | + return processId; |
| 115 | + } |
| 116 | + |
| 117 | + public int getBurstTime() { |
| 118 | + return burstTime; |
| 119 | + } |
| 120 | + |
| 121 | + public int getTickets() { |
| 122 | + return tickets; |
| 123 | + } |
| 124 | + |
| 125 | + public int getWaitingTime() { |
| 126 | + return waitingTime; |
| 127 | + } |
| 128 | + |
| 129 | + public void setWaitingTime(int waitingTime) { |
| 130 | + this.waitingTime = waitingTime; |
| 131 | + } |
| 132 | + |
| 133 | + public int getTurnAroundTime() { |
| 134 | + return turnAroundTime; |
| 135 | + } |
| 136 | + |
| 137 | + public void setTurnAroundTime(int turnAroundTime) { |
| 138 | + this.turnAroundTime = turnAroundTime; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments