- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/World/TigerChannel.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00017 using System;
    00018 using System.Diagnostics;
    00019 using System.Collections.Generic;
    00020 using System.Text;
    00021 using Smile;
    00022
    00023 namespace Game
    00024 {
    00025     public class TigerChannel : BayesianChannel
    00026     {
    00027         Network network;
    00028
    00029         public TigerChannel()
    00030         {
    00031             network = new Network();
    00032             network.ReadFile("Herbivore.xdsl");
    00033         }
    00034
    00035         public override void Run(WorldEntity controlledEntity)
    00036         {
    00037             Tiger tiger = controlledEntity as Tiger;
    00038             if (tiger == null)
    00039             {
    00040                 throw new ArgumentException("TigerChannel can only be associated with a Tiger entity!");
    00041             }
    00042             else
    00043             {
    00044                 Run(tiger);
    00045             }
    00046         }
    00047
    00048         public void Run(Tiger tiger)
    00049         {
    00050             network.ClearAllEvidence();
    00051             network.ClearAllTargets();
    00052             network.ClearControlledValue("Et_1");
    00053
    00054             LoadInputs(tiger);
    00055             network.UpdateBeliefs();
    00056             ProcessOutputs(tiger);
    00057         }
    00058
    00059         public void LoadInputs(Tiger tiger)
    00060         {
    00061             // Setting current state.
    00062             switch (tiger.currentState)
    00063             {
    00064                 case State.Explore:
    00065                     network.SetEvidence("Et", "Explore");
    00066                     Debug.WriteLine("Et = Explore");
    00067                     break;
    00068                 case State.Bois:
    00069                     network.SetEvidence("Et", "Bois");
    00070                     Debug.WriteLine("Et = Bois");
    00071                     break;
    00072                 case State.Dort:
    00073                     network.SetEvidence("Et", "Dort");
    00074                     Debug.WriteLine("Et = Dort");
    00075                     break;
    00076                 case State.Fuit:
    00077                     network.SetEvidence("Et", "Fuit");
    00078                     Debug.WriteLine("Et = Fuit");
    00079                     break;
    00080                 case State.Mange:
    00081                     network.SetEvidence("Et", "Mange");
    00082                     Debug.WriteLine("Et = Mange");
    00083                     break;
    00084                 default:
    00085                     break;
    00086             }
    00087
    00088             // Sensors
    00089             if (tiger.Faim)
    00090             {
    00091                 network.SetEvidence("Faim", "Oui");
    00092                 Debug.WriteLine("Faim");
    00093             }
    00094             else
    00095             {
    00096                 network.SetEvidence("Faim", "Non");
    00097                 Debug.WriteLine("Pas Faim");
    00098             }
    00099             if (tiger.Soif)
    00100             {
    00101                 network.SetEvidence("Soif", "Oui");
    00102                 Debug.WriteLine("Soif");
    00103             }
    00104             else
    00105             {
    00106                 network.SetEvidence("Soif", "Non");
    00107                 Debug.WriteLine("Pas Soif");
    00108             }
    00109             if (tiger.ProxNourriture)
    00110             {
    00111                 network.SetEvidence("ProxNourriture", "Oui");
    00112                 Debug.WriteLine("ProxNourriture");
    00113             }
    00114             else
    00115             {
    00116                 network.SetEvidence("ProxNourriture", "Non");
    00117                 Debug.WriteLine("Pas ProxNourriture");
    00118             }
    00119             if (tiger.ProxEau)
    00120             {
    00121                 network.SetEvidence("ProxEau", "Oui");
    00122                 Debug.WriteLine("ProxEau");
    00123             }
    00124             else
    00125             {
    00126                 network.SetEvidence("ProxEau", "Non");
    00127                 Debug.WriteLine("Pas ProxEau");
    00128             }
    00129             if (tiger.Sommeil)
    00130             {
    00131                 network.SetEvidence("Sommeil", "Oui");
    00132                 Debug.WriteLine("Sommeil");
    00133             }
    00134             else
    00135             {
    00136                 network.SetEvidence("Sommeil", "Non");
    00137                 Debug.WriteLine("Pas Sommeil");
    00138             }
    00139             if (tiger.Bruit)
    00140             {
    00141                 network.SetEvidence("Bruit", "Oui");
    00142                 Debug.WriteLine("Bruit");
    00143             }
    00144             else
    00145             {
    00146                 network.SetEvidence("Bruit", "Non");
    00147                 Debug.WriteLine("Pas Bruit");
    00148             }
    00149         }
    00150
    00151         public void ProcessOutputs(Tiger tiger)
    00152         {
    00153             double[] values = network.GetNodeValue("Et_1");
    00154
    00155             int indexOfHighestValue = 0;
    00156             double highest = values[0];
    00157
    00158             for (int i = 1; i < values.Length; i++)
    00159             {
    00160                 Debug.WriteLine("Value[" + i + "]=" + values[i]);
    00161                 if (values[i] > highest)
    00162                 {
    00163                     indexOfHighestValue = i;
    00164                     highest = values[i];
    00165                 }
    00166             }
    00167
    00168             network.ClearAllEvidence();
    00169             network.ClearAllTargets();
    00170
    00171             switch (indexOfHighestValue)
    00172             {
    00173                 case 0:
    00174                     if (tiger.currentState != State.Bois)
    00175                     {
    00176                         tiger.currentState = State.Bois;
    00177                         tiger.stateTime = 0;
    00178                         tiger.stateJustChanged = true;
    00179                     }
    00180                     break;
    00181                 case 1:
    00182                     if (tiger.currentState != State.Dort)
    00183                     {
    00184                         tiger.currentState = State.Dort;
    00185                         tiger.stateTime = 0;
    00186                         tiger.stateJustChanged = true;
    00187                     }
    00188                     break;
    00189                 case 2:
    00190                     if (tiger.currentState != State.Explore)
    00191                     {
    00192                         tiger.currentState = State.Explore;
    00193                         tiger.stateTime = 0;
    00194                         tiger.stateJustChanged = true;
    00195                     }
    00196                     break;
    00197                 case 3:
    00198                     if (tiger.currentState != State.Fuit)
    00199                     {
    00200                         tiger.currentState = State.Fuit;
    00201                         tiger.stateTime = 0;
    00202                         tiger.stateJustChanged = true;
    00203                     }
    00204                     break;
    00205                 case 4:
    00206                     if (tiger.currentState != State.Mange)
    00207                     {
    00208                         tiger.currentState = State.Mange;
    00209                         tiger.stateTime = 0;
    00210                         tiger.stateJustChanged = true;
    00211                     }
    00212                     break;
    00213                 default:
    00214                     break;
    00215             }
    00216         }
    00217     }
    00218 }