Skip to content

Commit ec2f0de

Browse files
authored
Merge pull request #14 from Dacarpe03/developApp
Phase 1 finished
2 parents 3020d6e + 87b3076 commit ec2f0de

File tree

238 files changed

+17046
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

238 files changed

+17046
-67
lines changed

EcoSim/Assets/Controller.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using UnityEngine;
7+
8+
public class Controller : MonoBehaviour
9+
{
10+
//PATHS FOR FILES
11+
private string PATH;
12+
private string _currentFileName;
13+
//END PATHS FOR FILES
14+
15+
16+
//PARAMETERS OF SIMULATION
17+
private int NUMBER_OF_SIMULATIONS = 500;
18+
private int ITERATIONS_PER_SIMULATION = 100;
19+
20+
private double PREY_REPRODUCTION_PROB = 1;
21+
private double PREDATOR_REPRODUCTION_PROB = 1;
22+
23+
private double PREY_MAX_SPEED = 0.5;
24+
private double PREDATOR_MAX_SPEED = 0.6;
25+
26+
private double PREY_VISION_RADIUS = 8;
27+
private double PREDATOR_VISION_RADIUS = 15;
28+
29+
private int PREY_GROUP_SIZE = 500 ;
30+
private int PREDATOR_GROUP_SIZE = 15;
31+
//END PARAMETERS OF SIMULATION
32+
33+
//COUNTER PARAMETERS
34+
private int _simulationCounter = 1;
35+
//END COUNTER PARAMETERS
36+
37+
//MAIN ATTRIBUTES
38+
private Ecosystem _ecosystem;
39+
40+
public View MyView;
41+
private View _myView;
42+
//END MAIN ATTRIBUTES
43+
44+
void Start()
45+
{
46+
Debug.Log("Simulación " + this._simulationCounter);
47+
this._ecosystem = new Ecosystem(PREY_GROUP_SIZE, PREY_MAX_SPEED, PREY_VISION_RADIUS, PREY_REPRODUCTION_PROB, PREDATOR_GROUP_SIZE, PREDATOR_MAX_SPEED, PREDATOR_VISION_RADIUS, PREDATOR_REPRODUCTION_PROB);
48+
this._myView = Instantiate(MyView);
49+
this._myView.Initialize(PREY_GROUP_SIZE, PREDATOR_GROUP_SIZE);
50+
51+
this.PATH = Application.dataPath + "/SimulationData/";
52+
this.CreateFileForSimulation();
53+
}
54+
55+
// Update is called once per frame
56+
void Update()
57+
{
58+
if (this._ecosystem.Iteration < ITERATIONS_PER_SIMULATION & !this._ecosystem.Extinguised)
59+
{
60+
61+
if (this._ecosystem.Reset)
62+
{
63+
this.UpdateFile();
64+
this._ecosystem.Update();
65+
this.ResetView();
66+
}
67+
else
68+
{
69+
this._ecosystem.Update();
70+
List<Vector3> preyModelPositions = this.GetModelPositions(this._ecosystem.Preys);
71+
List<Vector3> predatorModelPositions = this.GetModelPositions(this._ecosystem.Predators);
72+
this._myView.UpdatePositions(preyModelPositions, predatorModelPositions);
73+
}
74+
}
75+
else if (this._simulationCounter < this.NUMBER_OF_SIMULATIONS)
76+
{
77+
this._simulationCounter++;
78+
Debug.Log("Simulación " + this._simulationCounter);
79+
this._ecosystem = new Ecosystem(PREY_GROUP_SIZE, PREY_MAX_SPEED, PREY_VISION_RADIUS, PREY_REPRODUCTION_PROB, PREDATOR_GROUP_SIZE, PREDATOR_MAX_SPEED, PREDATOR_VISION_RADIUS, PREDATOR_REPRODUCTION_PROB);
80+
this.ResetView();
81+
this.CreateFileForSimulation();
82+
}
83+
else
84+
{
85+
Debug.Log("Fin de las simulaciones");
86+
Destroy(this.gameObject);
87+
}
88+
}
89+
90+
public List<Vector3> GetModelPositions(AnimalGroup modelGroup)
91+
{
92+
List<Vec3> modelPositions = modelGroup.GetPositions();
93+
List<Vector3> viewPositions = this.TransformToVector3(modelPositions);
94+
95+
return viewPositions;
96+
}
97+
98+
public List<Vector3> TransformToVector3(List<Vec3> vectors)
99+
{
100+
List<Vector3> newVectors = new List<Vector3>();
101+
foreach(Vec3 v in vectors)
102+
{
103+
Vector3 vNew = new Vector3((float)v.XCoord, (float)v.YCoord, (float)v.ZCoord);
104+
newVectors.Add(vNew);
105+
}
106+
107+
return newVectors;
108+
}
109+
110+
public void ResetView()
111+
{
112+
this._myView.Reset();
113+
Destroy(this._myView.gameObject);
114+
this._myView = Instantiate(MyView);
115+
this._myView.Initialize(this._ecosystem.Preys.Size, this._ecosystem.Predators.Size);
116+
}
117+
118+
public void CreateFileForSimulation()
119+
{
120+
var dt = DateTime.Now;
121+
string date = dt.ToString("MM-dd-yyyy-hh-mm");
122+
string fileName = date + "-simulation" + this._simulationCounter + ".txt";
123+
this._currentFileName = this.PATH + fileName;
124+
if (!File.Exists(this._currentFileName))
125+
{
126+
StreamWriter sr = File.CreateText(_currentFileName);
127+
sr.WriteLine("Parameters (in next line): Iterations|PreyReproductionRate|PreyVisionRadius|PreyMaxSpeed|PredatorReproductionRate|PredatorVisionRadius|PredatorMaxSpeed");
128+
sr.WriteLine(this.ITERATIONS_PER_SIMULATION
129+
+ "||" + this.PREY_REPRODUCTION_PROB
130+
+ "||" + this.PREY_VISION_RADIUS
131+
+ "||" + this.PREY_MAX_SPEED
132+
+ "||" + this.PREDATOR_REPRODUCTION_PROB
133+
+ "||" + this.PREDATOR_VISION_RADIUS
134+
+ "||" + this.PREDATOR_MAX_SPEED);
135+
136+
sr.WriteLine("Iteracion|InicialPresas|InicialPredadores");
137+
sr.WriteLine("Iteracion|SupervivientesPresas|SupervivientesPredadores");
138+
sr.Close();
139+
}
140+
}
141+
142+
public void UpdateFile()
143+
{
144+
if (File.Exists(this._currentFileName))
145+
{
146+
StreamWriter sr = File.AppendText(_currentFileName);
147+
sr.WriteLine(this._ecosystem.Iteration + "||" + this._ecosystem.Preys.Size + "||" + this._ecosystem.Predators.Size);
148+
sr.WriteLine(this._ecosystem.Iteration + "||" + this._ecosystem.Preys.SurvivorsNumber + "||" + this._ecosystem.Predators.SurvivorsNumber);
149+
sr.Close();
150+
}
151+
}
152+
}
153+
154+

