Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.64 KB | None | 0 0
  1. class Factory:
  2.     def __init__(self, unit_type, exp_grade=None):
  3.         self.exp_grade = exp_grade
  4.         self.unit_type = unit_type
  5.    
  6.     def create_unit(self, unit_name='default_name'):
  7.         func_nodes = []
  8.         unit_data = pd.read_csv('data/{}.csv'.format(self.unit_type), delimiter=';')
  9.         for node in unit_data.values:
  10.             node_type = node[0]
  11.             node_weight = node[1]
  12.             func_nodes.append(self.create_func_node(node_type, node_weight))
  13.         return EquipmentUnit(unit_name, self.unit_type, func_nodes)
  14.  
  15.     def create_func_node(self, node_type, node_weight):
  16.         param_groups = []
  17.         node_data = pd.read_csv('data/{0}/{1}.csv'.format(self.unit_type, node_type), delimiter=';')
  18.         for group in node_data.values:
  19.             group_type = group[0]
  20.             group_weight = group[1]
  21.             param_groups.append(self.create_param_group(group_type, group_weight, node_type))
  22.         return FunctionalNode(node_type, param_groups, node_weight)
  23.        
  24.     def create_param_group(self, group_type, group_weight, node_type):
  25.         params = pd.DataFrame()
  26.         group_data = pd.read_csv('data/{0}/{1}/{2}.csv'.format(self.unit_type, node_type, group_type),\
  27.                                  delimiter=';')
  28.         for param in group_data.values:
  29.             cols = ['name', 'grade']
  30.             params = pd.DataFrame(columns=cols)
  31.             if self.exp_grade != None:
  32.                 param[1] = self.rand_grade()
  33.             params = params.append(pd.DataFrame([[param[0], param[1]]],columns=cols))  
  34.         return ParamGroup(group_type, group_weight, params)
  35.        
  36.     def rand_grade(self):
  37.         grade = round(np.random.normal(self.exp_grade, 1, 1)[0])
  38.         if grade > 4:
  39.             grade = 4
  40.         if grade < 0:
  41.             grade = 0
  42.         return grade
  43.  
  44. class EquipmentUnit:
  45.     def __init__(self, unit_name, unit_type, func_nodes):
  46.         self.unit_name = unit_name
  47.         self.unit_type = unit_type
  48.         self.func_nodes = func_nodes
  49.        
  50.     def __repr__(self):
  51.         return '{0}.{1}'.format(self.unit_name, self.unit_type)
  52.        
  53.     def __str__(self):
  54.         string = 'Unit name: {0}\nUnit type: {1}\nUnit consist of nodes:{2}'\
  55.         .format(self.unit_name, self.unit_type, [x.node_type for x in self.func_nodes])
  56.         return string
  57.        
  58.     def get_unit_itc(self):
  59.         unit_itc = sum([x.node_weight*x.get_node_itc() for x in self.func_nodes])
  60.         return unit_itc
  61.    
  62. class FunctionalNode:
  63.     def __init__(self, node_type, param_groups, node_weight):
  64.         self.node_type = node_type
  65.         self.param_groups = param_groups
  66.         self.node_weight = node_weight
  67.        
  68.     def __repr__(self):
  69.         return self.node_type
  70.    
  71.     def __str__(self):
  72.         string = 'Node type: {0}\nNode weight: {1}\nNode consist of groups:{2}'\
  73.         .format(self.node_type, self.node_weight, [x.group_type for x in self.param_groups])
  74.         return string
  75.        
  76.     def get_node_itc(self):
  77.         node_itc = 100*sum([x.group_weight*x.grade() for x in self.param_groups])/4
  78.         return node_itc
  79.    
  80. class ParamGroup:
  81.     def __init__(self, group_type, group_weight, params):
  82.         self.group_type = group_type
  83.         self.group_weight = group_weight
  84.         self.params = params
  85.        
  86.     def __repr__(self):
  87.         return self.group_type
  88.    
  89.     def __str__(self):
  90.         string = 'Group type: {0}\nGroup weight: {1}\nGroup grade: {2}'\
  91.         .format(self.group_type, self.group_weight, self.grade())
  92.         return string
  93.    
  94.     def grade(self):
  95.         return self.params['grade'].min()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement