-
Notifications
You must be signed in to change notification settings - Fork 36
/
service_batch_generator.py
54 lines (42 loc) · 1.92 KB
/
service_batch_generator.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
#-*- coding: utf-8 -*-
import numpy as np
class ServiceBatchGenerator(object):
"""
Implementation of a random service chain generator
Attributes:
state[batchSize, maxServiceLength] -- Generated random service chains
serviceLength[batchSize] -- Generated array contining services length
"""
def __init__(self, batchSize, minServiceLength, maxServiceLength, numDescriptors):
"""
Args:
batchSize(int) -- Number of service chains to be generated
minServiceLength(int) -- Minimum service length
maxServiceLength(int) -- Maximum service length
numDescriptors(int) -- Number of unique descriptors
"""
self.batchSize = batchSize
self.minServiceLength = minServiceLength
self.maxServiceLength = maxServiceLength
self.numDescriptors = numDescriptors
self.serviceLength = np.zeros(self.batchSize, dtype='int32')
self.state = np.zeros((self.batchSize, self.maxServiceLength), dtype='int32')
def getNewState(self):
""" Generate new batch of service chain """
# Clean attributes
self.serviceLength = np.zeros(self.batchSize, dtype='int32')
self.state = np.zeros((self.batchSize, self.maxServiceLength), dtype='int32')
# Compute random services
for batch in range(self.batchSize):
self.serviceLength[batch] = np.random.randint(self.minServiceLength, self.maxServiceLength+1, dtype='int32')
for i in range(self.serviceLength[batch]):
pktID = np.random.randint(0, self.numDescriptors, dtype='int32')
self.state[batch][i] = pktID
if __name__ == "__main__":
# Define generator
batch_size = 5
minServiceLength = 2
maxServiceLength = 6
numDescriptors = 8
env = ServiceBatchGenerator(batch_size, minServiceLength, maxServiceLength, numDescriptors)
env.getNewState()