EcoSim/Assets/Controller/Controller.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &8465234516850191603
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
serializedVersion: 6
10+
m_Component:
11+
- component: {fileID: 4893672450304702451}
12+
- component: {fileID: 5670853230243482470}
13+
m_Layer: 0
14+
m_Name: ControllerPrefab
15+
m_TagString: Untagged
16+
m_Icon: {fileID: 0}
17+
m_NavMeshLayer: 0
18+
m_StaticEditorFlags: 0
19+
m_IsActive: 1
20+
--- !u!4 &4893672450304702451
21+
Transform:
22+
m_ObjectHideFlags: 0
23+
m_CorrespondingSourceObject: {fileID: 0}
24+
m_PrefabInstance: {fileID: 0}
25+
m_PrefabAsset: {fileID: 0}
26+
m_GameObject: {fileID: 8465234516850191603}
27+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
28+
m_LocalPosition: {x: 0, y: 0, z: 0}
29+
m_LocalScale: {x: 1, y: 1, z: 1}
30+
m_Children: []
31+
m_Father: {fileID: 0}
32+
m_RootOrder: 0
33+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
34+
--- !u!114 &5670853230243482470
35+
MonoBehaviour:
36+
m_ObjectHideFlags: 0
37+
m_CorrespondingSourceObject: {fileID: 0}
38+
m_PrefabInstance: {fileID: 0}
39+
m_PrefabAsset: {fileID: 0}
40+
m_GameObject: {fileID: 8465234516850191603}
41+
m_Enabled: 1
42+
m_EditorHideFlags: 0
43+
m_Script: {fileID: 11500000, guid: 50fb9d235a90c4e4696ef79b8d3e5796, type: 3}
44+
m_Name:
45+
m_EditorClassIdentifier:
46+
MyView: {fileID: 2794609854212894643, guid: 8f63e7ba571ac8944916ce5938334baf, type: 3}

