SlideShare a Scribd company logo
1 of 2
Download to read offline
Given a scenario where an farmer agent walks around a meadow that is randomly generated with
a farmer, horse, pig, chicken and various obstacles like trees and boulders. does the following
scripts look okay? If not what may i need to change?
farmeragent.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.AI; using Unity.MLAgents; using Unity.MLAgents.Actuators; using
Unity.MLAgents.Sensors; public class FarmerAgent : Agent { public GameObject horse; public
GameObject pig; public GameObject chicken; public GameObject foodPrefab; public Transform
foodDropPoint; public NavMeshAgent navMeshAgent; private bool isHorseFed; private bool
isPigFed; private bool isChickenFed; private List<GameObject> foodObjects = new
List<GameObject>(); private void Start() { navMeshAgent = GetComponent<NavMeshAgent>(); }
public override void OnEpisodeBegin() { isHorseFed = false; isPigFed = false; isChickenFed =
false; // Reset animal hunger horse.GetComponent<AnimalAgent>().isFed = false;
pig.GetComponent<AnimalAgent>().isFed = false; chicken.GetComponent<AnimalAgent>().isFed
= false; // Destroy all food objects foreach (GameObject foodObject in foodObjects) {
Destroy(foodObject); } // Spawn new food objects for (int i = 0; i < 3; i++) { Vector3 randomPosition
= new Vector3(Random.Range(-5f, 5f), 0f, Random.Range(-5f, 5f)); GameObject foodObject =
Instantiate(foodPrefab, randomPosition + foodDropPoint.position, Quaternion.identity);
foodObjects.Add(foodObject); } // Move farmer to random position Vector3 farmerPosition = new
Vector3(Random.Range(-4f, 4f), 0f, Random.Range(-4f, 4f));
navMeshAgent.Warp(farmerPosition); } public override void CollectObservations(VectorSensor
sensor) { // Is each animal fed? sensor.AddObservation(isHorseFed);
sensor.AddObservation(isPigFed); sensor.AddObservation(isChickenFed); } public override void
OnActionReceived(ActionBuffers actions) { Vector3 position = transform.position; Vector3 forward
= transform.forward; float moveSpeed = 0f; float turnSpeed = 0f; int action =
actions.DiscreteActions[0]; switch (action) { case 0: // Move forward moveSpeed = 1f; break; case
1: // Move backward moveSpeed = -1f; break; case 2: // Turn left turnSpeed = -1f; break; case 3: //
Turn right turnSpeed = 1f; break; case 4: // Drop apple for horse DropFood(horse,
FoodType.Apple); break; case 5: // Drop truffle for pig DropFood(pig, FoodType.Truffle); break;
case 6: // Drop corn for chicken DropFood(chicken, FoodType.Corn); break; default: break; } //
Apply movement and rotation transform.Translate(moveSpeed * Time.deltaTime *
transform.forward, Space.World); transform.Rotate(0f, turnSpeed * Time.deltaTime * 100f, 0f); //
Update perception sensors horseSensor.UpdateSensor(); pigSensor.UpdateSensor();
chickenSensor.UpdateSensor(); } private void DropFood(GameObject animal, FoodType
foodType) { if (animal == null) { return; } Vector3 dropPosition = animal.transform.position +
Vector3.up; GameObject foodObject = Instantiate(foodPrefab, dropPosition, Quaternion.identity);
foodObject.GetComponent<Food>().foodType = foodType;
foodObject.GetComponent<Food>().targetAnimal = animal; } private void ResetFlags() {
isHorseFed = false; isPigFed = false; isChickenFed = false; } private void OnDrawGizmos() { if
(showPerceptionGizmos) { horseSensor.OnDrawGizmos(); pigSensor.OnDrawGizmos();
chickenSensor.OnDrawGizmos(); } } }
Animalagent.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.AI; public class AnimalAgent : Agent { public enum FoodType { Apple, Truffle,
Worm } public FoodType preferredFood; public bool isFed = false; private NavMeshAgent
navMeshAgent; private Transform targetTransform; private void Awake() { navMeshAgent =
GetComponent<NavMeshAgent>(); } public override void AgentReset() { isFed = false; } public
override void CollectObservations() { // No observations needed for the animal agents } public
override void AgentAction(float[] vectorAction, string textAction) { // No actions needed for the
animal agents } public void SetTarget(Transform target) { targetTransform = target;
MoveToTarget(targetTransform.position); } private void MoveToTarget(Vector3 targetPosition) {
navMeshAgent.SetDestination(targetPosition); } private void OnTriggerEnter(Collider other) { if
(other.CompareTag("Food")) { Food food = other.GetComponent<Food>(); if (food.foodType ==
preferredFood && !isFed) { isFed = true; AddReward(1.0f); } else { AddReward(-1.0f); }
food.Consume(); } else if (other.CompareTag("Wall") || other.CompareTag("Obstacle")) {
AddReward(-0.1f); } } }
meadowarea.cs using System.Collections.Generic; using UnityEngine; using Unity.MLAgents;
using Unity.MLAgents.Sensors; public class MeadowArea : MonoBehaviour { public GameObject
treePrefab; public GameObject rockPrefab; public GameObject foodPrefab; public int numTrees;
public int numRocks; public int numFood; public List<AnimalAgent> animalAgents; public
FarmerAgent farmerAgent; public float rangeX = 20f; public float rangeZ = 20f; private
List<GameObject> trees; private List<GameObject> rocks; private List<GameObject> food; public
void ResetArea() { PlaceObjects(numTrees, treePrefab, trees); PlaceObjects(numRocks,
rockPrefab, rocks); PlaceObjects(numFood, foodPrefab, food); farmerAgent.transform.position =
GetRandomSpawnPos(); farmerAgent.transform.rotation = Quaternion.Euler(0f,
Random.Range(0, 360), 0f); foreach (var animalAgent in animalAgents) {
animalAgent.transform.position = GetRandomSpawnPos(); animalAgent.transform.rotation =
Quaternion.Euler(0f, Random.Range(0, 360), 0f); } } private void Awake() { trees = new
List<GameObject>(); rocks = new List<GameObject>(); food = new List<GameObject>();
SpawnAgents(); } private void SpawnAgents() { foreach (var animalAgent in animalAgents) {
animalAgent.SetMeadowArea(this); animalAgent.transform.position = GetRandomSpawnPos();
animalAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f);
animalAgent.gameObject.SetActive(true); } farmerAgent.SetMeadowArea(this);
farmerAgent.transform.position = GetRandomSpawnPos(); farmerAgent.transform.rotation =
Quaternion.Euler(0f, Random.Range(0, 360), 0f); farmerAgent.gameObject.SetActive(true); }
private void PlaceObjects(int numObjects, GameObject prefab, List<GameObject> objectList) { for
(int i = 0; i < numObjects; i++) { GameObject spawnedObject = Instantiate(prefab,
GetRandomSpawnPos(), Quaternion.identity); objectList.Add(spawnedObject); } } private Vector3
GetRandomSpawnPos() { float xPos = Random.Range(-rangeX, rangeX); float zPos =
Random.Range(-rangeZ, rangeZ); Vector3 randomPos = new Vector3(xPos, 0f, zPos); return
randomPos; } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Food")) {
food.Remove(other.gameObject); Destroy(other.gameObject); } } private void
OnDrawGizmosSelected() { Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(transform.position, new Vector3(rangeX * 2f, 0f, rangeZ * 2f)); } }
fooditem.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
using Unity.MLAgents; public class FoodItem : MonoBehaviour { public Food foodType; }

