private void CognitiveCycle(object obj) { Console.WriteLine("Starting Cognitive Cycle ... press CTRL-C to finish !"); // Cognitive Cycle starts here getting sensorial information while (CurrentCognitiveCycle != MaxNumberOfCognitiveCycles) { // Get current sensory information IList <Thing> currentSceneInWS3D = processSensoryInformation(); // Make the perception SensoryInformation si = prepareSensoryInformation(currentSceneInWS3D); //Perceive the sensory information CurrentAgent.Perceive(si); //Choose an action ExternalActionChunk chosen = CurrentAgent.GetChosenExternalAction(si); // Get the selected action String actionLabel = chosen.LabelAsIComparable.ToString(); CreatureActions actionType = (CreatureActions)Enum.Parse(typeof(CreatureActions), actionLabel, true); // Call the output event handler processSelectedAction(actionType); // Increment the number of cognitive cycles CurrentCognitiveCycle++; //Wait to the agent accomplish his job if (TimeBetweenCognitiveCycles > 0) { Thread.Sleep(TimeBetweenCognitiveCycles); } } }
/// <summary> /// Performs reasoning using a "noisy" input based on each pattern /// </summary> /// <param name="reasoner">The reasoner who is performing the reasoning</param> static void Run(Agent reasoner) { int pcounter = 0; //Iterates through each pattern foreach (DeclarativeChunk dc in chunks) { //Gets an input to use for reasoning. Note that the World.GetSensoryInformation method can also be used here ExternalActionChunk chosen = null; ++pcounter; Console.Write("Presenting degraded pattern "); Console.WriteLine(pcounter); int state_counter = 1; while (chosen == null || chosen == ExternalActionChunk.DO_NOTHING) { SensoryInformation si = World.NewSensoryInformation(reasoner); si.Add(World.GetDimensionValuePair("state", state_counter), 1); int count = 0; //Sets up the input foreach (DimensionValuePair dv in dvs) { if (((double)count / (double)dc.Count < (1 - noise))) { if (dc.Contains(dv)) { si.Add(dv, 1); ++count; } else { si.Add(dv, 0); } } else { si.Add(dv, 0); //Zeros out the dimension-value pair if "above the noise level" } } reasoner.Perceive(si); chosen = reasoner.GetChosenExternalAction(si); if (reasoner.GetInternals(Agent.InternalWorldObjectContainers.WORKING_MEMORY).Count() > 0) { state_counter = 3; } else { state_counter = 2; } } Console.Write("Is this pattern 2? Agent says: "); Console.WriteLine(chosen.LabelAsIComparable); reasoner.ResetWorkingMemory(); } }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); targetThing = listOfThings.Where(item => (item.CategoryId != Thing.CATEGORY_CREATURE && item.CategoryId != Thing.CATEGORY_BRICK)).OrderBy(x => x.DistanceToCreature).FirstOrDefault(); List <Leaflet> leaflets = c.getLeaflets(); // Detect if we have a wall ahead //Boolean wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= 61)).Any(); //double wallAheadActivationValue = wallAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; Boolean foodAhead = targetThing == null ? false : targetThing.CategoryId == Thing.categoryPFOOD || targetThing.CategoryId == Thing.CATEGORY_NPFOOD; double foodAheadActivationValue = foodAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; Boolean leafletJewelAhead = targetThing == null ? false : targetThing.CategoryId == Thing.CATEGORY_JEWEL && hasToGetJewelForLeaflet(targetThing.Material.Color, leaflets.FirstOrDefault()); double leafletJewelAheadActivationValue = leafletJewelAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; Boolean nonLeafletJewelAhead = targetThing == null ? false : leafletJewelAhead == false && targetThing.CategoryId == Thing.CATEGORY_JEWEL; double nonLeafletJewelAheadActivationValue = nonLeafletJewelAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; Boolean closeObjectAhead = targetThing == null ? false : targetThing.DistanceToCreature < 40; double closeObjectAheadActivationValue = closeObjectAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; Boolean hasCompletedLeaflet = leaflets[0].situation; double hasCompletedLeafletActivationValue = hasCompletedLeaflet ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; si.Add(inputFoodAhead, foodAheadActivationValue); si.Add(inputLeafletJewelAhead, leafletJewelAheadActivationValue); si.Add(inputNonLeafletJewelAhead, nonLeafletJewelAheadActivationValue); si.Add(inputCloseObjectAhead, closeObjectAheadActivationValue); si.Add(inputHasCompletedLeaflet, hasCompletedLeafletActivationValue); //Console.WriteLine(sensorialInformation); int n = 0; foreach (Leaflet l in c.getLeaflets()) { //mind.updateLeaflet(n,l); n++; } //mind.update(); leaflets.First().PrintLeaflet(0); return(si); }
private void CognitiveCycle(object obj) { Console.WriteLine("Starting Cognitive Cycle ... press CTRL-C to finish !"); // Cognitive Cycle starts here getting sensorial information var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here while (CurrentCognitiveCycle != MaxNumberOfCognitiveCycles) { Console.WriteLine("Creature: Clarion - Remaining Jewel: " + getJewelRemainingTotal()); // Get current sensory information IList <Thing> currentSceneInWS3D = processSensoryInformation(); // Make the perception SensoryInformation si = prepareSensoryInformation(currentSceneInWS3D); //Perceive the sensory information CurrentAgent.Perceive(si); //Choose an action ExternalActionChunk chosen = CurrentAgent.GetChosenExternalAction(si); // Get the selected action String actionLabel = chosen.LabelAsIComparable.ToString(); CreatureActions actionType = (CreatureActions)Enum.Parse(typeof(CreatureActions), actionLabel, true); // Increment the number of cognitive cycles CurrentCognitiveCycle++; // Call the output event handler processSelectedAction(actionType); //Wait to the agent accomplish his job if (TimeBetweenCognitiveCycles > 0) { Thread.Sleep(TimeBetweenCognitiveCycles); } } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds / 1000; Console.WriteLine("Clarion completed time: " + elapsedMs); }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); // Detect if we have jewel ahead IEnumerable <Thing> jewelsAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature <= CLOSE_DISTANCE)); Boolean jewelAhead = jewelsAhead.Any(); if (jewelAhead) { jewelName = jewelsAhead.First().Name; } // Detect if we have food ahead IEnumerable <Thing> foods = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_FOOD || item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature <= CLOSE_DISTANCE)); Boolean foodAhead = foods.Any(); if (foodAhead) { foodName = foods.First().Name; } // save the closest food just in case the creature needs it IEnumerable <Thing> distantFoods = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature > CLOSE_DISTANCE)); if (distantFoods.Count() > 0) { closestFood = distantFoods.OrderBy(item => item.DistanceToCreature).First(); } // Detect if we have a wall ahead Boolean wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= CLOSE_DISTANCE)).Any(); closestJewel = null; //Console.WriteLine(sensorialInformation); Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); int n = 0; bool deliver = false; foreach (Leaflet l in c.getLeaflets()) { // Detect if some leaflet is complete //l.situation mind.updateLeaflet(n, l); // check if we have all jewels from the leaflet (situation = true) if (l.situation) { // avoid delivering it twice if (!checkDelivered(l.leafletID.ToString())) { leafletId = l.leafletID.ToString(); deliver = true; } } else { // lookup the closest jewel that we need to fill a leaflet IEnumerable <Thing> distantJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature > CLOSE_DISTANCE && leafletNeedsJewel(l, item))); if (distantJewels.Count() > 0) { closestJewel = distantJewels.OrderBy(item => item.DistanceToCreature).First(); } } n++; } // Detect if we have food and we need it bool needAndHaveFood = (closestFood != null) && (c.Fuel < 400); // Add sensorial input with a rule-based prioritization if (stopped) { // Success, don't do anything else si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION); } else if (checkThreeLeafletsReady()) { // Time to deliver si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION); } else if (jewelAhead || foodAhead) { // Prioritize actions that don't require move: eat food, get jewel or deliver leaflet double jewelAheadActivationValue = jewelAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double foodAheadActivationValue = foodAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double deliverActivationValue = deliver ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, jewelAheadActivationValue); si.Add(inputFoodAhead, foodAheadActivationValue); si.Add(inputDeliverLeaflet, deliverActivationValue); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION); } else if (wallAhead) { // Avoid obstacle si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION); } else if (needAndHaveFood) { // go for food if the creature needs it si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION); } else if (closestJewel != null) { // go for the closest jewel needed for a leaflet si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputDistantJewel, CurrentAgent.Parameters.MAX_ACTIVATION); } return(si); }
public void RefreshSensoryInformation() { this.sensInfo = World.NewSensoryInformation(this.MAKaey); }
public void setSensoryInformation(SensoryInformation sensoryInfo) { this.sensInfo = sensoryInfo; }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); PreferenceThing = null; Boolean goItem = false; Boolean Ahead = false; Boolean SackItItem = false; Boolean EatItem = false; Boolean wallAhead = false; Boolean situationLeaflets = false; // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); // Detect if we have a wall ahead wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= 70)).Any(); var listLeaflets = c.getLeaflets(); // Detect if we have itens List <Thing> listItem = listOfThings .Where(item => (item.CategoryId != Thing.CATEGORY_CREATURE && item.CategoryId != Thing.CATEGORY_BRICK && item.DistanceToCreature <= 500)) .OrderBy(x => x.DistanceToCreature) .ToList(); if (!c.HasCollided) { if (listItem.Count() > 0 && PreferenceThing == null && wallAhead != true) { //If detect wall eat with mor distance int DISTANCE_SACK = wallAhead == true ? 70 : 40; PreferenceThing = listItem.FirstOrDefault(); if (PreferenceThing.DistanceToCreature < DISTANCE_SACK) { if (PreferenceThing.CategoryId == Thing.CATEGORY_JEWEL) { SackItItem = true; } else { EatItem = true; } } else { Thing preferenceJewel = PreferenceThing; int countSituation = 0; foreach (var leaflets in listLeaflets) { foreach (var item in leaflets.items) { if (item.collected < item.totalNumber) { Thing thing = listItem.Where(x => x.CategoryId == Thing.CATEGORY_JEWEL).FirstOrDefault(); if (thing != null) { if (thing.Material.Color == item.itemKey) { preferenceJewel = thing; } } } } Console.WriteLine(leaflets.leafletID + " - Pay:" + leaflets.payment + " Situation:" + leaflets.situation); situationLeaflets = leaflets.situation; if (situationLeaflets) { countSituation++; } } situationLeaflets = countSituation == 3 ? true : false; Console.WriteLine("situationLeaflets: " + situationLeaflets); if (!situationLeaflets) { if (PreferenceThing.DistanceToCreature >= preferenceJewel.DistanceToCreature) { PreferenceThing = preferenceJewel; } if (c.Fuel > 400 && PreferenceThing.CategoryId == Thing.CATEGORY_JEWEL) { goItem = true; } else if (PreferenceThing.CategoryId != Thing.CATEGORY_JEWEL && PreferenceThing.DistanceToCreature < 170) { goItem = true; } else { Ahead = true; } } } } else { Ahead = true; } } else { PreferenceThing = listItem.OrderBy(x => x.DistanceToCreature).FirstOrDefault(); if (PreferenceThing != null) { if (PreferenceThing.CategoryId == Thing.CATEGORY_JEWEL) { SackItItem = true; } else { EatItem = true; } } } double wallAheadActivationValue = Ahead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double goItemActivationValue = goItem ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double sackItItemActivationValue = SackItItem ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double eatItemActivationValue = EatItem ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double stopCreatureActivationValue = situationLeaflets ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; si.Add(inputAhead, wallAheadActivationValue); si.Add(inputGoItem, goItemActivationValue); si.Add(inputSackItItem, sackItItemActivationValue); si.Add(inputEatItem, eatItemActivationValue); si.Add(inputStopCreature, stopCreatureActivationValue); //Console.WriteLine(sensorialInformation); int n = 0; foreach (Leaflet l in c.getLeaflets()) { mind.updateLeaflet(n, l); n++; } return(si); }
public void Run() { foreach (Tasks t in Enum.GetValues(typeof(Tasks))) { int max_i = ((t == Tasks.PERSON) ? 2 * numTestTrials : numTestTrials); foreach (Groups g in Enum.GetValues(typeof(Groups))) { Console.WriteLine("Running Group " + g + " through task " + t); for (int r = 0; r < numRepeats; r++) { Console.Write("Participant #" + r + " is performing the task "); double currentW = rand.Next(12); double lastP = rand.Next(12); Initialize(g); ActivationCollection irlSI = ImplicitComponentInitializer.NewDataSet(); var irlVars = (from a in As select from b in Bs select from c in Cs select new { A = World.GetDimensionValuePair("A", a), B = World.GetDimensionValuePair("B", b), C = World.GetDimensionValuePair("C", c) }).SelectMany(k => k).SelectMany(k => k); foreach (var k in irlVars) { irlSI.Add(k.A, 1); irlSI.Add(k.B, 1); irlSI.Add(k.C, 1); } DimensionValuePair targetDV = World.GetDimensionValuePair("Target P", target); GenerateIRLRuleSet(IRL_Rule_Sets.ONE); SensoryInformation si = null; SensoryInformation prevSI; for (int i = 0; i < max_i; i++) { int shift = 10 - (int)Math.Round(10 * ((double)i / (double)max_i)); Console.CursorLeft -= shift; Console.Write("."); for (int s = 0; s < shift - 1; s++) { Console.Write(" "); } if ((from a in John.GetInternals(Agent.InternalContainers.ACTION_RULES) where a is IRLRule select a).Count() == 0) { GenerateIRLRuleSet(IRL_Rule_Sets.TWO); } prevSI = si; si = World.NewSensoryInformation(John); foreach (var s in irlSI) { si.Add(s); } si.Add(targetDV, 1); si.Add(World.GetActionChunk(currentW), 1); lastP = FactoryOutput(lastP, currentW); si.Add(World.GetDimensionValuePair("Current P", lastP), 1); if (Math.Abs(lastP - target) < double.Epsilon) { if ((t != Tasks.PERSON || (t == Tasks.PERSON && i >= numTestTrials))) { results[(int)t, (int)g, r]++; } if (prevSI != null) { John.ReceiveFeedback(prevSI, 1); } } else { if (prevSI != null) { John.ReceiveFeedback(prevSI, 0); } } John.Perceive(si); currentW = (double)John.GetChosenExternalAction(si).LabelAsIComparable; } Console.WriteLine(); Console.WriteLine("Participant #" + r + " is finished performing the task and hit the target " + results[(int)t, (int)g, r] + " times out of " + max_i); Console.WriteLine("At the end of the task, the participant had the following rules: "); foreach (var ar in John.GetInternals(Agent.InternalContainers.ACTION_RULES)) { Console.WriteLine(ar); } John.Die(); World.Remove(John); } } Console.WriteLine("Tabular results for the " + t + " task:"); Console.WriteLine("Group\tParticipant\tHits"); foreach (Groups g in Enum.GetValues(typeof(Groups))) { for (int i = 0; i < numRepeats; i++) { Console.WriteLine(g + "\t" + i + "\t" + results[(int)t, (int)g, i]); } } } }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); // Detect if we have a wall ahead Boolean wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= 21)).Any(); // double wallAheadActivationValue = wallAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; double wallAheadActivationValue = wallAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION; si.Add(inputWallAhead, wallAheadActivationValue); // Detect if we have food close by IEnumerable <Thing> foods = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature <= 61)); double foodAheadActivationValue; if (foods.Any()) { foodFound = foods.First().Name; foodAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } else { foodFound = String.Empty; foodAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; } si.Add(inputFoodAhead, foodAheadActivationValue); // Detect if we have jewel close by IEnumerable <Thing> closeJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature <= 61)); double jewelAheadActivationValue; if (closeJewels.Any()) { jewelFound = closeJewels.First().Name; jewelAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } else { jewelFound = String.Empty; jewelAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; } si.Add(inputJewelAhead, jewelAheadActivationValue); //Console.WriteLine(sensorialInformation); Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); int n = 0; foreach (Leaflet l in c.getLeaflets()) { mind.updateLeaflet(n, l); n++; } mind.updateTotLeaflet(); // Detect if has a required leaflet and prepare its position IEnumerable <Thing> jewels = listOfThings.Where(item => false); IEnumerable <Thing> redJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Red")); double goToJewelActivationValue; if (redJewels.Any() && ((mind.totLeaflet [0] - mind.red) > 0)) { jewels = jewels.Concat(redJewels); } IEnumerable <Thing> greenJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Green")); if (greenJewels.Any() && ((mind.totLeaflet [1] - mind.green) > 0)) { jewels = jewels.Concat(greenJewels); } IEnumerable <Thing> blueJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Blue")); if (blueJewels.Any() && ((mind.totLeaflet [2] - mind.blue) > 0)) { jewels = jewels.Concat(blueJewels); } IEnumerable <Thing> yellowJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Yellow")); if (yellowJewels.Any() && ((mind.totLeaflet [3] - mind.yellow) > 0)) { jewels = jewels.Concat(yellowJewels); } IEnumerable <Thing> magentaJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Magenta")); if (magentaJewels.Any() && ((mind.totLeaflet[4] - mind.magenta) > 0)) { jewels = jewels.Concat(magentaJewels); } IEnumerable <Thing> whiteJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "White")); if (whiteJewels.Any() && ((mind.totLeaflet[5] - mind.white) > 0)) { jewels = jewels.Concat(whiteJewels); } if (jewels.Any()) { jewels = jewels.OrderBy(item => item.DistanceToCreature); x_go = jewels.First().X1; y_go = jewels.First().Y1; goToJewelActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } else { goToJewelActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; } si.Add(inputNeedJewel, goToJewelActivationValue); return(si); }
public void RunTaskStep(int tick, ref int choicesMade, ref int correctChoicesMade) { // check that we have a valid enumerator // also check that the time of the hero's next state update matches the current tick // otherwise, there's nothing for us to do here until the simulation advances further if (MyHero.IsEnumeratorValid() == false || MyHero.GetCurrentStep().Tick > tick) { return; } List <Item> purchasedItems = new List <Item>(); // pull out any purchases made by the player at this time step foreach (StateChange diff in MyHero.GetCurrentStep().Diffs) { if (diff.Type == UpdateType.ItemPurchase) { Item newItem = ((ItemPurchase)diff).NewItem; if (newItem.IsConsumable() == false && newItem.IsPurchasable() == true) { purchasedItems.Add(((ItemPurchase)diff).NewItem); } } } /* * MyAgent.ACS.Parameters.FIXED_BL_LEVEL_SELECTION_MEASURE = 0.0; * MyAgent.ACS.Parameters.FIXED_RER_LEVEL_SELECTION_MEASURE = 0.0; * MyAgent.ACS.Parameters.FIXED_IRL_LEVEL_SELECTION_MEASURE = 0; * MyAgent.ACS.Parameters.FIXED_FR_LEVEL_SELECTION_MEASURE = 1.0; */ // for each purchase made by the player at this step, give the agent a chance to try to make the // same purchase // Note: the state may change without a purchase being made, in which case there is nothing for the agent to do, // we should just update the state and move on (in the calling function, for now) // Note: it is possible for the log file to record more than one purchase for a single time step, // this seems to be an artifact of the way missing components can be automatically purchased to upgrade an item. // It doesn't seem to happen often, and I'm not sure why it happens some times and not others. // In such a case, we will "peek ahead" and add the purchased item into the inventory before asking the agent // to make decisions corresponding to each subsequent purchase. // After we have run through all the purchases, we will remove these purchases from the inventory counts and // update normally (which will add them back in). foreach (Item item in purchasedItems) { // if we are using imitative learning, learning by example, supervised learning... // we are going to temporarily create a fixed rule in clarion, that suggests to the agent // that it perform the same action that the human took // we will then retract the rule after the agent makes its choice // Note: another (possibly better) approach would be to set up fixed rules for each possible // action the human might take and leave them all active // As far as I currently know, there is no way to specify a single rule that simple says // "if human did x, do x", we have to explicitly specify a new rule for each possible action // get the action chunk corresponding to the action the human player took PlayerChoice = PurchaseActions[(int)(item.Id)]; // set up a fixed rule that specifies the same action the human player took // since the rule should only be active when it is relevant, we shouldn't really need to // bother with calculating a support value. If it is active, it should have a fixed support of 1. // FixedRule imitateRule = AgentInitializer.InitializeActionRule(MyAgent, FixedRule.Factory, playerChoice, ImitativeSupportDelegate); // IRLRule imitateRule = AgentInitializer.InitializeActionRule(MyAgent, IRLRule.Factory, playerChoice, ImitativeSupportDelegate); // MyAgent.Commit(imitateRule); // sensory input to the Calrion agent //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // do we need to be creating a new object every time? // can we reuse the old and only change updated percepts? // is this currently a big performance hit? //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SensoryInformation sensoryInput = World.NewSensoryInformation(MyAgent); // output from the Clarion agent ExternalActionChunk myChoice = null; // do perception // perceive the hero's current level for (int i = 0; i < 25; i++) { if (i == MyHero.Level - 1) { sensoryInput.Add(LevelInputs[MyHero.Level - 1], MyAgent.Parameters.MAX_ACTIVATION); } else { sensoryInput.Add(LevelInputs[i], MyAgent.Parameters.MIN_ACTIVATION); } } // set the input that corresponds to the count of each item to maximum // I **think** it is not neccessary to set the other possible inputs (i.e., for different count numbers) to minimum // but I could be wrong about that // Note: we are assuming count will never be > 3 - it could be (buy more than 3 copies of an item), but there is never // likely to be a reason to do so for (int i = 0; i < Items.Length; i++) { // Console.Write(((ItemId)i).ToString() + "=" + Items[i] + "; "); for (int j = 0; j < 4; j++) { if (j == Items[i]) { sensoryInput.Add(InventoryInputs[i, Items[i]], MyAgent.Parameters.MAX_ACTIVATION); } else { sensoryInput.Add(InventoryInputs[i, j], MyAgent.Parameters.MIN_ACTIVATION); } } /* if (Items[i] > 0) { * sensoryInput.Add(InventoryInputs[i, Items[i]], MyAgent.Parameters.MAX_ACTIVATION); * } */ } // Console.WriteLine(""); // perceive MyAgent.Perceive(sensoryInput); // and choose an action myChoice = MyAgent.GetChosenExternalAction(sensoryInput); choicesMade++; // deliver appropriate feedback if (myChoice == PlayerChoice) // agent was right { MyAgent.ReceiveFeedback(sensoryInput, 1.0); // Console.WriteLine(tick + ": " + MyHero.Name + ": player and agent bought: " + item.Id.ToString()); correctChoicesMade++; } else { MyAgent.ReceiveFeedback(sensoryInput, 0.0); // agent was wrong // String choiceString = myChoice.ToString().Split(':')[0].Split(' ')[1]; // Console.WriteLine(tick + ": " + MyHero.Name + ": player bought: " + item.Id.ToString() + "; agent bought: " + choiceString); } // if we have created a fixed rule to support imitation of the human player, then // retract (remove) it here // MyAgent.Retract(imitateRule, Agent.InternalContainers.ACTION_RULES); // MyAgent.Remove(imitateRule); // increment the count for the item purchased by the player (this will get decremented later, then updated based on the inventory state) // this is to handle the case where more than one purchase is recorded for a single time step (something a human can't actually do, // but that the software may record as a result of automated purchases during an upgrade) Items[(int)(item.Id)]++; } // decrement the count of each purchased item // this is to handle the case where more than one purchase is recorded for a single time step (something a human can't actually do, // but that the software may record as a result of automated purchases during an upgrade) foreach (Item item in purchasedItems) { Items[(int)(item.Id)]--; } }
public static void Test(Groups g, int a) { Console.Write("Performing Task..."); int [,] shuffler = new int[8, 8]; bgErr[g].Add(0); btErr[g].Add(0); wgErr[g].Add(0); wtErr[g].Add(0); List <DeclarativeChunk> primes = new List <DeclarativeChunk>(); primes.AddRange(white_faces); primes.AddRange(black_faces); List <DeclarativeChunk> targets = new List <DeclarativeChunk>(); targets.AddRange(guns); targets.AddRange(tools); for (int i = 0; i < numTestTrials; i++) { int p = r.Next(8); int t = r.Next(8); while (shuffler[p, t] == 2) { p = r.Next(8); t = r.Next(8); } shuffler[p, t]++; SensoryInformation si = World.NewSensoryInformation(Participant); si.AddRange(primes[p], 1); si.Add(targets[t], 1); si[Drive.MetaInfoReservations.STIMULUS, typeof(HonorDrive).Name] = (double)1 / (double)5; Participant.Perceive(si); ExternalActionChunk chosen = Participant.GetChosenExternalAction(si); if ((chosen.LabelAsIComparable.Equals("Tool") && !tools.Contains(targets[t]))) { //The participant made an inaccurate judgment on a gun trial if (si.Contains(World.GetDimensionValuePair("SkinColor", "Black"))) //The error was on a black trial { bgErr[g][a]++; } else { wgErr[g][a]++; } } else if ((chosen.LabelAsIComparable.Equals("Gun") && !guns.Contains(targets[t]))) { //The participant made an inaccurate judgment on a tool trial if (si.Contains(World.GetDimensionValuePair("SkinColor", "Black"))) //The error was on a black trial { btErr[g][a]++; } else { wtErr[g][a]++; } } } Console.WriteLine("Finished"); }
/// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // FMT 30/04/2017 - debugging /*Console.WriteLine("Dumping listoFThings (input):"); * for (int iList = 0; iList < listOfThings.Count(); iList++) * { * Thing currThing = (Thing) listOfThings[iList]; * Console.Write("Thing "); * Console.WriteLine(iList); * Console.WriteLine(currThing); * }*/ // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); // FMT 29/04/2017 - handle food and jewel - initialization Boolean jewelAhead = false; Boolean foodAhead = false; Boolean wallAhead = false; double closeObjectActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double foodAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double jewelAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double jewelHideActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double leafletJewelAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double leafletCompleteActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; double wallAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION; // FMT updating mind view myCreature = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); seenThing = listOfThings.Where(item => (item.CategoryId != Thing.CATEGORY_CREATURE && item.CategoryId != Thing.CATEGORY_BRICK)).OrderBy(x => x.DistanceToCreature).FirstOrDefault(); int n = 0; int isRequiredTotal = 0; List <Leaflet> leaflets = myCreature.getLeaflets(); foreach (Leaflet l in leaflets) { if (mind != null) { mind.updateLeaflet(n, l); } // FMT checking if we target a jewel if that jewel is in the leaflet if (jewelAheadActivationValue > CurrentAgent.Parameters.MIN_ACTIVATION) { int isRequired = l.getRequired(lastSeenJewelColor); int isCollected = l.getCollected(lastSeenJewelColor); isRequiredTotal = isRequiredTotal + (isRequired - isCollected); } n++; } // FMT preparing input foreach (Thing item in listOfThings) { switch (item.CategoryId) { case Thing.CATEGORY_JEWEL: if (item.DistanceToCreature <= 61) { jewelAhead = true; //CurrentAgent.ReceiveFeedback(si, 1.0); ; } else { // FMT 15/06 disable jewelAhead verification for now jewelAhead = true; if ((thingDistance > 0) && (thingDistance > item.DistanceToCreature)) { thingX = item.comX; thingY = item.comY; thingDistance = item.DistanceToCreature; //CurrentAgent.ReceiveFeedback(si, 0.0); } } if (jewelAhead) { if (jewelInLeaflets(item.Material.Color, leaflets)) { leafletJewelAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } else { jewelAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; jewelHideActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } lastSeenJewel = item.Name; lastSeenJewelColor = item.Material.Color; Console.Write("Input: jewel "); Console.WriteLine(lastSeenJewel); //jewelAhead = false; } break; case Thing.CATEGORY_FOOD: case Thing.CATEGORY_PFOOD: case Thing.CATEGORY_NPFOOD: if (item.DistanceToCreature <= 61) { foodAhead = true; } if ((foodAhead) && !(jewelAhead)) { foodAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; lastSeenFood = item.Name; Console.Write("Input: food "); Console.WriteLine(lastSeenFood); } break; case Thing.CATEGORY_BRICK: if (item.DistanceToCreature <= 61) { wallAhead = true; } // Detect if we have a wall ahead if ((wallAhead) && !(jewelAhead) && !(foodAhead)) { wallAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } break; } } Boolean hasCompletedLeaflet = leaflets[0].situation; if (hasCompletedLeaflet) { leafletCompleteActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } Boolean closeObjectAhead = seenThing == null ? false : seenThing.DistanceToCreature < 40; if (closeObjectAhead) { closeObjectActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION; } // FMT adding inputs si.Add(inputCloseObject, closeObjectActivationValue); si.Add(inputFoodAhead, foodAheadActivationValue); si.Add(inputJewelAhead, jewelAheadActivationValue); si.Add(inputJewelHide, jewelHideActivationValue); si.Add(inputLeafletJewelAhead, leafletJewelAheadActivationValue); si.Add(inputLeafletComplete, leafletCompleteActivationValue); si.Add(inputWallAhead, wallAheadActivationValue); //Console.WriteLine("prepareSensoryInformation: food "+ foodAheadActivationValue+" jewel "+ jewelAheadActivationValue+ // " leafletJewel "+ leafletJewelAheadActivationValue+" hide "+ jewelHideActivationValue+ // " wall "+ wallAheadActivationValue); //Console.WriteLine(sensorialInformation); //System.Threading.Thread.Sleep(100); return(si); }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); int targetRed, targetGreen, targetBlue, targetYellow, targetMagenta, targetWhite; Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First(); Sack s = updateSackAndTarget(listOfThings, c, out targetRed, out targetGreen, out targetBlue, out targetYellow, out targetMagenta, out targetWhite); // Set up initial activation levels. double wallAheadActivationValue = MIN_ACT_VAL; double jewelAheadActivationValue = MIN_ACT_VAL; double foodAheadActivationValue = MIN_ACT_VAL; double jewelAwayActivationValue = MIN_ACT_VAL; double foodAwayActivationValue = MIN_ACT_VAL; double allJewelsCollectedActivationValue = MIN_ACT_VAL; double creatureCanDeliverActivationValue = MIN_ACT_VAL; // Loop through the list of things in the environment. // First, handle close objects. foreach (Thing thing in listOfThings) { // Thing is close to the creature, so change activation values accordingly. int categoryId = thing.CategoryId; if (thing.DistanceToCreature <= 50 && categoryId != Thing.CATEGORY_CREATURE) { switch (categoryId) { case Thing.CATEGORY_BRICK: wallAheadActivationValue = WALL_AHEAD_ACT_VAL; break; case Thing.CATEGORY_JEWEL: jewelAheadActivationValue = JEWEL_AHEAD_ACT_VAL; jewelToGet = thing; break; case Thing.categoryPFOOD: case Thing.CATEGORY_NPFOOD: case Thing.CATEGORY_FOOD: foodAheadActivationValue = FOOD_AHEAD_ACT_VAL; foodToGet = thing; break; default: break; } } } // Look now for the closest jewel to go to. IEnumerable <Thing> jewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature > 50)); if (jewels.Any()) { IEnumerable <Thing> orderedJewels = jewels.OrderBy(item => item.DistanceToCreature); Boolean foundJewel = false; foreach (Thing jewel in orderedJewels) { // Check if the jewel is required, otherwise skip. if ((jewel.Material.Color.Equals("Red") && targetRed > 0) || (jewel.Material.Color.Equals("Green") && targetGreen > 0) || (jewel.Material.Color.Equals("Blue") && targetBlue > 0) || (jewel.Material.Color.Equals("Yellow") && targetYellow > 0) || (jewel.Material.Color.Equals("Magenta") && targetMagenta > 0) || (jewel.Material.Color.Equals("White") && targetWhite > 0)) { jewelAwayActivationValue = JEWEL_AWAY_ACT_VAL; jewelToGoTo = jewel; // Found one jewel as target, no need to keep looking. Console.WriteLine("Jewel to go to: " + jewel.Name); Console.WriteLine("Jewel color: " + jewel.Material.Color); break; } } if (!foundJewel && !GlobalVars.competitionMode) { Console.WriteLine("No more jewels to collect!!!"); Console.WriteLine("targetRed = " + targetRed); Console.WriteLine("targetGreen = " + targetGreen); Console.WriteLine("targetBlue = " + targetBlue); Console.WriteLine("targetYellow = " + targetYellow); Console.WriteLine("targetMagenta = " + targetMagenta); Console.WriteLine("targetWhite = " + targetWhite); } } // Verify if all collection of jewels is done. if (targetRed <= 0 && targetGreen <= 0 && targetBlue <= 0 && targetYellow <= 0 && targetMagenta <= 0 && targetWhite <= 0) { allJewelsCollectedActivationValue = ALL_JEWELS_COLLECTED_ACT_VAL; Console.WriteLine("Go to delivery, all items collected"); } // And the closest food to go to, if required. IEnumerable <Thing> foods = listOfThings.Where(item => (item.CategoryId == Thing.categoryPFOOD || item.CategoryId == Thing.CATEGORY_FOOD || item.CategoryId == Thing.CATEGORY_NPFOOD && item.DistanceToCreature > 50)); if (foods.Any() && c.Fuel < 400) { foodAwayActivationValue = FOOD_AWAY_ACT_VAL; foodToGoTo = foods.OrderBy(item => item.DistanceToCreature).First(); Console.WriteLine("Food to go to: " + foodToGoTo.Name); } // Check if at the delivery spot with jewels to deliver and set activation for delivery. if (c.X1 == 0 && c.X2 == 0 && s != null && ((s.red_crystal >= 0) || (s.green_crystal >= 0) || (s.blue_crystal >= 0) || (s.yellow_crystal >= 0) || (s.magenta_crystal >= 0) || (s.white_crystal >= 0))) { creatureCanDeliverActivationValue = CREATURE_CAN_DELIVER_ACT_VAL; Console.WriteLine("Creature can deliver jewels"); } // Set up activation levels. si.Add(inputWallAhead, wallAheadActivationValue); si.Add(inputJewelAhead, jewelAheadActivationValue); si.Add(inputFoodAhead, foodAheadActivationValue); si.Add(inputJewelAway, jewelAwayActivationValue); si.Add(inputFoodAway, foodAwayActivationValue); si.Add(inputAllJewelsCollected, allJewelsCollectedActivationValue); si.Add(inputCreatureCanDeliver, creatureCanDeliverActivationValue); return(si); }
static void DoReasoning(Agent reasoner) { /////Property Test: "lives in water"///// SensoryInformation si = World.NewSensoryInformation(reasoner); //Used to compare activations later double TunaWhale = 0; double WhaleBear = 0; //Adds the "lives in water" property to the input si.Add(World.GetDimensionValuePair("lives in", "water"), 1); var o = reasoner.NACS.PerformReasoning(si); //Checks each conclusion chunk and adds up the activations foreach (var i in o) { if ((string)i.CHUNK.LabelAsIComparable == "Tuna") { TunaWhale += i.ACTIVATION; } else if ((string)i.CHUNK.LabelAsIComparable == "Whale") { TunaWhale += i.ACTIVATION; WhaleBear += i.ACTIVATION; } else if ((string)i.CHUNK.LabelAsIComparable == "Bear") { WhaleBear += i.ACTIVATION; } } Console.WriteLine(); Console.WriteLine("Which animal pairing is more likely to live in water?"); if (TunaWhale > WhaleBear) { Console.WriteLine("A tuna and whale (" + Math.Round(TunaWhale, 2) + " vs. " + Math.Round(WhaleBear, 2) + ")"); } else { Console.WriteLine("A bear and whale (" + Math.Round(WhaleBear, 2) + " vs. " + Math.Round(TunaWhale, 2) + ")"); } /////Property Test: "eats fish"///// si = World.NewSensoryInformation(reasoner); //Resets the activation counters TunaWhale = 0; WhaleBear = 0; //Adds the "eats fish" property to the input si.Add(World.GetDimensionValuePair("eats", "fish"), 1); o = reasoner.NACS.PerformReasoning(si); //Checks each conclusion chunk and adds up the activations foreach (var i in o) { if ((string)i.CHUNK.LabelAsIComparable == "Tuna") { TunaWhale += i.ACTIVATION; } else if ((string)i.CHUNK.LabelAsIComparable == "Whale") { TunaWhale += i.ACTIVATION; WhaleBear += i.ACTIVATION; } else if ((string)i.CHUNK.LabelAsIComparable == "Bear") { WhaleBear += i.ACTIVATION; } } Console.WriteLine(); Console.WriteLine("Which animal pairing is more likely to eat fish?"); if (TunaWhale > WhaleBear) { Console.WriteLine("A tuna and whale (" + Math.Round(TunaWhale, 2) + " vs. " + Math.Round(WhaleBear, 2) + ")"); } else { Console.WriteLine("A bear and whale (" + Math.Round(WhaleBear, 2) + " vs. " + Math.Round(TunaWhale, 2) + ")"); } }
/// <summary> /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand /// </summary> /// <param name="sensorialInformation">The information that came from server</param> /// <returns>The perceived information</returns> private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings) { // New sensory information SensoryInformation si = World.NewSensoryInformation(CurrentAgent); currentJewel = null; currentFood = null; bool brickCreatureCheck = false; bool jewelAhead = false; bool foodAhead = false; bool desiredJewelInVision = false; Thing jewelInVision = null; Thing desiredJewelToGet = null; Thing foodToEat = null; if (getJewelRemainingTotal() == jewelGoal && exchangeJewels) { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); return(si); } else { // Read each Thing to prepare Sensory Information activation for (int i = 0; i < listOfThings.Count; i++) { if (listOfThings [i].CategoryId == Thing.CATEGORY_BRICK && listOfThings [i].DistanceToCreature <= 50) { brickCreatureCheck = true; break; } if (listOfThings [i].CategoryId == Thing.CATEGORY_CREATURE && !listOfThings [i].Name.Equals(creatureName) && listOfThings [i].DistanceToCreature <= 50) { brickCreatureCheck = true; break; } if (listOfThings [i].CategoryId == Thing.CATEGORY_JEWEL) { if (listOfThings [i].DistanceToCreature <= 55) { jewelAhead = true; desiredJewelToGet = listOfThings [i]; } else if (isDesiredJewel(listOfThings [i].Material.Color)) { desiredJewelInVision = true; jewelInVision = listOfThings [i]; } } if (listOfThings [i].CategoryId == Thing.categoryPFOOD && listOfThings [i].DistanceToCreature <= 55) { foodAhead = true; if (foodToEat != null) { if (listOfThings [i].DistanceToCreature < foodToEat.DistanceToCreature) { foodToEat = listOfThings [i]; } } else { foodToEat = listOfThings [i]; } } if (listOfThings [i].CategoryId == Thing.CATEGORY_FOOD && listOfThings [i].DistanceToCreature <= 55) { foodAhead = true; if (foodToEat != null) { if (listOfThings [i].DistanceToCreature < foodToEat.DistanceToCreature) { foodToEat = listOfThings [i]; } } else { foodToEat = listOfThings [i]; } } if (listOfThings [i].CategoryId == Thing.CATEGORY_NPFOOD && listOfThings [i].DistanceToCreature <= 55) { foodAhead = true; if (foodToEat != null) { if (listOfThings [i].DistanceToCreature < foodToEat.DistanceToCreature) { foodToEat = listOfThings [i]; } } else { foodToEat = listOfThings [i]; } } if (listOfThings [i].CategoryId == Thing.CATEGORY_JEWEL && isDesiredJewel(listOfThings [i].Material.Color) && listOfThings [i].DistanceToCreature > 55) { desiredJewelInVision = true; if (jewelInVision != null) { if (listOfThings [i].DistanceToCreature < jewelInVision.DistanceToCreature) { jewelInVision = listOfThings [i]; } } else { jewelInVision = listOfThings [i]; } } } Creature cr = (Creature)listOfThings [0]; //Console.WriteLine ("Fuel:" + cr.Fuel); fuelLow = false; if (cr.Fuel <= fuelLimit && !foodAhead && !jewelAhead && !brickCreatureCheck) { fuelLow = true; foreach (Thing t in listOfThings) { if (t.CategoryId == Thing.CATEGORY_FOOD || t.CategoryId == Thing.CATEGORY_NPFOOD) { if (currentFood == null) { currentFood = t; //Console.WriteLine ("Food found...."); } else { if (t.DistanceToCreature < currentFood.DistanceToCreature) { currentFood = t; //Console.WriteLine ("Food found...."); } } } } si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MAX_ACTIVATION); Console.WriteLine("GO TO SPOT"); return(si); } else if (brickCreatureCheck) { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); Console.WriteLine("CATEGORY_BRICK"); } else if (jewelAhead) { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); currentJewel = desiredJewelToGet; Console.WriteLine("CATEGORY_JEWEL_AHEAD"); } else if (foodAhead) { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); currentFood = foodToEat; Console.WriteLine("CATEGORY_FOOD"); } else if (desiredJewelInVision) { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MAX_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); currentJewel = jewelInVision; Console.WriteLine("CATEGORY_JEWEL_IN_VISION"); } else { si.Add(inputWallCreatureAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputJewelInVision, CurrentAgent.Parameters.MIN_ACTIVATION); si.Add(inputFuelLow, CurrentAgent.Parameters.MIN_ACTIVATION); Console.WriteLine("WANDER"); } return(si); } }
/// <summary> /// The event handler for new external action chosen events /// </summary> protected override void ProcessChosenExternalAction(Agent actor, ExternalActionChunk chosenAction, SensoryInformation relatedSI, Dictionary <ActionChunk, double> finalActionActivations, long performedAt, long responseTime) { if ((bool)chosenAction.LabelAsIComparable) { //The agent said "True". if ((relatedSI["Boolean 1", true] == John.Parameters.MAX_ACTIVATION && relatedSI["Boolean 2", false] == John.Parameters.MAX_ACTIVATION) || (relatedSI["Boolean 1", false] == John.Parameters.MAX_ACTIVATION && relatedSI["Boolean 2", true] == John.Parameters.MAX_ACTIVATION)) { //The agent responded correctly Trace.WriteLineIf(World.LoggingSwitch.TraceWarning, "John was correct"); //Record the agent's success. CorrectCounter++; //Give positive feedback. John.ReceiveFeedback(relatedSI, 1.0); } else { //The agent responded incorrectly Trace.WriteLineIf(World.LoggingSwitch.TraceWarning, "John was incorrect"); //Give negative feedback. John.ReceiveFeedback(relatedSI, 0.0); } } else { //The agent said "False". if ((relatedSI["Boolean 1", true] == John.Parameters.MAX_ACTIVATION && relatedSI["Boolean 2", true] == John.Parameters.MAX_ACTIVATION) || (relatedSI["Boolean 1", false] == John.Parameters.MAX_ACTIVATION && relatedSI["Boolean 2", false] == John.Parameters.MAX_ACTIVATION)) { //The agent responded correctly Trace.WriteLineIf(World.LoggingSwitch.TraceWarning, "John was correct"); //Record the agent's success. CorrectCounter++; //Give positive feedback. John.ReceiveFeedback(relatedSI, 1.0); } else { //The agent responded incorrectly Trace.WriteLineIf(World.LoggingSwitch.TraceWarning, "John was incorrect"); //Give negative feedback. John.ReceiveFeedback(relatedSI, 0.0); } } trialWaitHold.Set(); }