def test_no_5_some_batch_data(self): agents = get_SmartAgents() actual_phase = 0 orange_phase_duration = 2 phase_duration = orange_phase_duration + 1 den = [0, 0, 10, 8] den_pred = [2, 2, 2, 78] state_to_predict = LearningState( actual_phase=actual_phase, starting_actual_phase=actual_phase, phase_duration=phase_duration, global_densities=den_pred + [0, 0], densities=den, orange_phase_duration=orange_phase_duration) for i in range(150): # utrzymanie fazy - gorszy wybór action_0 = [0] env = single_simulate(agents, actual_phase, phase_duration, den, orange_phase_duration=orange_phase_duration, actions=action_0, u=env_settings.u_all_2) for x in range(40): simulate_from_env(env, action_0) action_1 = [1] # zmiana fazy - lepszy wybór # print('stan bazowy', env.x[env.t]) # print(env.global_memories[-1]) simulate_from_env(env, action_1) # print('stan', env.x[env.t]) # print('stan po', env.x[env.t]) simulate_from_env(env, [orange]) # print('stan', env.x[env.t]) simulate_from_env(env, [orange]) # print('stan', env.x[env.t]) for x in range(20): simulate_from_env(env, action_1) # print('stan', env.x[env.t]) memories = env.global_memories memories += env.global_memories save_batches(agents) batches = get_batches() train(agents=agents) env.global_memories = [] for agent in env.agents: agent.memories = [] prediction = agents[0].model.predict( state_to_predict.to_learn_array()) # zbiega to 10,20, cokolwiek print('prediction :)', prediction) exportData = ExportData(learningMethod='DQN', learningEpochs=0, nets=memories, netName='net14', densityName='test_fixed_no_4') exportData.saveToJson() a = 23
def assign_local_state(self, densities): global_densities = [] for road in sections_of_roads: for sec in road: global_densities.append(densities[sec]) local_state = LearningState( global_densities=global_densities, actual_phase=self.actual_phase, phase_duration=self.phase_duration, densities=densities, starting_actual_phase=self.actual_phase, yellow_phase_duration=self.yellow_phase_duration) local_state.phase_absences = [x for x in self.phase_absences] self.local_state = local_state
def get_radom_learning_state(): pre_cross_densities = (random.randint(0, 5), random.randint(0, 5), random.randint(0, 5)) global_aggregated_densities = () for i in range(12): global_aggregated_densities = global_aggregated_densities + (random.randint(0, 10),) phase_index: int = random.randint(0, 4) phase_duration: int = random.randint(0, 15) ls = LearningState(pre_cross_densities, global_aggregated_densities, phase_index, phase_duration) return ls
def centroid(self): if len(self.states) == 0: return self.random_centroid all_arr_states = [state.to_array() for state in self.states] arr_state = [] for i in range(len(all_arr_states[0])): mean_of_i_coordinate = np.mean([x[i] for x in all_arr_states]) arr_state.append(mean_of_i_coordinate) return LearningState.from_array(arr_state)
def single_simulate(agents, actual_phase, phase_duration, den, orange_phase_duration=2): densities: List[int] = den + [0, 0] # dodajemy 2 ostatnie nieistotne global_densities: List[int] = densities env = Env(agents) state = LearningState(orange_phase_duration=orange_phase_duration, actual_phase=actual_phase, starting_actual_phase=actual_phase, phase_duration=phase_duration, global_densities=global_densities, densities=densities) env.assign_state(state) env.step([1])
def assign_local_state(self, densities): global_densities = [] for road in sections_of_roads: for sec in road: global_densities.append(densities[sec]) local_state = LearningState(global_densities=global_densities, actual_phase=self.actual_phase, phase_duration=self.phase_duration, densities=densities, starting_actual_phase=self.actual_phase, orange_phase_duration=self.orange_phase_duration) self.local_state = local_state
def assign_local_state(self, densities): pre_cross_densities = () for sec in self.local_phase_sections: pre_cross_densities = pre_cross_densities + (densities[sec], ) global_aggregated_densities = () for road in sections_of_roads: global_aggregated_densities = global_aggregated_densities + (round( np.sum([densities[section] for section in road])), ) local_state = LearningState( pre_cross_densities=pre_cross_densities, global_aggregated_densities=global_aggregated_densities, phase_index=self.actual_phase, phase_duration=self.phase_duration) self.local_state = local_state
def single_simulate(agents, actual_phase, phase_duration, den, orange_phase_duration=2, actions=[1], u=env_settings.u): densities: List[int] = den + [0, 0] # dodajemy 2 ostatnie nieistotne - one sa juz na wyjsciu global_densities: List[int] = densities env = Env(agents) env.u = u state = LearningState(orange_phase_duration=orange_phase_duration, actual_phase=actual_phase, starting_actual_phase=actual_phase, phase_duration=phase_duration, global_densities=global_densities, densities=densities) env.assign_state(state) env.assign_local_states_to_agents() env.step(actions) return env
def assign_local_state(self, densities): pre_cross_densities = () for sec in self.local_phase_sections: pre_cross_densities = pre_cross_densities + (densities[sec],) global_aggregated_densities = () global_densities = [] for road in sections_of_roads: for sec in road: global_densities.append(densities[sec]) local_state = LearningState(pre_cross_densities=pre_cross_densities, global_aggregated_densities=global_aggregated_densities, global_densities=global_densities, actual_phase=self.actual_phase, phase_duration=self.phase_duration, densities=densities, starting_actual_phase=self.actual_phase) self.local_state = local_state
def assign_local_state(self, densities): pre_cross_densities = () for sec in self.local_phase_sections: pre_cross_densities = pre_cross_densities + (densities[sec],) global_aggregated_densities = () global_densities = [] densities_9 = [] for road in sections_of_roads: global_aggregated_densities = global_aggregated_densities + ( round(np.sum([densities[section] for section in road])),) for sec in road: global_densities.append(densities[sec]) for sec in self.sections_9_indexes: densities_9.append(densities[sec]) local_state = LearningState(pre_cross_densities=pre_cross_densities, global_aggregated_densities=global_aggregated_densities, global_densities=global_densities, actual_phase=self.actual_phase, phase_duration=self.phase_duration, densities_9=densities_9, starting_actual_phase=self.actual_phase) self.local_state = local_state
def add_state(self, state: LearningState): state.cluster_index = self.get_cluster(state).index self.states.append(state) self.clusters[state.cluster_index].states.append(state)
from model.LearningState import LearningState from services.parser import to_array S1 = [] S1.append( LearningState(pre_cross_densities=(1, 0, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0), phase_index=3, phase_duration=3)) S1.append( LearningState(pre_cross_densities=(2, 1, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0), phase_index=3, phase_duration=1)) S1.append( LearningState(pre_cross_densities=(2, 2, 2), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0), phase_index=3, phase_duration=1)) S1.append( LearningState(pre_cross_densities=(2, 0, 2), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0), phase_index=0, phase_duration=0)) S1.append( LearningState(pre_cross_densities=(1, 0, 1), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0,
from model.LearningState import LearningState from services.parser import to_array S1=[] S1.append(LearningState(pre_cross_densities=(1, 0, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0), actual_phase=3, phase_duration=3)) S1.append(LearningState(pre_cross_densities=(2, 1, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0), actual_phase=3, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 2, 2), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0), actual_phase=3, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 0, 2), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(1, 0, 1), global_aggregated_densities=(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0), actual_phase=3, phase_duration=2)) S1.append(LearningState(pre_cross_densities=(2, 3, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(2, 2, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=2, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(1, 2, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(2, 3, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 2.0, 1.0, 1.0, 1.0, 0.0, 2.0), actual_phase=1, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 0, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 2.0, 1.0, 1.0, 1.0, 0.0, 2.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(1, 0, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 2.0, 1.0, 1.0, 1.0, 0.0, 2.0), actual_phase=1, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(1, 2, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(2, 1, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=1, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 0, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(1, 2, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=2, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 0, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=0, phase_duration=0)) S1.append(LearningState(pre_cross_densities=(1, 1, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=2, phase_duration=1)) S1.append(LearningState(pre_cross_densities=(2, 2, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0), actual_phase=0, phase_duration=0)) S2=[] S2.append(to_array(LearningState(pre_cross_densities=(2, 2, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0), actual_phase=0, phase_duration=0))) S2.append(to_array(LearningState(pre_cross_densities=(0, 2, 2), global_aggregated_densities=(0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=0, phase_duration=0))) S2.append(to_array(LearningState(pre_cross_densities=(2, 0, 1), global_aggregated_densities=(0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=0, phase_duration=0))) S2.append(to_array(LearningState(pre_cross_densities=(1, 2, 0), global_aggregated_densities=(0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0), actual_phase=2, phase_duration=1))) S2.append(to_array(LearningState(pre_cross_densities=(1, 2, 2), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0), actual_phase=1, phase_duration=1))) S2.append(to_array(LearningState(pre_cross_densities=(2, 0, 1), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0), actual_phase=2, phase_duration=1))) S2.append(to_array(LearningState(pre_cross_densities=(1, 2, 0), global_aggregated_densities=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0), actual_phase=0, phase_duration=0)))