More Related Content

Similar to Given a scenario where an farmer agent walks around a meadow.pdf

Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲吳錫修 (ShyiShiou Wu)
 
In this project you will define some interfaces, abstract classes, a.pdf
In this project you will define some interfaces, abstract classes, a.pdfIn this project you will define some interfaces, abstract classes, a.pdf
In this project you will define some interfaces, abstract classes, a.pdffathimaoptical
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performancejohndaviddalton
 
Leveraging jQuery's Special Events API (JSConf 2012)
Leveraging jQuery's Special Events API (JSConf 2012)Leveraging jQuery's Special Events API (JSConf 2012)
Leveraging jQuery's Special Events API (JSConf 2012)James Greene
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloRobert Nyman
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresRobert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileRobert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoRobert Nyman
 

Similar to Given a scenario where an farmer agent walks around a meadow.pdf (10)

Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
 
In this project you will define some interfaces, abstract classes, a.pdf
In this project you will define some interfaces, abstract classes, a.pdfIn this project you will define some interfaces, abstract classes, a.pdf
In this project you will define some interfaces, abstract classes, a.pdf
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
Leveraging jQuery's Special Events API (JSConf 2012)
Leveraging jQuery's Special Events API (JSConf 2012)Leveraging jQuery's Special Events API (JSConf 2012)
Leveraging jQuery's Special Events API (JSConf 2012)
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
 
