How to Perform Game Theory Based Resource Allocation using Cloudsim?
Share
Condition for Perform Game Theory Based Resource Allocation using Cloudsim
Description: To perform game theory-based resource allocation in CloudSim, model interactions between providers and users as a game where participants optimize objectives like cost or performance. Providers act as players competing to allocate resources efficiently, while users submit requirements via brokers. Set up datacenters with VMs and resources such as CPU, memory, and storage. Implement a game-theoretic approach, such as Nash Equilibrium or Stackelberg Game, where players adjust resource strategies based on payoffs considering factors like cost or utilization. Simulate the process in CloudSim through iterative rounds, updating strategies until a stable allocation is achieved, demonstrating efficient resource utilization.
Sample Code
Game_Theory.java:
package GameTheory;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
public class Game_Theory extends javax.swing.JFrame {
/**
* The cloudlet lists.
*/
private static List cloudletList1;
private static List cloudletList2;
/**
* The vmlists.
*/
private static List vmlist1;
private static List vmlist2;
int num_user;
int num_task;
HashMap hm = new HashMap();
/**
* Creates new form Game_Theory
*/
public Game_Theory() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel1.setText("Number of Users");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel2.setText("Number of Tasks");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton1.setText("Bid of the user per unit resource");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
jLabel3.setText("Resource Specification");
jButton2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton2.setText("PayOff");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PayOff p1 = new PayOff(hm);
p1.setVisible(true);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
num_user = Integer.parseInt(jTextField1.getText()); // number of cloud users
num_task = Integer.parseInt(jTextField2.getText());
jTextArea1.setText("");
for (int i = 0; i < num_user; i++) {
Random rand = new Random();
int r1 = rand.nextInt(4);
r1 = r1 + 1;
int r2 = rand.nextInt(4);
r2 = r2 + 5;
int r3 = rand.nextInt(4);
r3 = r3 + 10;
String text = "User-" + String.valueOf(i) + " Cost_Small_Task:" + r1 + " Cost_Medium_Task:" + r2 + " Cost_Large_Task:" + r3 + " \n";
jTextArea1.append(text);
String user = "User" + i;
String cost = "Small Task:"+r1 + "#" +"Medium Task:"+ r2 + "#" +"Large Task:"+ r3;
hm.put(user, cost);
}
createCloud();
}
public void createCloud() {
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation
@SuppressWarnings("unused")
Datacenter datacenter0 = createDatacenter("Datacenter_0");
//Datacenter datacenter1 = createDatacenter("Datacenter_1");
//Third step: Create Brokers
//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
DatacenterBroker broker[] = new DatacenterBroker[num_user];
int brokerId[] = new int[num_user];
int uid = 0;
int vid = 0;
for (int i = 0; i < num_user; i++) {
broker[i] = createBroker(i);
brokerId[i] = broker[i].getId();
vmlist1 = new ArrayList();
for (int m = 0; m < 3; m++) {
Vm vm1 = new Vm(vid, brokerId[i], mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmlist1.add(vm1);
vid++;
}
broker[i].submitVmList(vmlist1);
//Fifth step: Create two Cloudlets
cloudletList1 = new ArrayList();
for (int j = 0; j < num_task; j++) {
Cloudlet cloudlet1 = new Cloudlet(uid, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId[i]);
cloudletList1.add(cloudlet1);
uid++;
}
broker[i].submitCloudletList(cloudletList1);
}
// Sixth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over
for (int i = 0; i < num_user; i++) {
List newList1 = broker[i].getCloudletReceivedList();
Log.print("=============> User " + brokerId[i] + " ");
printCloudletList(newList1);
}
CloudSim.stopSimulation();
Log.printLine("CloudSimExample5 finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}
private static Datacenter createDatacenter(String name) {
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine
List hostList = new ArrayList();
// 2. A Machine contains one or more PEs or CPUs/Cores.
// In this example, it will have only one core.
List peList = new ArrayList();
int mips = 1000;
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating
peList.add(new Pe(1, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating
//4. Create Host with its id and list of PEs and add them to the list of machines
int hostId = 0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
//in this example, the VMAllocatonPolicy in use is SpaceShared. It means that only one VM
//is allowed to run on each Pe. As each Host has only one Pe, only one VM can run on each Host.
for (int i = 0; i < 10; i++) {
hostList.add(
new Host(
i,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerSpaceShared(peList)
)
); // This is our first machine
}
// 5. Create a DatacenterCharacteristics object that stores the
// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); //we are not adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker(int id) {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker" + id);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
*
* @param list list of Cloudlets
*/
private static void printCloudletList(List list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent
+ "Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
Log.printLine(indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId()
+ indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())
+ indent + indent + dft.format(cloudlet.getFinishTime()));
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Game_Theory().setVisible(true);
}
});
}
}
Payoff.java:
package GameTheory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class PayOff extends javax.swing.JFrame {
/**
* Creates new form PayOff
*/
int small_task_length = 1000;
int medium_task_length = 5000;
int large_task_length = 10000;
int small_vm_mips = 250;
int medium_vm_mips = 500;
int large_vm_mips = 1000;
int total_small_bid = 0;
int total_medium_bid = 0;
int total_large_bid = 0;
int spent_time = 0;
int bid_sum = 0;
int other_bids = 0;
int teta = 0;
int speed = 0;
int bid = 0;
double payoff=0.0;
public PayOff(HashMap hm) {
initComponents();
Iterator myVeryOwnIterator = hm.keySet().iterator();
while (myVeryOwnIterator.hasNext()) {
String key = (String) myVeryOwnIterator.next();
String value = (String) hm.get(key);
System.out.println(key + " " + value);
jComboBox1.addItem(key);
String ss[] = value.split("#");
//for (int i = 0; i < ss.length; i++) {
System.out.println("ss" + ss[0]);
String sb1[] = ss[0].split(":");
String sb2[] = ss[1].split(":");
String sb3[] = ss[2].split(":");
total_small_bid = total_small_bid + Integer.parseInt(sb1[1]);
total_medium_bid = total_medium_bid + Integer.parseInt(sb2[1]);
total_large_bid = total_large_bid + Integer.parseInt(sb3[1]);
//}
}
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when a selection has been made
jComboBox2.removeAllItems();
String key = (String) jComboBox1.getSelectedItem();
String cost = (String) hm.get(key);
String ss[] = cost.split("#");
for (int i = 0; i < ss.length; i++) {
jComboBox2.addItem(ss[i]);
payoff(ss);
}
}
});
}
public void payoff(String[] ss) {
//small
String ss1[] = ss[0].split(":");
int speed = small_task_length / small_vm_mips;
int teta = total_small_bid - Integer.parseInt(ss1[1]);
int bid = Integer.parseInt(ss1[1]);
int bid_sum = total_small_bid;
int other_bids = total_small_bid - bid;
int spent_time1 = speed + ((speed * teta) / bid);
int cost1 = spent_time1 * bid;
//medium
String ss2[] = ss[1].split(":");
int speedm = medium_task_length / medium_vm_mips;
int tetam = total_medium_bid - Integer.parseInt(ss2[1]);
int bidm = Integer.parseInt(ss2[1]);
int bid_summ = total_medium_bid;
int other_bidsm = total_medium_bid - bidm;
int spent_time2 = speedm + ((speedm * tetam) / bidm);
int cost2 = spent_time2 * bid;
//large
String ss3[] = ss[2].split(":");
int speedl = large_task_length / large_vm_mips;
int tetal = total_large_bid - Integer.parseInt(ss3[1]);
int bidl = Integer.parseInt(ss3[1]);
int bid_suml = total_large_bid;
int other_bidsl = total_large_bid - bidl;
int spent_time3 = speedl + ((speedl * tetal) / bidl);
int cost3 = spent_time3 * bid;
int total_time=spent_time1+spent_time2+spent_time3;
int total_cost=cost1+cost2+cost3;
System.out.println("Total time="+total_time+" Total cost="+total_cost);
int pe=1;int pt=1;
payoff= (pe*Math.log(total_cost))+(pt*Math.log(total_time));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jTextField4 = new javax.swing.JTextField();
jButton5 = new javax.swing.JButton();
jTextField5 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton6 = new javax.swing.JButton();
jTextField6 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel1.setText("User");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel2.setText("Task");
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select" }));
jButton1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton1.setText("Execution Speed");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton2.setText("Execution Time");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton3.setText("Payoff");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton4.setText("Sum of Bids");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton5.setText("Bids of Other Competitors");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
jLabel3.setText("Payoff Estimation");
jButton6.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jButton6.setText("Completion Cost");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String cost = (String) jComboBox2.getSelectedItem();
if (cost.contains("Small Task")) {
String ss[] = cost.split(":");
speed = small_task_length / small_vm_mips;
jTextField1.setText(String.valueOf(speed));
teta = total_small_bid - Integer.parseInt(ss[1]);
bid = Integer.parseInt(ss[1]);
bid_sum = total_small_bid;
other_bids = total_small_bid - bid;
} else if (cost.contains("Medium Task")) {
String ss[] = cost.split(":");
speed = medium_task_length / medium_vm_mips;
jTextField1.setText(String.valueOf(speed));
teta = total_medium_bid - Integer.parseInt(ss[1]);
bid = Integer.parseInt(ss[1]);
bid_sum = total_medium_bid;
other_bids = total_medium_bid - bid;
} else {
String ss[] = cost.split(":");
speed = large_task_length / large_vm_mips;
jTextField1.setText(String.valueOf(speed));
teta = total_large_bid - Integer.parseInt(ss[1]);
bid = Integer.parseInt(ss[1]);
bid_sum = total_large_bid;
other_bids = total_large_bid - bid;
}
spent_time = speed + ((speed * teta) / bid);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField2.setText(String.valueOf(spent_time));
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField3.setText(String.valueOf(bid_sum));
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField4.setText(String.valueOf(other_bids));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField5.setText(String.valueOf(payoff));
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int cost = spent_time * bid;
jTextField6.setText(String.valueOf(cost));
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new PayOff().setVisible(true);
}
});
}
}
Step 1
To specify the resource for Game theory.
Step 2
To set the number of users and number of task for game theory.
Step 3
Simulate tge bid of user per unit resource.
Step 4
To run the user 3 to 7 cloudlet task.
Step 5
To allocate the resources successfully..
Step 6
To perform the PayOff operation.
Step 7
To divide the task into three stage(small, medium and large)
Step 8
To set the user2 and perform user2 operation.
Step 9
To get the performance metrics like execution speed, execution time, completion cost, sum of bids, bids of other competitors and payoff values.
Step 10
Finally, get the total time and total cost for allocate the resources using game theory.