EcoSim/Assets/Controller/ControllerPrefab.prefab.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EcoSim/Assets/Model/Animal.cs

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
using System;
2+
using System.Collections.Generic;
23

34
public class Animal
45
{
56
//SECTION: Attributes and properties
7+
private const double STEER_FORCE = 0.7;
8+
69
private int _id;
7-
public int ID { get => _id; }
10+
public int Id { get => _id; }
811

912
private AnimalState _state;
1013
public AnimalState State { get => _state; set => _state = value; }
1114

12-
private float _maxSpeed;
13-
private float _maxSquareSpeed; //So that the computation of the norm of the vector skips one step, the sqrt
15+
private double _maxSpeed;
16+
public double MaxSpeed { get => _maxSpeed; }
17+
private double _maxSquaredSpeed; //So that the computation of the norm of the vector skips one step, the sqrt
18+
public double MaxSquaredSpeed { get => _maxSquaredSpeed; }
1419

20+
private double _visionRadius;
21+
private double _squaredVisionRadius;
22+
public double SquaredVisionRadius { get => _squaredVisionRadius; }
23+
1524
private Boolean _isSafe;
1625
public Boolean IsSafe { get => _isSafe; set => _isSafe = value; }
1726

27+
private Boolean _isDead = false;
28+
public Boolean IsDead { get => _isDead; set => _isDead = value; }
29+
1830
private Vec3 _position;
1931
public Vec3 Position { get => _position; set => _position = value; }
2032

@@ -24,13 +36,22 @@ public class Animal
2436
//END: Attributes and properties
2537

2638
//SECTION: Constructor and main methods
27-
public Animal(AnimalState state, float maxSpeed, int id)
39+
public Animal(AnimalState state, double maxSpeed, double visionRadius, int id, Random rand)
2840
{
2941
this.TransitionTo(state);
42+
3043
this._isSafe = false;
44+
3145
this._maxSpeed = maxSpeed;
32-
this._maxSquareSpeed = maxSpeed * maxSpeed;
33-
this._position = new Vec3();
46+
this._maxSquaredSpeed = maxSpeed * maxSpeed;
47+
48+
this._visionRadius = visionRadius;
49+
this._squaredVisionRadius = visionRadius * visionRadius;
50+
51+
this._position = new Vec3(rand);
52+
this._speed = new Vec3(rand);
53+
this._speed.Trim(this._maxSquaredSpeed);
54+
3455
this._id = id;
3556
}
3657

@@ -40,9 +61,52 @@ public void TransitionTo(AnimalState newState)
4061
this._state.Agent = this;
4162
}
4263

43-
public void ResetPosition()
64+
public void Move()
4465
{
45-
this._position.RandomizeCoords();
66+
this._position.Add(Speed);
4667
}
68+
69+
public void UpdateSpeed(Vec3 acceleration)
70+
{
71+
if (!acceleration.IsZero())
72+
{
73+
Vec3 newSpeed = Vec3.Zero();
74+
75+
this._speed.Multiply(STEER_FORCE);
76+
newSpeed.Add(this._speed);
77+
78+
acceleration.Multiply(1 - STEER_FORCE);
79+
newSpeed.Add(acceleration);
80+
81+
newSpeed.Trim(MaxSquaredSpeed);
82+
this._speed = newSpeed;
83+
}
84+
}
85+
4786
//END: Constructor and main methods
87+
88+
//SECTION: Secondary methods
89+
public Boolean InDanger(List<Animal> foes)
90+
{
91+
foreach(Animal a in foes)
92+
{
93+
if(this.SquareDistanceTo(a) < this._squaredVisionRadius)
94+
{
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
101+
public double SquareDistanceTo(Animal other)
102+
{
103+
return this._position.SquaredDistanceTo(other.Position);
104+
}
105+
106+
public void ResetPosition(Random rand)
107+
{
108+
this._isSafe = false;
109+
this._position.RandomizeCoords(rand);
110+
}
111+
//END: Secondary methods
48112
}

0 commit comments

Comments
 (0)