Android workshop
Android workshopAndroid workshop
Android workshop
 

More from sales79

Which vitamin is produced by bacteria in the colon All of t.pdf
Which vitamin is produced by bacteria in the colon All of t.pdfWhich vitamin is produced by bacteria in the colon All of t.pdf
Which vitamin is produced by bacteria in the colon All of t.pdfsales79
 
The nurse is aware that particular populations are prone to .pdf
The nurse is aware that particular populations are prone to .pdfThe nurse is aware that particular populations are prone to .pdf
The nurse is aware that particular populations are prone to .pdfsales79
 
Three coins are dropped on a table a Let A be the event e.pdf
Three coins are dropped on a table a Let A be the event e.pdfThree coins are dropped on a table a Let A be the event e.pdf
Three coins are dropped on a table a Let A be the event e.pdfsales79
 
The purpose of this lab exercise is to review terminology co.pdf
The purpose of this lab exercise is to review terminology co.pdfThe purpose of this lab exercise is to review terminology co.pdf
The purpose of this lab exercise is to review terminology co.pdfsales79
 
Show and explainjustify your answers Use a formal approach.pdf
Show and explainjustify your answers Use a formal approach.pdfShow and explainjustify your answers Use a formal approach.pdf
Show and explainjustify your answers Use a formal approach.pdfsales79
 
Scores on the SAT form a normal distribution with 500 and .pdf
Scores on the SAT form a normal distribution with 500 and .pdfScores on the SAT form a normal distribution with 500 and .pdf
Scores on the SAT form a normal distribution with 500 and .pdfsales79
 
Requirement a How much is Zendas charitable contributions .pdf
Requirement a How much is Zendas charitable contributions .pdfRequirement a How much is Zendas charitable contributions .pdf
Requirement a How much is Zendas charitable contributions .pdfsales79
 
Put the following events into the correct order from earlie.pdf
Put the following events into the correct order from earlie.pdfPut the following events into the correct order from earlie.pdf
Put the following events into the correct order from earlie.pdfsales79
 
Pregunta 20 1 punto Los grados de pago son estructuras d.pdf
Pregunta 20 1 punto   Los grados de pago son estructuras d.pdfPregunta 20 1 punto   Los grados de pago son estructuras d.pdf
Pregunta 20 1 punto Los grados de pago son estructuras d.pdfsales79
 
Percance de mala interpretacin jocelyn bronceado Tony es.pdf
Percance de mala interpretacin   jocelyn bronceado  Tony es.pdfPercance de mala interpretacin   jocelyn bronceado  Tony es.pdf
Percance de mala interpretacin jocelyn bronceado Tony es.pdfsales79
 
Answer should be in MIPS please Please ensure you are follo.pdf
Answer should be in MIPS please Please ensure you are follo.pdfAnswer should be in MIPS please Please ensure you are follo.pdf
Answer should be in MIPS please Please ensure you are follo.pdfsales79
 
Amber laves to solse cuzzles At first glance a new project.pdf
Amber laves to solse cuzzles At first glance a new project.pdfAmber laves to solse cuzzles At first glance a new project.pdf
Amber laves to solse cuzzles At first glance a new project.pdfsales79
 
Heritage Ltd was organized on January 2 2020 The followin.pdf
Heritage Ltd was organized on January 2 2020 The followin.pdfHeritage Ltd was organized on January 2 2020 The followin.pdf
Heritage Ltd was organized on January 2 2020 The followin.pdfsales79
 
Bonita Corporation issued 700 shares of nopar common stock .pdf
Bonita Corporation issued 700 shares of nopar common stock .pdfBonita Corporation issued 700 shares of nopar common stock .pdf
Bonita Corporation issued 700 shares of nopar common stock .pdfsales79
 
Dozier Corporation is a fastgrowing supplier of office prod.pdf
Dozier Corporation is a fastgrowing supplier of office prod.pdfDozier Corporation is a fastgrowing supplier of office prod.pdf
Dozier Corporation is a fastgrowing supplier of office prod.pdfsales79
 
