-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacenter.py
88 lines (66 loc) · 2.92 KB
/
datacenter.py
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
# -*- coding: utf-8 -*-
"""
@author: Xining Wang
"""
from mdp import *
import random
import math
# ===============================================================================================
# class for representing the data center control as an
# MDPs. Nothing here is specific to the SSB-Q-Learning algorithm whatsoever.
# ===============================================================================================
class DataCenterMDP (MDP):
def __init__ (self):
self.server = 30
self.poisson = {"low": self.server/2, "normal": self.server*3/2, "high": self.server*5/2}
self.cumulatedCost = 0
states = ["s"+"0"+str(i) + "0" + str(j) for i in xrange(1,10) for j in xrange(10)] + \
["s"+"0"+str(i)+str(j) for i in xrange(1,10) for j in xrange(10,91)] + \
["s"+str(i)+"0"+str(j) for i in xrange(10,31) for j in xrange(10)] + \
["s"+str(i)+str(j) for i in xrange(10,31) for j in xrange(10,91)] + ["s0"]
actions = ["reinit"] + ["A"+ "0" +str(i) for i in xrange(1,10)] + ["A"+str(i) for i in xrange(10,31)]
wealthLevels = []
finalStates = ["s0"]
random.seed(10)
#self.real_nash_equilibrium = {"w1":1./3,"w2":1./3, "w3":1./3}
MDP.__init__(self,states,actions,wealthLevels,self.allowedActionsFunction,finalStates,self.wealthFunction,self.transitionFunction,self.ssbFunction,random.choice(states[:-1]),"DataCenter",5)
self.transitionTable = {}
self.generateTransitionTable()
def allowedActionsFunction (self, state):
return self.actions[1:]
def wealthFunction (self, finalState):
pass
def generateTransitionTable(self):
for i in self.states[:-1]:
self.transitionTable[i] = {}
for j in self.actions[1:]:
self.transitionTable[i][j] = {}
lam = 0
if int(i[-1:]) < 20:
lam = self.poisson["low"]
elif int(i[-1:]) < 40:
lam = self.poisson["normal"]
else:
lam = self.poisson["high"]
for nextArrival in xrange(91):
if nextArrival < 10:
nextArrivalStr = "0" + str(nextArrival)
else:
nextArrivalStr = str(nextArrival)
self.transitionTable[i][j]["s"+j[1:]+nextArrivalStr] = lam**nextArrival*math.exp(-lam)/math.factorial(nextArrival)
return
def transitionFunction (self, state, action):
if action == "reinit":
return {self.initialState:1.}
if action == "stop":
return {"s0":1.}
return self.transitionTable[state][action]
def ssbFunction():
pass
def __str__ (self):
return "Datacenter"
def main():
mdp = DataCenterMDP()
print(mdp.transitionTable)
if __name__ == "__main__":
main()