Skip to content

Commit 4a7a567

Browse files
authored
Merge pull request #28 from Dacarpe03/Stage2-Menu
Stage2 menu
2 parents 0e38c52 + b01d791 commit 4a7a567

21 files changed

+3407
-3222
lines changed

EcoSim/Assets/Controller/Controller.cs

Lines changed: 228 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,56 @@
44
using System.IO;
55
using System.Linq;
66
using UnityEngine;
7+
using UnityEngine.UI;
78

89
public class Controller : MonoBehaviour
910
{
10-
/*
11-
* These are equilibrium parameters with the simplestrategy
12-
private double PREY_REPRODUCTION_PROB = 1;
13-
private double PREDATOR_REPRODUCTION_PROB = 1;
14-
15-
private double PREY_MAX_SPEED = 0.5;
16-
private double PREDATOR_MAX_SPEED = 0.6;
17-
18-
private double PREY_VISION_RADIUS = 8;
19-
private double PREDATOR_VISION_RADIUS = 15;
20-
21-
private int PREY_GROUP_SIZE = 500 ;
22-
private int PREDATOR_GROUP_SIZE = 15;
23-
*/
11+
public bool ready;
12+
public bool firstTime;
13+
//Paramenters for menu
14+
public GameObject menu;
15+
//Predators
16+
private double reproductionPredator;
17+
private double maxSpeedPredator;
18+
private double visionRadiusPredator;
19+
private int initialPopulationPredator;
20+
public GameObject reprodPredator;
21+
public GameObject speedPredator;
22+
public GameObject visionPredator;
23+
public GameObject initialPredator;
24+
//Preys
25+
private double reproductionPrey;
26+
private double maxSpeedPrey;
27+
private double visionRadiusPrey;
28+
private int initialPopulationPrey;
29+
public GameObject reprodPrey;
30+
public GameObject speedPrey;
31+
public GameObject visionPrey;
32+
public GameObject initialPrey;
33+
34+
//Plants
35+
private double initialPlants;
36+
private double growthRate;
37+
private double threshold;
38+
public GameObject initPlants;
39+
public GameObject grRate;
40+
public GameObject trhold;
41+
42+
//Strategy
43+
private int strategy;
44+
private int iterations;
45+
private int agents;
46+
public GameObject stratMenu;
47+
public GameObject itMenu;
48+
public GameObject agentsMenu;
2449

2550
//PARAMETERS OF SIMULATION
26-
private int NUMBER_OF_SIMULATIONS = 40;
51+
private int NUMBER_OF_SIMULATIONS = 1;
2752
private int ITERATIONS_PER_SIMULATION = 200;
2853

29-
private double INITIAL_PLANTS = 1200;
30-
private double GROWTH_RATE = 1.7;
31-
private double THRESHOLD = 200;
32-
//Reproduction probability, maximum speed, visionRadius, GroupSize
33-
private GroupParameters _preyParameters = new GroupParameters(0.9, 0.55, 8, 500);
34-
private GroupParameters _predatorParameters = new GroupParameters(0.3, 0.6, 15, 12);
54+
//Reproduction probability, maximum speed, visionRadius, GroupSize
55+
private GroupParameters _preyParameters;
56+
private GroupParameters _predatorParameters;
3557
//END PARAMETERS OF SIMULATION
3658

3759
//PATHS FOR FILES
@@ -56,77 +78,109 @@ private void Awake()
5678
QualitySettings.vSyncCount = 1;
5779
Application.targetFrameRate = 30;
5880
}
81+
5982
void Start()
6083
{
61-
Debug.Log("Simulación " + this._simulationCounter);
62-
63-
//Initialize the ecosystem
64-
Resource plants = new Resource(INITIAL_PLANTS, GROWTH_RATE, THRESHOLD);
65-
this._ecosystem = new Ecosystem(this._preyParameters, this._predatorParameters, plants);
66-
67-
//Initialize the view
68-
this._myView = Instantiate(MyView);
69-
this._myView.Initialize(this._preyParameters.GroupSize, this._predatorParameters.GroupSize);
70-
this.CalculateTotalSimulations();
71-
72-
this.PATH = Application.dataPath + "/SimulationData/SimulationDataPhase2/";
73-
Debug.Log(this.PATH);
74-
this.CreateFileForSimulation();
75-
}//End Start
76-
84+
this.ready = false;
85+
this.firstTime = true;
86+
//Predators
87+
this.reproductionPredator = 0.3;
88+
this.maxSpeedPredator = 0.6;
89+
this.visionRadiusPredator = 30;
90+
this.initialPopulationPredator = 8;
91+
//Preys
92+
this.reproductionPrey = 0.9;
93+
this.maxSpeedPrey = 0.55;
94+
this.visionRadiusPrey = 8;
95+
this.initialPopulationPrey = 200;
96+
//Plants
97+
this.initialPlants = 1200;
98+
this.growthRate = 1.7;
99+
this.threshold = 200;
100+
//Strategy
101+
this.strategy = 0;
102+
this.iterations = 200;
103+
this.agents = 4;
104+
}
77105