1 Huntington disease HD is a genetic disorder caused by a.pdf
1 Huntington disease HD is a genetic disorder caused by a.pdf1 Huntington disease HD is a genetic disorder caused by a.pdf
1 Huntington disease HD is a genetic disorder caused by a.pdfsales79
 
13 Multiple choice 1Computers and the digital revolution .pdf
13  Multiple choice 1Computers and the digital revolution .pdf13  Multiple choice 1Computers and the digital revolution .pdf
13 Multiple choice 1Computers and the digital revolution .pdfsales79
 
3 Let F be a measure space and let fg be positive mea.pdf
3 Let F be a measure space and let fg be positive mea.pdf3 Let F be a measure space and let fg be positive mea.pdf
3 Let F be a measure space and let fg be positive mea.pdfsales79
 
3 10 points Construct NFAs for the following regular exp.pdf
3 10 points Construct NFAs for the following regular exp.pdf3 10 points Construct NFAs for the following regular exp.pdf
3 10 points Construct NFAs for the following regular exp.pdfsales79
 
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdfsales79
 

More from sales79 (20)

Which vitamin is produced by bacteria in the colon All of t.pdf
Which vitamin is produced by bacteria in the colon All of t.pdfWhich vitamin is produced by bacteria in the colon All of t.pdf
Which vitamin is produced by bacteria in the colon All of t.pdf
 
The nurse is aware that particular populations are prone to .pdf
The nurse is aware that particular populations are prone to .pdfThe nurse is aware that particular populations are prone to .pdf
The nurse is aware that particular populations are prone to .pdf
 
Three coins are dropped on a table a Let A be the event e.pdf
Three coins are dropped on a table a Let A be the event e.pdfThree coins are dropped on a table a Let A be the event e.pdf
Three coins are dropped on a table a Let A be the event e.pdf
 
The purpose of this lab exercise is to review terminology co.pdf
The purpose of this lab exercise is to review terminology co.pdfThe purpose of this lab exercise is to review terminology co.pdf
The purpose of this lab exercise is to review terminology co.pdf
 
Show and explainjustify your answers Use a formal approach.pdf
Show and explainjustify your answers Use a formal approach.pdfShow and explainjustify your answers Use a formal approach.pdf
Show and explainjustify your answers Use a formal approach.pdf
 
Scores on the SAT form a normal distribution with 500 and .pdf
Scores on the SAT form a normal distribution with 500 and .pdfScores on the SAT form a normal distribution with 500 and .pdf
Scores on the SAT form a normal distribution with 500 and .pdf
 
Requirement a How much is Zendas charitable contributions .pdf
Requirement a How much is Zendas charitable contributions .pdfRequirement a How much is Zendas charitable contributions .pdf
Requirement a How much is Zendas charitable contributions .pdf
 
Put the following events into the correct order from earlie.pdf
Put the following events into the correct order from earlie.pdfPut the following events into the correct order from earlie.pdf
Put the following events into the correct order from earlie.pdf
 
Pregunta 20 1 punto Los grados de pago son estructuras d.pdf
Pregunta 20 1 punto   Los grados de pago son estructuras d.pdfPregunta 20 1 punto   Los grados de pago son estructuras d.pdf
Pregunta 20 1 punto Los grados de pago son estructuras d.pdf
 
Percance de mala interpretacin jocelyn bronceado Tony es.pdf
Percance de mala interpretacin   jocelyn bronceado  Tony es.pdfPercance de mala interpretacin   jocelyn bronceado  Tony es.pdf
Percance de mala interpretacin jocelyn bronceado Tony es.pdf
 
Answer should be in MIPS please Please ensure you are follo.pdf
Answer should be in MIPS please Please ensure you are follo.pdfAnswer should be in MIPS please Please ensure you are follo.pdf
Answer should be in MIPS please Please ensure you are follo.pdf
 
Amber laves to solse cuzzles At first glance a new project.pdf
Amber laves to solse cuzzles At first glance a new project.pdfAmber laves to solse cuzzles At first glance a new project.pdf
Amber laves to solse cuzzles At first glance a new project.pdf
 
Heritage Ltd was organized on January 2 2020 The followin.pdf
Heritage Ltd was organized on January 2 2020 The followin.pdfHeritage Ltd was organized on January 2 2020 The followin.pdf
Heritage Ltd was organized on January 2 2020 The followin.pdf
 
