float getExplorationReward(GameObject infopoint) { if (infopointsVisited.Contains(infopoint)) { Debug.LogFormat("<color=red>infopoint already visited</color>"); return(visitedReward); } string sucess; infopointsVisited.Add(infopoint); infopointDynamicsScript = infopoint.GetComponent <infopointDynamics>(); sucess = infopointDynamicsScript.eatFood(gameObject.name.Substring(5)); Debug.LogFormat("<color=blue>{0} trying to eat {1} {2}</color>", gameObject.name, infopoint.name, sucess); if (sucess == "positive") { return(consumeReward); } else if (sucess == "negative") { return(-consumeReward); } else { return(0); } }
void updateFood() { //Debug.Log(foodCount + " " + totalFood); if (foodCount < totalFood) { Vector3 position = Vector3.zero; position.x = Random.Range(-range, range); position.z = Random.Range(-range, range); position.y = 0.9f; GameObject temp = Instantiate(foodSource, position, Quaternion.identity) as GameObject; temp.layer = 8; temp.tag = "info"; if (goodfoodCount >= badfoodCount) { infopointScript = temp.GetComponent <infopointDynamics>(); infopointScript.disposition = infopointDynamics.possibleDispositions.Bad; badfoodCount++; } else { goodfoodCount++; } foodCount++; generatedFood++; } }
void kill() { infopointDynamics infopointDynamicsscript = gameObject.GetComponent <infopointDynamics>(); if (!infopointDynamicsscript.isGoal) { Destroy(gameObject); movingFood movingFoodScript = camera.GetComponent <movingFood>(); movingFoodScript.foodCount--; } }
double[] getState(Vector3 position) { double[] featuresVector = new double[vectorSize]; numberOfAgents = 0; numberofGoodInfopoints = 0; numberofBadInfopoints = 0; double distance = 0f; numberOfAgentse = 0; numberOfAgentsi = 0; numberOfAgentsc = 0; numberOfAgentss = 0; //Debug.Log ("range: " + range); directionsState = new GameObject[numberOfDirections]; Collider[] cols = Physics.OverlapSphere(position, range); foreach (var colider in cols) { //Debug.Log(colider.tag); if (colider.tag == "agent") { /* * * float itemDistance = Vector3.Distance(transform.position, colider.transform.position); * * if (itemDistance > 0.1f) * { * * var AgentScript = colider.GetComponent<simulationAgentEncapsulator>(); * * if (AgentScript.assignedPersonality == simulationAgentEncapsulator.personalityType.Extroverted) * { * * numberOfAgentse++; * * } * else * { * * numberOfAgentsi++; * } * * if (AgentScript.assignedPersonalitySecond == simulationAgentEncapsulator.personalityTypeSecond.Cooperative) * { * * numberOfAgentsc++; * * } * else * { * * numberOfAgentss++; * } * * * } * * */ } else if (colider.tag == "info") { infopointDynamicsScript = colider.GetComponent <infopointDynamics>(); if (infopointDynamicsScript.disposition == infopointDynamics.possibleDispositions.Good) { numberofGoodInfopoints++; } else { numberofBadInfopoints++; } } distance = Vector3.Distance(position, colider.transform.position); if (distance > 0.2f && distance < range) { //Debug.Log(colider.tag + " " + distance); for (int i = 0; i < directionsMatrix.GetLength(0); i++) { if (TestRange(SignedAngleBetween(position, colider.transform.position), directionsMatrix[i, 0], directionsMatrix[i, 1])) { if (colider.tag == "info") { Debug.Log("f: " + i + " " + colider.tag + " distance: " + distance); } if (directionsState[i] != null) { if (distance < Vector3.Distance(transform.position, directionsState[i].transform.position)) { //Debug.Log (distance + " ---- " + Vector3.Distance (position, directionsState [i].transform.position)); directionsState[i] = colider.transform.gameObject; } } else { directionsState[i] = colider.transform.gameObject; } } } //Debug.Log (distance); //SignedAngleBetween (transform.position, colider.transform.position); } } int c = 0; foreach (var item in directionsState) { //Debug.Log (item); if (item != null) { float itemDistance = Vector3.Distance(position, item.transform.position); if (item.tag == "agent") { /* * * var AgentScript = item.GetComponent<simulationAgentEncapsulator>(); * * if (AgentScript.assignedPersonality == simulationAgentEncapsulator.personalityType.Extroverted) { * * featuresVector [matrixLength * c + 2] = 1 - (itemDistance - 0.2f) / (range - 0.2f); * * } else { * * featuresVector [matrixLength * c + 3] = 1 - (itemDistance - 0.2f) / (range - 0.2f); * * } * * if (AgentScript.assignedPersonalitySecond == simulationAgentEncapsulator.personalityTypeSecond.Cooperative) { * * featuresVector [matrixLength * c + 4] = 1 - (itemDistance - 0.2f) / (range - 0.2f); * * } else { * * featuresVector [matrixLength * c + 5] = 1 - (itemDistance - 0.2f) / (range - 0.2f); * * } * */ } else if (item.tag == "info") { //Debug.Log("info found"); infopointDynamicsScript = item.GetComponent <infopointDynamics>(); if (infopointDynamicsScript.disposition == infopointDynamics.possibleDispositions.Good) { featuresVector[matrixLength * c] = 1 - (itemDistance - 0.2f) / (range - 0.2f); } else { featuresVector[matrixLength * c + 1] = 1 - (itemDistance - 0.2f) / (range - 0.2f); } } } c++; } var divideNormalize = 10; /* * featuresVector [matrixSize] = (float) numberOfAgentse / divideNormalize; * * featuresVector [matrixSize + 1] = (float) numberOfAgentsi / divideNormalize; * * featuresVector [matrixSize + 2] = (float) numberOfAgentse / divideNormalize; * * featuresVector [matrixSize + 3] = (float) numberOfAgentsi / divideNormalize; * * * * * featuresVector [matrixSize + 4] = (float) numberofGoodInfopoints / divideNormalize; * * * featuresVector [matrixSize + 5] = (float) numberofBadInfopoints / divideNormalize; * */ /* * * if (agentEncapsulatorScript.assignedPersonality == simulationAgentEncapsulator.personalityType.Extroverted) { * * featuresVector [matrixSize + 3] = 1f; * * } else { * * featuresVector [matrixSize + 3] = 0.1f; * * } * * if (agentEncapsulatorScript.currentMood == simulationAgentEncapsulator.agentsMood.Positive) { * * featuresVector [matrixSize + 4] = 1f; * * } else if (agentEncapsulatorScript.currentMood == simulationAgentEncapsulator.agentsMood.Neutral) { * * * featuresVector [matrixSize + 4] = 0.5f; * * } else { * * * featuresVector [matrixSize + 4] = 0.1f; * * } * */ //Debug.Log (q); List <double> featuresFront = new List <double>(); featuresFront = featuresVector.ToList().GetRange(4, 24); printArrayDouble(featuresFront.ToArray()); //printArrayDouble(featuresVector); return(featuresVector); }
void statistics() { GameObject a = GameObject.FindGameObjectWithTag("agent"); AgentBehaviourScript = a.GetComponent <AgentBehaviour>(); numberOfDirections = AgentBehaviourScript.numberOfDirections; directionalFeatures = AgentBehaviourScript.stateMatrixFeatures.Length; onedimensionalFeatures = AgentBehaviourScript.onedimensionalFeatures.Length; GameObject[] agents = GameObject.FindGameObjectsWithTag("agent"); Debug.Log("agents " + agents.Length + " directional " + numberOfDirections * directionalFeatures + " onedimensional " + onedimensionalFeatures); //Time.timeScale = 0; //string buffer = "visited_info,unvisited_info,extraverted_agents,introverted_agents,total_agents,total_visited_info,total_unvisited_info,type" + System.Environment.NewLine; string buffer = "vi,uvi,ea,ia,ca,sa,tae,tai,tca,tsa,tvi,tuvi,good,bad,score,type" + System.Environment.NewLine; foreach (GameObject agent in agents) { AgentBehaviourScript = agent.GetComponent <AgentBehaviour>(); AgentEncapsulatorScript = agent.GetComponent <simulationAgentEncapsulator>(); double[] currentVector = AgentBehaviourScript.weightsVector; List <GameObject> infopointsVisited = AgentBehaviourScript.infopointsVisited; float totalScore = AgentBehaviourScript.totalScore; int goodInfo = 0; int badInfo = 0; foreach (var infopoint in infopointsVisited) { infopointScript = infopoint.GetComponent <infopointDynamics> (); if (infopointScript.disposition == infopointDynamics.possibleDispositions.Good) { goodInfo++; } else { badInfo++; } } string personality = ""; if (AgentEncapsulatorScript.assignedPersonality == simulationAgentEncapsulator.personalityType.Extroverted) { personality = "E"; } else { personality = "I"; } if (AgentEncapsulatorScript.assignedPersonalitySecond == simulationAgentEncapsulator.personalityTypeSecond.Cooperative) { personality += "C"; } else { personality += "S"; } printArrayDouble(currentVector); double bias = currentVector[currentVector.Length - 1]; Debug.Log(bias); for (int i = 0; i < currentVector.Length; i++) { currentVector[i] = currentVector[i] - bias; } printArrayDouble(currentVector); List <double> features = new List <double>(); for (int i = 0; i < directionalFeatures; i++) { //List<double> f = new List<double>(); double featureSum = 0f; for (int j = 0; j < numberOfDirections; j++) { //Debug.Log(currentVector[j * directionalFeatures + i]); featureSum += currentVector[j * directionalFeatures + i]; //f.Add(currentVector[j * directionalFeatures + i]); //f.Add(j * directionalFeatures + i); } features.Add(featureSum / numberOfDirections); } for (int i = 0; i < onedimensionalFeatures; i++) { features.Add(currentVector[numberOfDirections * directionalFeatures + i]); } features.Add(goodInfo); features.Add(badInfo); features.Add(totalScore); //Debug.Log(agent.name); string featureBuffer = ""; int count = 0; foreach (var feature in features) { count++; if (count < features.Count) { featureBuffer += feature + ","; } else { featureBuffer += feature; } } // if(!featureBuffer.Contains("0,0,0,0,0,0,0")) buffer += featureBuffer + "," + personality + System.Environment.NewLine; } writeData(buffer); }
void spawnInfopoints(int cubeNumber) { int range = 0; int quarter = Mathf.RoundToInt(cubeNumber / 4); if (selectedTerrain == terrainType.bad) { range = quarter; } else if (selectedTerrain == terrainType.medium) { range = quarter * 2; } else if (selectedTerrain == terrainType.good) { range = quarter * 3; } Debug.Log("Generated " + range + " good out of " + cubeNumber); infopointDynamics infopointScript = null; Vector3 v3T = Vector3.zero; Vector3[] arv3 = new Vector3[cubeNumber]; float fMinDist = 10.0f; // Minimum distance they need to be apart int iMaxTries = 1000; // Number of times to try to generate a position float fMinTry = Mathf.Infinity; int i; for (i = 0; i < cubeNumber; i++) { for (int j = 0; j < iMaxTries; j++) { v3T.x = Random.Range(currentGridBoundsMin, currentGridBoundsMax); v3T.z = Random.Range(currentGridBoundsMin, currentGridBoundsMax); v3T.y = 16; fMinTry = Mathf.Infinity; for (int k = 0; k < i; k++) { fMinTry = Mathf.Min((v3T - arv3[k]).magnitude, fMinTry); } if (fMinTry > fMinDist) // Far enough apart { break; } } if (fMinTry < fMinDist) { Debug.Log("Generation failed -- only found " + i + " points"); break; } arv3[i] = v3T; } for (int j = 0; j < i; j++) { GameObject go = Instantiate(prefabInfo, arv3[j], Quaternion.identity) as GameObject; go.layer = 8; go.tag = "info"; go.name = "info" + j; infopointScript = go.GetComponent <infopointDynamics>(); if (j <= range) { infopointScript.disposition = infopointDynamics.possibleDispositions.Good; } else { infopointScript.disposition = infopointDynamics.possibleDispositions.Bad; } } }
public double[] getRaycast(int maxDistance, int variables) { int numberOfRays = (int)360 / rayAngle; GameObject[] arrayOfHits; arrayOfHits = new GameObject [numberOfRays]; double[] stateSpace = new double[numberOfRays * variables]; for (int i = 0; i < stateSpace.Length; i++) { stateSpace[i] = 0f; } RaycastHit hit; int index = 0; while (index < numberOfRays) { //cast a ray in the current direction. if (Physics.Raycast(transform.position, transform.forward, out hit, maxDistance)) { Debug.DrawLine(transform.position, hit.point, Color.cyan); //Distance To the wall //distanceToWalls [index] = Vector3.Distance (transform.position, hit.point); arrayOfHits [index] = hit.transform.gameObject; } else { arrayOfHits [index] = null; } transform.Rotate(0, rayAngle, 0); index += 1; } int count = 0; foreach (var item in arrayOfHits) { if (item != null) { double itemDistance = Vector3.Distance(transform.position, item.transform.position); if (item.tag == "info") { infopointDynamicsScript = item.GetComponent <infopointDynamics>(); if (infopointDynamicsScript.disposition == infopointDynamics.possibleDispositions.Good) { stateSpace[variables * count] = 1 - (itemDistance - 1f) / (maxDistance - 1f); } else { stateSpace[variables * count + 1] = 1 - (itemDistance - 1f) / (maxDistance - 1f); } stateSpace[variables * count + 3] = item.GetComponent <Rigidbody>().velocity.x / 10; stateSpace[variables * count + 4] = item.GetComponent <Rigidbody>().velocity.z / 10; } else if (item.tag == "wall") { stateSpace[variables * count + 2] = 1 - (itemDistance - 1f) / (maxDistance - 1f); } // Debug.Log ("---------- " +item.transform.tag + " " + count + " " + distance); } count++; } //Debug.Log ("next"); //printArrayDouble(stateSpace); return(stateSpace); }
// Use this for initialization void Start() { singleAgent = Resources.Load("Prefabs/agent"); positions = new List <Vector3>(); Debug.Log(evolved.Length); agents = GameObject.FindGameObjectsWithTag("agent"); foodSource = Resources.Load("Prefabs/food"); for (int i = 0; i < agents.Length; i++) { positions.Add(agents[i].gameObject.transform.position); } Vector3 position = Vector3.zero; position.y = 0.9f; for (int i = 0; i < totalFood; i++) { position.x = Random.Range(-range, range); position.z = Random.Range(-range, range); GameObject temp = Instantiate(foodSource, position, Quaternion.identity) as GameObject; temp.layer = 8; temp.tag = "info"; if (i % 2 == 0) { infopointScript = temp.GetComponent <infopointDynamics>(); infopointScript.disposition = infopointDynamics.possibleDispositions.Bad; badfoodCount++; } else { goodfoodCount++; } foodCount = i; } InvokeRepeating("updateFood", 1f, 1f); loadEvolved(); /* * * AgentBehaviourRemote AgentBehaviourScript = agents[0].GetComponent<AgentBehaviourRemote>(); * * printListDouble(AgentBehaviourScript.downloadNet()); * * * * List<double> first = new List<double>(new double[] { 0.5, 1, 5, 9, 0.1 }); * * List<double> second = new List<double>(new double[] { 8, 3, 6, 0.9, 0.8 }); * * List<double> child = mating(first, second); * * List<double> child2 = mutation(first); * * Debug.Log("child"); * * printListDouble(child); * * Debug.Log("mutation"); * * printListDouble(child2); * */ }
// Use this for initialization void Start() { generatedFood = new List <GameObject>(); foodCells = new List <Cell>(); foodSource = Resources.Load("Prefabs/food"); mainGrid = GridManager.instance.GetGrid(new Vector3(0, 0, 0)); IEnumerable <Cell> allCells = mainGrid.cells; totalCells = mainGrid.sizeX * mainGrid.sizeZ; int totalFood = Mathf.RoundToInt((totalCells) / foodRatio); Debug.Log("Total " + totalFood + " out of " + totalCells); cellsClose = new List <Cell>(); cellList = allCells.ToList(); while (cellList.Count > totalFood) { //Debug.Log(cellList.Count); Cell current = cellList[Mathf.RoundToInt(Random.Range(0, cellList.Count))]; if (generateFood(current)) { cellList.Remove(current); cellList.Remove(current.GetNeighbour(0, 1)); cellList.Remove(current.GetNeighbour(0, -1)); cellList.Remove(current.GetNeighbour(-1, 0)); cellList.Remove(current.GetNeighbour(1, 0)); } } int quarter = Mathf.RoundToInt(generatedFood.Count / 4); if (selectedTerrain == terrainType.bad) { range = quarter; } else if (selectedTerrain == terrainType.medium) { range = quarter * 2; } else if (selectedTerrain == terrainType.good) { range = quarter * 3; } for (int i = 0; i < generatedFood.Count; i++) { generatedFood[i].layer = 8; generatedFood[i].tag = "info"; generatedFood[i].name = "info" + i; } int total = generatedFood.Count; range = generatedFood.Count; while (generatedFood.Count > range) { GameObject current = generatedFood[Mathf.RoundToInt(Random.Range(0, generatedFood.Count))]; infopointScript = current.GetComponent <infopointDynamics>(); infopointScript.disposition = infopointDynamics.possibleDispositions.Bad; generatedFood.Remove(current); } Debug.Log("Generated " + generatedFood.Count + " good out of " + total); }