78106
// Update is called once per frame
79107
void Update()
80108
{
81-
//If we haven't completed the number of iterations fixed or there is no animal group extinguised we keep on the loop
82-
if (this._ecosystem.Iteration < ITERATIONS_PER_SIMULATION & !this._ecosystem.Extinguised)
109+
if (this.ready)
83110
{
84-
//If the ecosystem has resseted (the iteration has finished), then we reset the view and save the data in the file
85-
if (this._ecosystem.Reset)
111+
Debug.Log("Updateamos");
112+
if (this.firstTime) {
113+
this.firstTime = false;
114+
Debug.Log("Simulación " + this._simulationCounter);
115+
116+
this._predatorParameters = new GroupParameters(this.reproductionPredator, this.maxSpeedPredator, this.visionRadiusPredator, this.initialPopulationPredator);
117+
this._preyParameters = new GroupParameters(this.reproductionPrey, this.maxSpeedPrey, this.visionRadiusPrey, this.initialPopulationPrey);
118+
//Initialize the ecosystem
119+
Resource plants = new Resource(initialPlants, growthRate, threshold);
120+
Debug.Log(initialPlants);
121+
Debug.Log(threshold);
122+
this._ecosystem = new Ecosystem(this._preyParameters, this._predatorParameters, plants, iterations, agents, strategy);
123+
124+
//Initialize the view
125+
this._myView = Instantiate(MyView);
126+
this._myView.Initialize(this._preyParameters.GroupSize, this._predatorParameters.GroupSize);
127+
this.CalculateTotalSimulations();
128+
129+
this.PATH = Application.dataPath + "/SimulationData/SimulationDataPhase2/";
130+
Debug.Log(this.PATH);
131+
this.CreateFileForSimulation();
132+
}
133+
134+
//If we haven't completed the number of iterations fixed or there is no animal group extinguised we keep on the loop
135+
if (this._ecosystem.Iteration < ITERATIONS_PER_SIMULATION & !this._ecosystem.Extinguised)
86136
{
87-
this.UpdateFile();
88-
this._ecosystem.Update();
137+
//If the ecosystem has resseted (the iteration has finished), then we reset the view and save the data in the file
138+
if (this._ecosystem.Reset)
139+
{
140+
this.UpdateFile();
141+
this._ecosystem.Update();
142+
this.ResetView();
143+
}
144+
//If not we update the ecosystem (the next step/frame of the iteration)
145+
else
146+
{
147+
//Update the model
148+
this._ecosystem.Update();
149+
150+
//Pass the information of positions from model to view
151+
List<Vector3> preyModelPositions = this.TransformToVector3(this._ecosystem.GetPreyPositions());
152+
List<Vector3> predatorModelPositions = this.TransformToVector3(this._ecosystem.GetPredatorPositions());
153+
this._myView.UpdatePositions(preyModelPositions, predatorModelPositions);
154+
}
155+
}
156+
//If the simulation has finished, we start another one if necessary
157+
else if (this._simulationCounter < this.NUMBER_OF_SIMULATIONS)
158+
{
159+
this._simulationCounter++;
160+
this._totalSimulations += 1;
161+
Debug.Log("Simulación " + this._simulationCounter);
162+
163+
//Initialize the ecosystem
164+
Resource plants = new Resource(initialPlants, growthRate, threshold);
165+
this._ecosystem = new Ecosystem(this._preyParameters, this._predatorParameters, plants, iterations, agents, strategy);
166+
//Reset the view
89167
this.ResetView();
168+
//Create a new file to save the data of the simulation
169+
this.CreateFileForSimulation();
90170
}
91-
//If not we update the ecosystem (the next step/frame of the iteration)
92171
else
93172
{
94-
//Update the model
95-
this._ecosystem.Update();
96-
97-
//Pass the information of positions from model to view
98-
List<Vector3> preyModelPositions = this.TransformToVector3(this._ecosystem.GetPreyPositions());
99-
List<Vector3> predatorModelPositions = this.TransformToVector3(this._ecosystem.GetPredatorPositions());
100-
this._myView.UpdatePositions(preyModelPositions, predatorModelPositions);
173+
Debug.Log("Fin de las simulaciones");
174+
Destroy(this.gameObject);
101175
}
102176
}
103-
//If the simulation has finished, we start another one if necessary
104-
else if (this._simulationCounter < this.NUMBER_OF_SIMULATIONS)
105-
{
106-
this._simulationCounter++;
107-
this._totalSimulations += 1;
108-
Debug.Log("Simulación " + this._simulationCounter);
109-
110-
//Initialize the ecosystem
111-
Resource plants = new Resource(INITIAL_PLANTS, GROWTH_RATE, THRESHOLD);
112-
this._ecosystem = new Ecosystem(this._preyParameters, this._predatorParameters, plants);
113-
//Reset the view
114-
this.ResetView();
115-
//Create a new file to save the data of the simulation
116-
this.CreateFileForSimulation();
117-
}
118-
else
119-
{
120-
Debug.Log("Fin de las simulaciones");
121-
Destroy(this.gameObject);
122-
}
123177
}//End Update
124178