Bonita Corporation issued 700 shares of nopar common stock .pdf
Bonita Corporation issued 700 shares of nopar common stock .pdfBonita Corporation issued 700 shares of nopar common stock .pdf
Bonita Corporation issued 700 shares of nopar common stock .pdf
 
Dozier Corporation is a fastgrowing supplier of office prod.pdf
Dozier Corporation is a fastgrowing supplier of office prod.pdfDozier Corporation is a fastgrowing supplier of office prod.pdf
Dozier Corporation is a fastgrowing supplier of office prod.pdf
 
1 Huntington disease HD is a genetic disorder caused by a.pdf
1 Huntington disease HD is a genetic disorder caused by a.pdf1 Huntington disease HD is a genetic disorder caused by a.pdf
1 Huntington disease HD is a genetic disorder caused by a.pdf
 
13 Multiple choice 1Computers and the digital revolution .pdf
13  Multiple choice 1Computers and the digital revolution .pdf13  Multiple choice 1Computers and the digital revolution .pdf
13 Multiple choice 1Computers and the digital revolution .pdf
 
3 Let F be a measure space and let fg be positive mea.pdf
3 Let F be a measure space and let fg be positive mea.pdf3 Let F be a measure space and let fg be positive mea.pdf
3 Let F be a measure space and let fg be positive mea.pdf
 
