private void show_gen_button_Click(object sender, EventArgs e)//Zobrazí genomy jednotlivých agentů { sim_form.Show(); sim_form.Refresh(); Visual_bridge.Reset(); Visual_bridge.Render_genome(); }
private void New_evol_button_Click(object sender, EventArgs e)//Vytvoří novou Simulaci s náhodně inicializovanými agenty { bool safe_to_continue = false; if (Num_of_agents_txtbox.Text != "" && Party_size_txbox.Text != "") { try { if (Convert.ToInt32(Num_of_agents_txtbox.Text) < 1 || Convert.ToInt32(Num_of_agents_txtbox.Text) > 30 || Convert.ToInt32(Party_size_txbox.Text) < 1 || Convert.ToInt32(Party_size_txbox.Text) > 10) { throw new Exception("WrongInput"); } safe_to_continue = true; } catch { MessageBox.Show("Invalid input! Number of agents must be between 1 and 30 and Parties must be between 1 and 10. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); safe_to_continue = false; } } if (safe_to_continue) { Logic.sim_in_progress = false; sim_form.Hide(); Visual_bridge.Reset(); Sim_control_group.Enabled = true; Evol_control_group.Enabled = true; Logic.parties_num = Convert.ToInt32(Num_of_agents_txtbox.Text); Logic.PARTY_SIZE = Convert.ToInt32(Party_size_txbox.Text); Logic.agents_in_race = Convert.ToInt32(Num_of_agents_txtbox.Text) * Logic.PARTY_SIZE; Logic.num_of_agents = Convert.ToInt32(Num_of_agents_txtbox.Text) * Logic.PARTY_SIZE * 3; Logic.Evolution_init(Sim_timer, sim_form, this); } }
public static void Cancel_battle() { timer.Enabled = false; sim_in_progress = false; Visual_bridge.Reset(); Generation_init(); }//Zruší bitvu
static bool Is_in_sight(Agent first, Agent second, out double[] vis_input)//Zjistí, jestli jeden vidí druhého a případně mu přiřadí visuální vstup { int dist = Get_dist(first, second); vis_input = new double[Agent.EYES_NUM]; bool output = false; if (dist > Agent.SIGHT_LENGTH) { return(false); } else { double angle = Get_angle(first, second); double tolerance = Math.Atan((double)Visual_bridge.Health_to_size(second.max_health) / (double)dist); for (int i = 0; i < Agent.EYES_NUM; i++) { vis_input[i] = 0.0; double eye_angle = first.orient + first.eyes[i]; Normalize_angle(ref eye_angle); if (Math.Abs(angle - eye_angle) < tolerance) { vis_input[i] = (double)(Agent.SIGHT_LENGTH - dist) / (double)Agent.SIGHT_LENGTH; if (first.color == second.color) { vis_input[i] *= -1; } output = true; } } return(output); } }
}//zjistí, co má každý brabenec za lubem podle jeho visuálního vstupu public static void Fire(Agent agent) { agent.time_to_fire = agent.reload_time; int x2 = agent.pos_X + (int)(Math.Cos(agent.orient) * Agent.FIRING_RANGE * 0.7); int y2 = agent.pos_Y + (int)(Math.Sin(agent.orient) * Agent.FIRING_RANGE * 0.7); Visual_bridge.Draw_bullet(agent.pos_X, agent.pos_Y, x2, y2, Visual_bridge.white_pen); int friendly_fire; //Multiplier for either adding or substracting score bool wasted_shot = true; for (int i = 0; i < PARTY_SIZE * 3; i++) { Agent second_agent = Current_agents[i]; if ((second_agent.alive == true) && !second_agent.Equals(agent) && (Get_dist(agent, second_agent) < Agent.FIRING_RANGE)) { if ((agent.orient < Math.Abs(Get_angle(agent, second_agent)) + Math.PI / 6) && (agent.orient > Math.Abs(Get_angle(agent, second_agent)) - Math.PI / 6)) { wasted_shot = false; second_agent.health -= agent.damage; friendly_fire = 1; if (agent.color == second_agent.color) { wasted_shot = true; friendly_fire = -1; } agent.fitness += DMG_PTS_MULTIPLIER * agent.damage * friendly_fire;; if (second_agent.health <= 0) { second_agent.alive = false; Visual_bridge.Kill_agent(second_agent.pos_X, second_agent.pos_Y, second_agent.max_health); Visual_bridge.Draw_dead_bullet(second_agent.pos_X, second_agent.pos_Y, second_agent.orient); agent.fitness += KILL_PTS * friendly_fire * agent.enemies_killed; if (friendly_fire == 1) { agent.enemies_killed++; agent.health += (agent.max_health / 3) % agent.max_health; //Nechat nebo nenechat? } } } } } if (wasted_shot) { agent.health -= MISS_PENALTY; } if (agent.health < 0) { agent.alive = false; Visual_bridge.Kill_agent(agent.pos_X, agent.pos_Y, agent.max_health); Visual_bridge.Draw_dead_bullet(agent.pos_X, agent.pos_Y, agent.orient); } }//Obstarává střelbu daného agenta
public void Commit_move()//Nastaví pozice, aby odpovídali agentovo rozhodnutí { int old_X = 0; int old_Y = 0; if (brain.output[0] != 0) { old_X = pos_X; old_Y = pos_Y; if (brain.output[0] > 0) { Move_forward(speed * brain.output[0]); } if (pos_X >= 850) { pos_X = 850; } if (pos_X <= 0) { pos_X = 0; } if (pos_Y >= 850) { pos_Y = 850; } if (pos_Y <= 0) { pos_Y = 0; } if (!Logic.fast_forward) { Visual_bridge.Move_agent(old_X, old_Y, pos_X, pos_Y, max_health, color_string, orient); } } Turn(brain.output[1] * 1);//Tady najít vhodnou konstantu! if ((brain.output[2] > 0.5) && (time_to_fire == 0)) { attacking = true; } if ((old_X == pos_X) && (old_Y == pos_Y)) { fitness--; } }
public static void Simulate_battle()//Má na starosti bitvy a jejich správný průběh { timer.Enabled = false; if (current_party >= parties_num) //Keeps control over which battle goes on { Finish_simulation(); } else { menu_form.progress_label.Text = "Simulating..."; Divide_agents(); Visual_bridge.Reset(); Position_init(Current_agents, PARTY_SIZE * 3); sim_in_progress = true; elapsed_ticks = 0; timer.Enabled = true; } }
private void Menu_form_Load(object sender, EventArgs e) { sim_form = new Sim_form(); Visual_bridge.Initialize(sim_form.Canvas_PB); }
public static void Excite_agents() { double[] sensory_input; double[] local_sens_input; for (int first_num = 0; first_num < PARTY_SIZE * 3; first_num++) { Agent agent = Current_agents[first_num]; if (agent.alive == true) { if (Visual_bridge.SHOW_ID) { Visual_bridge.Write_text(Convert.ToString(agent.id), agent.pos_X, agent.pos_Y, Visual_bridge.black_pen); } sensory_input = new double[Agent.SENSOR_NUM]; for (int sec_num = 0; sec_num < PARTY_SIZE * 3; sec_num++) { Agent sec_agent = Current_agents[sec_num]; if ((first_num != sec_num) && (Is_in_sight(agent, sec_agent, out local_sens_input))) { for (int i = 0; i < Agent.EYES_NUM; i++) { if (Math.Abs(local_sens_input[i]) > Math.Abs(sensory_input[i])) { sensory_input[i] = local_sens_input[i]; } } } } sensory_input[8] = 1 - agent.time_to_fire / agent.reload_time; agent.input = sensory_input; agent.Make_decision(); agent.Commit_move(); if (Collisions) { for (int i = 0; i < Current_agents.Length; i++) { Agent sec_agent = Current_agents[i]; if ((!agent.Equals(sec_agent)) && (sec_agent.alive == true) && (Get_dist(agent, sec_agent) < Visual_bridge.Health_to_size(agent.max_health) + Visual_bridge.Health_to_size(sec_agent.max_health))) { double angle = Get_angle(agent, sec_agent); angle = angle - Math.PI; Normalize_angle(ref angle); int old_x = agent.pos_X; int old_y = agent.pos_Y; agent.pos_X = agent.pos_X + (int)(Math.Cos(angle) * (Visual_bridge.Health_to_size(agent.max_health) + Visual_bridge.Health_to_size(sec_agent.max_health) - Get_dist(agent, sec_agent))); agent.pos_Y = agent.pos_Y + (int)(Math.Sin(angle) * (Visual_bridge.Health_to_size(agent.max_health) + Visual_bridge.Health_to_size(sec_agent.max_health) - Get_dist(agent, sec_agent))); Visual_bridge.Move_agent(old_x, old_y, agent.pos_X, agent.pos_Y, agent.max_health, agent.color_string, agent.orient); } } } if (agent.attacking == true) { Fire(agent); } agent.attacking = false; if (agent.time_to_fire > 0) { agent.time_to_fire--; } if (Visual_bridge.SHOW_ID) { Visual_bridge.Write_text(Convert.ToString(agent.id), agent.pos_X, agent.pos_Y, Visual_bridge.white_pen); } } else if (!fast_forward) { Visual_bridge.Kill_agent(agent.pos_X, agent.pos_Y, agent.max_health); } } }//zjistí, co má každý brabenec za lubem podle jeho visuálního vstupu