def simulate(self, rand_int): for k in range(self.no_days): print 'Day: ', k self.obtain_prism_model() # if self.prob30_diff_list: if self.pareto_points: # for e,x in self.prob30_diff_list_sorted: for e, x in enumerate(self.pareto_points_sorted): self.actions = [] self.obtained_rewards = [] self.battery = [] self.charging = [] self.time = [] # f_ind = self.prob30_diff_list.index(x) f_ind = self.pareto_points.index(x) pareto_point = self.pareto_points[f_ind] print pareto_point, 'Pareto Point ------------------------------------------' f_no = self.f_no_list[f_ind] if f_no != None: #######################SPECIFY LOCATION AS BEFORE ###################### print 'Reading from bcth_model_t' + f_no + '.adv' self.pp = bc_read_adversary.ParseAdversary([ 'bcth_model_t' + f_no + '.adv', 'bcth_model_t.sta', 'bcth_model_t.lab' ]) else: raise ValueError('Adversary Not Found!!!') battery = np.zeros((self.no_simulations)) charging = np.zeros((self.no_simulations)) init_cluster = np.zeros((self.no_simulations)) tr_day = np.zeros((self.no_simulations)) for i in range(self.no_simulations): final_state = self.simulate_day(k) t, _, _, _, b, ch, cl = self.pp.get_state(final_state) battery[i] = int(b) # print battery[i] charging[i] = int(ch) # print charging[i] tr_day[i] = np.sum( self.obtained_rewards[k * self.no_int:(k + 1) * self.no_int]) self.get_plan( 'bcth_pareto' + str(e) + '_' + self.plan_id + '_70b_' + str(rand_int), pareto_point) self.init_battery = int(np.mean(battery)) self.init_charging = int(np.mean(charging)) self.avg_totalreward[k] = np.mean(tr_day) # print self.init_battery, ' end battery' # print self.init_charging, ' end charging' else: raise ValueError('Adversary List Not Found!!!')
def obtain_prism_model(self, t): prob_c = np.zeros((self.horizon, len(self.clusters))) prob_t = np.zeros((self.horizon, 2)) for k in range(self.horizon): prob_c[k] = self.prob[(t + k) % self.no_int] prob_t[k] = self.task_prob[(t + k) % self.no_int] pm = bcth_prism_model.PrismModel('model_rhc.prism', self.horizon, self.init_battery, self.init_charging, self.cl_id[t], prob_t, self.clusters, prob_c, self.charge_model, self.discharge_model) #######################SPECIFY LOCATION ###################### # running prism and saving output from prism with open(self.path_data + 'result_rhc', 'w') as f: process = subprocess.call( './prism ' + self.path_mod + 'model_rhc.prism ' + self.path_mod + 'batterycost_model_prop.props -paretoepsilon 0.1 -v -exportadv ' + self.path_mod + 'model_rhc.adv -exportprodstates ' + self.path_mod + 'model_rhc.sta -exporttarget ' + self.path_mod + 'model_rhc.lab', cwd='/home/milan/prism/prism/bin', shell=True, stdout=f) ##reading output from prism to find policy file ### for bcth policy_file = [] pre1_point = None pre2_point = None with open(self.path_data + 'result_rhc', 'r') as f: line_list = f.readlines() f_no_list = [] pareto_points = [] init = 0 for e, line in enumerate(line_list): if 'pre1.adv' in line: pre1_point = abs( float( line_list[e + 1].split(',')[0].split('(')[1].strip())) if 'pre2.adv' in line: pre2_point = abs( float( line_list[e + 1].split(',')[0].split('(')[1].strip())) if ': New point is (' in line: el = line.split(' ') if init == 0: init_no = int(el[0][:-1]) cost40 = abs(float(el[4][1:-1])) pareto_points.append(cost40) f_no_list.append(str(int(el[0][:-1]) - 2)) init += 1 if 'pre1' == self.req_pareto_point or 'pre2' == self.req_pareto_point: f_no = self.req_pareto_point if self.req_pareto_point == 'pre1': self.pareto_point.append(pre1_point) elif self.req_pareto_point == 'pre2': self.pareto_point.append(pre2_point) else: if f_no_list: if self.req_pareto_point > 3 and self.req_pareto_point < 6: approx_p_point = min(pareto_points) + ( (max(pareto_points) - min(pareto_points)) / 3) * (float((self.req_pareto_point % 3)) / 3) elif self.req_pareto_point == 6: sorted_pareto_points = sorted(pareto_points) if len(sorted_pareto_points) > 1: approx_p_point = sorted_pareto_points[1] else: approx_p_point = sorted_pareto_points[0] else: approx_p_point = min(pareto_points) + ( (max(pareto_points) - min(pareto_points))) * ( float(self.req_pareto_point) / 3 ) ## 3 -> no. of pareto points being considered p_point = min(pareto_points, key=lambda x: abs(x - approx_p_point)) self.pareto_point.append(p_point) f_ind = pareto_points.index(p_point) f_no = f_no_list[f_ind] else: f_no = None if f_no != None: print 'Reading from model_rhc' + f_no + '.adv' pp = bc_read_adversary.ParseAdversary([ 'model_rhc' + f_no + '.adv', 'model_rhc.sta', 'model_rhc.lab' ]) return pp else: raise ValueError('Adversary Not Found !!!')
def obtain_prism_model(self): pm = bcth_prism_model.PrismModel('model_t.prism', self.init_battery, self.init_charging, self.task_prob, self.clusters, self.prob, self.charge_model, self.discharge_model) #######################SPECIFY LOCATION ###################### ### running prism and saving output from prism with open(self.path_data + 'result_fhc', 'w') as file: process = subprocess.Popen( './prism ' + self.path_mod + 'model_t.prism ' + self.path_mod + 'batterycost_model_prop.props -v -paretoepsilon 0.1 -exportadv ' + self.path_mod + 'model_t.adv -exportprodstates ' + self.path_mod + 'model_t.sta -exporttarget ' + self.path_mod + 'model_t.lab', cwd='/home/milan/prism/prism/bin', shell=True, stdout=subprocess.PIPE) for c in iter(lambda: process.stdout.read(1), ''): sys.stdout.write(c) file.write(c) ### reading output from prism to find policy file ### for dt policy_file = [] pre1_point = None pre2_point = None with open(self.path_data + 'result_fhc', 'r') as f: line_list = f.readlines() f_no_list = [] pareto_points = [] init = 0 for e, line in enumerate(line_list): if 'pre1.adv' in line: pre1_point = abs( float( line_list[e + 1].split(',')[0].split('(')[1].strip())) if 'pre2.adv' in line: pre2_point = abs( float( line_list[e + 1].split(',')[0].split('(')[1].strip())) if ': New point is (' in line: el = line.split(' ') if init == 0: init_no = int(el[0][:-1]) cost40 = abs(float(el[4][1:-1])) pareto_points.append(cost40) f_no_list.append(str(int(el[0][:-1]) - 2)) init += 1 if 'pre1' == self.req_pareto_point or 'pre2' == self.req_pareto_point: f_no = self.req_pareto_point if self.req_pareto_point == 'pre1': self.pareto_point.extend(self.no_int * [pre1_point]) elif self.req_pareto_point == 'pre2': self.pareto_point.extend(self.no_int * [pre2_point]) else: if f_no_list: if self.req_pareto_point > 3: approx_p_point = min(pareto_points) + ( (max(pareto_points) - min(pareto_points)) / 3) * (float((self.req_pareto_point % 3)) / 3) else: approx_p_point = max(pareto_points) * ( float(self.req_pareto_point) / 3 ) ## 3 -> no. of pareto points being considered p_point = min(pareto_points, key=lambda x: abs(x - approx_p_point)) self.pareto_point.extend(self.no_int * [p_point]) f_ind = pareto_points.index(p_point) f_no = f_no_list[f_ind] else: f_no = None if f_no != None: print 'Reading from model_t' + f_no + '.adv' pp = bc_read_adversary.ParseAdversary( ['model_t' + f_no + '.adv', 'model_t.sta', 'model_t.lab']) return pp else: raise ValueError('Adversary Not Found!!!')