3 10 points Construct NFAs for the following regular exp.pdf
3 10 points Construct NFAs for the following regular exp.pdf3 10 points Construct NFAs for the following regular exp.pdf
3 10 points Construct NFAs for the following regular exp.pdf
 
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf
1 Escenario C Gabriel director ejecutivo de Chesapeake Fi.pdf
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Given a scenario where an farmer agent walks around a meadow.pdf

  • 1. Given a scenario where an farmer agent walks around a meadow that is randomly generated with a farmer, horse, pig, chicken and various obstacles like trees and boulders. does the following scripts look okay? If not what may i need to change? farmeragent.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; public class FarmerAgent : Agent { public GameObject horse; public GameObject pig; public GameObject chicken; public GameObject foodPrefab; public Transform foodDropPoint; public NavMeshAgent navMeshAgent; private bool isHorseFed; private bool isPigFed; private bool isChickenFed; private List<GameObject> foodObjects = new List<GameObject>(); private void Start() { navMeshAgent = GetComponent<NavMeshAgent>(); } public override void OnEpisodeBegin() { isHorseFed = false; isPigFed = false; isChickenFed = false; // Reset animal hunger horse.GetComponent<AnimalAgent>().isFed = false; pig.GetComponent<AnimalAgent>().isFed = false; chicken.GetComponent<AnimalAgent>().isFed = false; // Destroy all food objects foreach (GameObject foodObject in foodObjects) { Destroy(foodObject); } // Spawn new food objects for (int i = 0; i < 3; i++) { Vector3 randomPosition = new Vector3(Random.Range(-5f, 5f), 0f, Random.Range(-5f, 5f)); GameObject foodObject = Instantiate(foodPrefab, randomPosition + foodDropPoint.position, Quaternion.identity); foodObjects.Add(foodObject); } // Move farmer to random position Vector3 farmerPosition = new Vector3(Random.Range(-4f, 4f), 0f, Random.Range(-4f, 4f)); navMeshAgent.Warp(farmerPosition); } public override void CollectObservations(VectorSensor sensor) { // Is each animal fed? sensor.AddObservation(isHorseFed); sensor.AddObservation(isPigFed); sensor.AddObservation(isChickenFed); } public override void OnActionReceived(ActionBuffers actions) { Vector3 position = transform.position; Vector3 forward = transform.forward; float moveSpeed = 0f; float turnSpeed = 0f; int action = actions.DiscreteActions[0]; switch (action) { case 0: // Move forward moveSpeed = 1f; break; case 1: // Move backward moveSpeed = -1f; break; case 2: // Turn left turnSpeed = -1f; break; case 3: // Turn right turnSpeed = 1f; break; case 4: // Drop apple for horse DropFood(horse, FoodType.Apple); break; case 5: // Drop truffle for pig DropFood(pig, FoodType.Truffle); break; case 6: // Drop corn for chicken DropFood(chicken, FoodType.Corn); break; default: break; } // Apply movement and rotation transform.Translate(moveSpeed * Time.deltaTime * transform.forward, Space.World); transform.Rotate(0f, turnSpeed * Time.deltaTime * 100f, 0f); // Update perception sensors horseSensor.UpdateSensor(); pigSensor.UpdateSensor(); chickenSensor.UpdateSensor(); } private void DropFood(GameObject animal, FoodType foodType) { if (animal == null) { return; } Vector3 dropPosition = animal.transform.position + Vector3.up; GameObject foodObject = Instantiate(foodPrefab, dropPosition, Quaternion.identity); foodObject.GetComponent<Food>().foodType = foodType; foodObject.GetComponent<Food>().targetAnimal = animal; } private void ResetFlags() { isHorseFed = false; isPigFed = false; isChickenFed = false; } private void OnDrawGizmos() { if (showPerceptionGizmos) { horseSensor.OnDrawGizmos(); pigSensor.OnDrawGizmos(); chickenSensor.OnDrawGizmos(); } } } Animalagent.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class AnimalAgent : Agent { public enum FoodType { Apple, Truffle,
  • 2. Worm } public FoodType preferredFood; public bool isFed = false; private NavMeshAgent navMeshAgent; private Transform targetTransform; private void Awake() { navMeshAgent = GetComponent<NavMeshAgent>(); } public override void AgentReset() { isFed = false; } public override void CollectObservations() { // No observations needed for the animal agents } public override void AgentAction(float[] vectorAction, string textAction) { // No actions needed for the animal agents } public void SetTarget(Transform target) { targetTransform = target; MoveToTarget(targetTransform.position); } private void MoveToTarget(Vector3 targetPosition) { navMeshAgent.SetDestination(targetPosition); } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Food")) { Food food = other.GetComponent<Food>(); if (food.foodType == preferredFood && !isFed) { isFed = true; AddReward(1.0f); } else { AddReward(-1.0f); } food.Consume(); } else if (other.CompareTag("Wall") || other.CompareTag("Obstacle")) { AddReward(-0.1f); } } } meadowarea.cs using System.Collections.Generic; using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; public class MeadowArea : MonoBehaviour { public GameObject treePrefab; public GameObject rockPrefab; public GameObject foodPrefab; public int numTrees; public int numRocks; public int numFood; public List<AnimalAgent> animalAgents; public FarmerAgent farmerAgent; public float rangeX = 20f; public float rangeZ = 20f; private List<GameObject> trees; private List<GameObject> rocks; private List<GameObject> food; public void ResetArea() { PlaceObjects(numTrees, treePrefab, trees); PlaceObjects(numRocks, rockPrefab, rocks); PlaceObjects(numFood, foodPrefab, food); farmerAgent.transform.position = GetRandomSpawnPos(); farmerAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f); foreach (var animalAgent in animalAgents) { animalAgent.transform.position = GetRandomSpawnPos(); animalAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f); } } private void Awake() { trees = new List<GameObject>(); rocks = new List<GameObject>(); food = new List<GameObject>(); SpawnAgents(); } private void SpawnAgents() { foreach (var animalAgent in animalAgents) { animalAgent.SetMeadowArea(this); animalAgent.transform.position = GetRandomSpawnPos(); animalAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f); animalAgent.gameObject.SetActive(true); } farmerAgent.SetMeadowArea(this); farmerAgent.transform.position = GetRandomSpawnPos(); farmerAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f); farmerAgent.gameObject.SetActive(true); } private void PlaceObjects(int numObjects, GameObject prefab, List<GameObject> objectList) { for (int i = 0; i < numObjects; i++) { GameObject spawnedObject = Instantiate(prefab, GetRandomSpawnPos(), Quaternion.identity); objectList.Add(spawnedObject); } } private Vector3 GetRandomSpawnPos() { float xPos = Random.Range(-rangeX, rangeX); float zPos = Random.Range(-rangeZ, rangeZ); Vector3 randomPos = new Vector3(xPos, 0f, zPos); return randomPos; } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Food")) { food.Remove(other.gameObject); Destroy(other.gameObject); } } private void OnDrawGizmosSelected() { Gizmos.color = Color.yellow; Gizmos.DrawWireCube(transform.position, new Vector3(rangeX * 2f, 0f, rangeZ * 2f)); } } fooditem.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.MLAgents; public class FoodItem : MonoBehaviour { public Food foodType; }