125179
//Transforms from Vec3 to Vector3
126180
public List<Vector3> TransformToVector3(List<Vec3> vectors)
127181
{
128182
List<Vector3> newVectors = new List<Vector3>();
129-
foreach(Vec3 v in vectors)
183+
foreach (Vec3 v in vectors)
130184
{
131185
Vector3 vNew = new Vector3((float)v.XCoord, (float)v.YCoord, (float)v.ZCoord);
132186
newVectors.Add(vNew);
@@ -168,8 +222,8 @@ public void CreateFileForSimulation()
168222
sr.WriteLine(this.ITERATIONS_PER_SIMULATION
169223
+ "|" + this._preyParameters.toString()
170224
+ "|" + this._predatorParameters.toString()
171-
+ "|" + INITIAL_PLANTS
172-
+ "|" + GROWTH_RATE
225+
+ "|" + initialPlants
226+
+ "|" + growthRate
173227
);
174228

175229
sr.WriteLine("Iteracion|InicialPresas|InicialPredadores|SupervivientesPresas|SupervivientesPredadores|Plantas|PlantasSupervivientes");
@@ -212,6 +266,106 @@ private void CalculateTotalSimulations()
212266

213267
}//End CalculateTotalSimulations
214268
}
269+
270+
public void InitiateParameters() {
271+
this.ready = true;
272+
273+
//Predator parameters
274+
string strRepPred = reprodPredator.GetComponent<Text>().text;
275+
if (!string.IsNullOrEmpty(strRepPred)) {
276+
this.reproductionPredator = Convert.ToDouble(strRepPred);
277+
}
278+
string strSpeedPred = speedPredator.GetComponent<Text>().text;
279+
if (!string.IsNullOrEmpty(strSpeedPred))
280+
{
281+
this.maxSpeedPredator = Convert.ToDouble(strSpeedPred);
282+
}
283+
string strVisionPred = visionPredator.GetComponent<Text>().text;
284+
if (!string.IsNullOrEmpty(strVisionPred))
285+
{
286+
this.visionRadiusPredator = Convert.ToDouble(strVisionPred);
287+
}
288+
string strInitPredator = initialPredator.GetComponent<Text>().text;
289+
if (!string.IsNullOrEmpty(strInitPredator))
290+
{
291+
this.initialPopulationPredator = Convert.ToInt32(strInitPredator);
292+
}
293+
294+
//Prey parameters
295+
string strRepPrey = reprodPrey.GetComponent<Text>().text;
296+
if (!string.IsNullOrEmpty(strRepPrey))
297+
{
298+
this.reproductionPrey = Convert.ToDouble(strRepPrey);
299+
}
300+
string strSpeedPrey = speedPrey.GetComponent<Text>().text;
301+
if (!string.IsNullOrEmpty(strSpeedPrey))
302+
{
303+
this.maxSpeedPrey = Convert.ToDouble(strSpeedPrey);
304+
}
305+
string strVisionPrey = visionPrey.GetComponent<Text>().text;
306+
if (!string.IsNullOrEmpty(strVisionPrey))
307+
{
308+
this.visionRadiusPrey = Convert.ToDouble(strVisionPrey);
309+
}
310+
string strInitPrey = initialPrey.GetComponent<Text>().text;
311+
if (!string.IsNullOrEmpty(strInitPrey))
312+
{
313+
this.initialPopulationPrey = Convert.ToInt32(strInitPrey);
314+
}
315+
316+
//Plants parameter
317+
string strInitPlants = initPlants.GetComponent<Text>().text;
318+
if (!string.IsNullOrEmpty(strInitPlants))
319+
{
320+
this.initialPlants = Convert.ToDouble(strInitPlants);
321+
}
322+
string strGrowth = grRate.GetComponent<Text>().text;
323+
if (!string.IsNullOrEmpty(strGrowth))
324+
{
325+
this.growthRate = Convert.ToDouble(strGrowth);
326+
}
327+
string strThres = trhold.GetComponent<Text>().text;
328+
if (!string.IsNullOrEmpty(strThres))
329+
{
330+
this.threshold = Convert.ToDouble(strThres);
331+
}
332+
333+
//Strategy
334+
string strStrategy = stratMenu.GetComponent<Text>().text;
335+
if (!string.IsNullOrEmpty(strStrategy))
336+
{
337+
switch (strStrategy)
338+
{
339+
case "Estrategia simple":
340+
this.strategy = 0;
341+
break;
342+
case "Particle Swarm Optimization":
343+
this.strategy = 1;
344+
break;
345+
case "Grey Wolf Optimizer":
346+
this.strategy = 2;
347+
break;
348+
case "Whale Optimization Algorithm":
349+
this.strategy = 3;
350+
break;
351+
default:
352+
this.strategy = 0;
353+
break;
354+
}
355+
}
356+
string strIterations = itMenu.GetComponent<Text>().text;
357+
if (!string.IsNullOrEmpty(strIterations))
358+
{
359+
this.iterations = Convert.ToInt32(strIterations);
360+
}
361+
string strAgents = agentsMenu.GetComponent<Text>().text;
362+
if (!string.IsNullOrEmpty(strAgents))
363+
{
364+
this.agents = Convert.ToInt32(strAgents);
365+
}
366+
367+
Destroy(menu);
368+
}
215369
}
216370

217371

EcoSim/Assets/Controller/ControllerPrefab.prefab

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,11 @@ MonoBehaviour:
4343
m_Script: {fileID: 11500000, guid: 50fb9d235a90c4e4696ef79b8d3e5796, type: 3}
4444
m_Name:
4545
m_EditorClassIdentifier:
46+
ready: 1
47+
firstTime: 1
48+
menu: {fileID: 1936411909009720315, guid: 9be69d7278b611041bf4a75e19c11b48, type: 3}
49+
reprodPredator: {fileID: 0}
50+
speedPredator: {fileID: 0}
51+
visionPredator: {fileID: 0}
52+
initialPredator: {fileID: 0}
4653
MyView: {fileID: 2794609854212894643, guid: 8f63e7ba571ac8944916ce5938334baf, type: 3}

0 commit comments

Comments
 (0)