Browse Source

added auto think time update; added destiation on ui

master
Robin Thoni 7 years ago
parent
commit
2dad70e4d8

+ 6
- 0
.idea/.idea.uqac-ia-aspirobot/.idea/vcs.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

+ 23
- 13
uqac-ia-aspirobot/Agent/AgAgent.cs View File

@@ -1,4 +1,5 @@
1
-using System.Linq;
1
+using System;
2
+using System.Linq;
2 3
 using System.Threading;
3 4
 using Microsoft.Extensions.DependencyInjection;
4 5
 using Microsoft.Extensions.Options;
@@ -73,7 +74,8 @@ namespace uqac_ia_aspirobot.Agent
73 74
             _state = state;
74 75
             _ui = ui;
75 76
             _options = options.Value;
76
-            _state.SleepTime = _options.SleepTime;
77
+            _state.LastThinkTime = DateTime.MinValue;
78
+            _state.ThinkTimeInterval = _options.ThinkTimeInterval;
77 79
         }
78 80
 
79 81
         private void Run()
@@ -85,9 +87,9 @@ namespace uqac_ia_aspirobot.Agent
85 87
 
86 88
             while (running)
87 89
             {
88
-                if (_state.SleepTime > 0)
90
+                if (_options.SleepTime > 0)
89 91
                 {
90
-                    Thread.Sleep(_state.SleepTime);
92
+                    Thread.Sleep(_options.SleepTime);
91 93
                 }
92 94
                 UpdateSensors();
93 95
                 _ui.Update();
@@ -104,26 +106,34 @@ namespace uqac_ia_aspirobot.Agent
104 106
 
105 107
         public void UpdateState()
106 108
         {
107
-            var dustyRooms = _environment.FindDustyRooms();//TODO Compute average on xx last values
108
-            if (_options.AutoAdjustSleepTime)
109
+            if (_options.AutoAdjustThinkTimeInterval)
109 110
             {
110
-                if (dustyRooms.Count > _state.DustyRooms.Count)
111
+                var dustyRooms = _environment.FindDustyRooms();
112
+                if ((dustyRooms.Count < _state.DustyRooms.Count || !dustyRooms.Any()) && _state.ThinkTimeInterval < 7500)
111 113
                 {
112
-                    _state.SleepTime += _state.SleepTime / 2;
114
+                    _state.ThinkTimeInterval += _state.ThinkTimeInterval / 2;
113 115
                 }
114
-                else if (dustyRooms.Count < _state.DustyRooms.Count)
116
+                else if (dustyRooms.Count > _state.DustyRooms.Count && _state.ThinkTimeInterval > 100)
115 117
                 {
116
-                    _state.SleepTime -= _state.SleepTime / 2;
118
+                    _state.ThinkTimeInterval -= _state.ThinkTimeInterval / 2;
117 119
                 }
120
+                _state.DustyRooms = dustyRooms;
121
+            }
122
+            else
123
+            {
124
+                _state.DustyRooms = _environment.FindDustyRooms();
118 125
             }
119
-            _state.DustyRooms = dustyRooms;
120 126
         }
121 127
 
122 128
         public void Think()
123 129
         {
124
-            if (_state.Destination == null && _state.DustyRooms.Any())
130
+            if (_state.LastThinkTime.AddMilliseconds(_state.ThinkTimeInterval) <= DateTime.Now)
125 131
             {
126
-                _state.Destination = _state.DustyRooms.OrderBy(room => room.Distance(_engineEffector)).First();
132
+                if (_state.Destination == null && _state.DustyRooms.Any())
133
+                {
134
+                    _state.Destination = _state.DustyRooms.OrderBy(room => room.Distance(_engineEffector)).First();
135
+                }
136
+                _state.LastThinkTime = DateTime.Now;
127 137
             }
128 138
         }
129 139
 

+ 5
- 1
uqac-ia-aspirobot/Agent/AgConfig.cs View File

@@ -2,17 +2,21 @@
2 2
 {
3 3
     public class AgConfig
4 4
     {
5
-        public bool AutoAdjustSleepTime { get; set; }
5
+        public bool AutoAdjustThinkTimeInterval { get; set; }
6 6
 
7 7
         public int SleepTime { get; set; }
8 8
 
9
+        public int ThinkTimeInterval { get; set; }
10
+
9 11
         public int StartX { get; set; }
10 12
 
11 13
         public int StartY { get; set; }
12 14
 
13 15
         public void CopyTo(AgConfig other)
14 16
         {
17
+            other.AutoAdjustThinkTimeInterval = AutoAdjustThinkTimeInterval;
15 18
             other.SleepTime = SleepTime;
19
+            other.ThinkTimeInterval = ThinkTimeInterval;
16 20
             other.StartX = StartX;
17 21
             other.StartY = StartY;
18 22
         }

+ 5
- 2
uqac-ia-aspirobot/Agent/AgState.cs View File

@@ -1,11 +1,14 @@
1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
2 3
 using uqac_ia_aspirobot.Interfaces;
3 4
 
4 5
 namespace uqac_ia_aspirobot.Agent
5 6
 {
6 7
     public class AgState
7 8
     {
8
-        public int SleepTime { get; set; }
9
+        public int ThinkTimeInterval { get; set; }
10
+
11
+        public DateTime LastThinkTime { get; set; }
9 12
 
10 13
         public IList<IRoom> DustyRooms { get; set; }
11 14
 

+ 1
- 2
uqac-ia-aspirobot/Agent/Effectors/AgEngineEffector.cs View File

@@ -1,5 +1,4 @@
1
-using System;
2
-using Microsoft.Extensions.Options;
1
+using Microsoft.Extensions.Options;
3 2
 using uqac_ia_aspirobot.Extensions;
4 3
 
5 4
 namespace uqac_ia_aspirobot.Agent.Effectors

+ 17
- 22
uqac-ia-aspirobot/Common/ArStreamString.cs View File

@@ -1,5 +1,4 @@
1 1
 using System;
2
-using System.Collections.Generic;
3 2
 using System.IO;
4 3
 using System.Text;
5 4
 
@@ -7,26 +6,23 @@ namespace uqac_ia_aspirobot.Common
7 6
 {
8 7
     public class ArStreamString
9 8
     {
10
-        private readonly Stream ioStream;
11
-        private readonly UnicodeEncoding streamEncoding;
12
-        private readonly IList<string> _writtenStrings = new List<string>();
13
-        private readonly IList<string> _readStrings = new List<string>();
9
+        private readonly Stream _ioStream;
10
+        private readonly UnicodeEncoding _streamEncoding;
14 11
 
15 12
         public ArStreamString(Stream ioStream)
16 13
         {
17
-            this.ioStream = ioStream;
18
-            streamEncoding = new UnicodeEncoding();
14
+            _ioStream = ioStream;
15
+            _streamEncoding = new UnicodeEncoding();
19 16
         }
20 17
 
21 18
         public string ReadString()
22 19
         {
23
-            int len = ioStream.ReadByte() * 256;
24
-            len += ioStream.ReadByte();
25
-            byte[] inBuffer = new byte[len];
26
-            ioStream.Read(inBuffer, 0, len);
20
+            var len = _ioStream.ReadByte() * 256;
21
+            len += _ioStream.ReadByte();
22
+            var inBuffer = new byte[len];
23
+            _ioStream.Read(inBuffer, 0, len);
27 24
 
28
-            var str = streamEncoding.GetString(inBuffer);
29
-            _readStrings.Add(str);
25
+            var str = _streamEncoding.GetString(inBuffer);
30 26
             return str;
31 27
         }
32 28
 
@@ -44,17 +40,16 @@ namespace uqac_ia_aspirobot.Common
44 40
 
45 41
         public int Write(string outString)
46 42
         {
47
-            _writtenStrings.Add(outString);
48
-            byte[] outBuffer = streamEncoding.GetBytes(outString);
49
-            int len = outBuffer.Length;
50
-            if (len > UInt16.MaxValue)
43
+            var outBuffer = _streamEncoding.GetBytes(outString);
44
+            var len = outBuffer.Length;
45
+            if (len > ushort.MaxValue)
51 46
             {
52
-                len = (int)UInt16.MaxValue;
47
+                len = ushort.MaxValue;
53 48
             }
54
-            ioStream.WriteByte((byte)(len / 256));
55
-            ioStream.WriteByte((byte)(len & 255));
56
-            ioStream.Write(outBuffer, 0, len);
57
-            ioStream.Flush();
49
+            _ioStream.WriteByte((byte)(len / 256));
50
+            _ioStream.WriteByte((byte)(len & 255));
51
+            _ioStream.Write(outBuffer, 0, len);
52
+            _ioStream.Flush();
58 53
 
59 54
             return outBuffer.Length + 2;
60 55
         }

+ 6
- 0
uqac-ia-aspirobot/Extensions/Extensions.cs View File

@@ -1,6 +1,7 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4
+using uqac_ia_aspirobot.Agent;
4 5
 using uqac_ia_aspirobot.Agent.Effectors;
5 6
 using uqac_ia_aspirobot.Common;
6 7
 using uqac_ia_aspirobot.Interfaces;
@@ -140,6 +141,11 @@ namespace uqac_ia_aspirobot.Extensions
140 141
             return engine.X == x && engine.Y == y;
141 142
         }
142 143
 
144
+        public static bool IsInPosition(this IRoom room, int x, int y)
145
+        {
146
+            return room.X == x && room.Y == y;
147
+        }
148
+
143 149
         public static bool IsInRoom(this AgEngineEffector engine, IRoom room)
144 150
         {
145 151
             return engine.IsInPosition(room.X, room.Y);

+ 3
- 3
uqac-ia-aspirobot/Program.cs View File

@@ -24,8 +24,7 @@ namespace uqac_ia_aspirobot
24 24
                 ActionPropability = 30.0f,
25 25
                 AddDustProbability = 50.0f,
26 26
                 AddJewelProbability = 50.0f,
27
-//                RemoveJewelProbability = 50.0f,
28
-                RemoveJewelProbability = 0,
27
+                RemoveJewelProbability = 30.0f,
29 28
                 SleepTime = 1000
30 29
             }, arConfig);
31 30
 
@@ -36,7 +35,8 @@ namespace uqac_ia_aspirobot
36 35
                 SleepTime = 1000,
37 36
                 StartX = 0,
38 37
                 StartY = 0,
39
-                AutoAdjustSleepTime = false
38
+                AutoAdjustThinkTimeInterval = true,
39
+                ThinkTimeInterval = 10000
40 40
             }, arConfig);
41 41
 
42 42
             AgAgent.Join();

+ 68
- 2
uqac-ia-aspirobot/UI/UiConsole.cs View File

@@ -14,15 +14,17 @@ namespace uqac_ia_aspirobot.UI
14 14
         private readonly AgPickedSensor _agPickedSensor;
15 15
         private readonly AgBatterySensor _agBatterySensor;
16 16
         private readonly AgVaccumSensor _agVaccumSensor;
17
+        private readonly AgState _agState;
17 18
 
18 19
         public UiConsole(IEnvironment environment, AgEngineEffector agEngineEffector, AgPickedSensor agPickedSensor,
19
-            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor)
20
+            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor, AgState agState)
20 21
         {
21 22
             _environment = environment;
22 23
             _agEngineEffector = agEngineEffector;
23 24
             _agPickedSensor = agPickedSensor;
24 25
             _agBatterySensor = agBatterySensor;
25 26
             _agVaccumSensor = agVaccumSensor;
27
+            _agState = agState;
26 28
         }
27 29
 
28 30
         public void SetBackgroundColor(ConsoleColor? color)
@@ -74,6 +76,55 @@ namespace uqac_ia_aspirobot.UI
74 76
             }
75 77
         }
76 78
 
79
+        public void SetForegroundColor(ConsoleColor? color)
80
+        {
81
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
82
+            {
83
+                Console.ForegroundColor = color.GetValueOrDefault(ConsoleColor.Black);
84
+            }
85
+            else
86
+            {
87
+                var str = "";
88
+                if (color == null)
89
+                {
90
+                    str = $"{(char)27}[1;39m";
91
+                }
92
+                if (color == ConsoleColor.Black)
93
+                {
94
+                    str = $"{(char)27}[1;30m";
95
+                }
96
+                if (color == ConsoleColor.Red)
97
+                {
98
+                    str = $"{(char)27}[1;31m";
99
+                }
100
+                if (color == ConsoleColor.Green)
101
+                {
102
+                    str = $"{(char)27}[1;32m";
103
+                }
104
+                if (color == ConsoleColor.Yellow)
105
+                {
106
+                    str = $"{(char)27}[1;33m";
107
+                }
108
+                if (color == ConsoleColor.Blue)
109
+                {
110
+                    str = $"{(char)27}[1;34m";
111
+                }
112
+                if (color == ConsoleColor.Magenta)
113
+                {
114
+                    str = $"{(char)27}[1;35m";
115
+                }
116
+                if (color == ConsoleColor.Cyan)
117
+                {
118
+                    str = $"{(char)27}[1;36m";
119
+                }
120
+                if (color == ConsoleColor.Gray)
121
+                {
122
+                    str = $"{(char)27}[1;37m";
123
+                }
124
+                Console.Write(str);
125
+            }
126
+        }
127
+
77 128
         public void Clear()
78 129
         {
79 130
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -118,7 +169,21 @@ namespace uqac_ia_aspirobot.UI
118 169
                         color = ConsoleColor.Red;
119 170
                     }
120 171
                     SetBackgroundColor(color);
121
-                    Console.Write(_agEngineEffector.IsInPosition(x, y) ? "X" : " ");
172
+                    var isDest = _agState.Destination?.IsInPosition(x, y) ?? false;
173
+                    if (_agEngineEffector.IsInPosition(x, y))
174
+                    {
175
+                        Console.Write("X");
176
+                    }
177
+                    else if (isDest)
178
+                    {
179
+                        SetForegroundColor(ConsoleColor.Red);
180
+                        Console.Write("+");
181
+                    }
182
+                    else
183
+                    {
184
+                        Console.Write(" ");
185
+                    }
186
+                    SetForegroundColor(null);
122 187
                     SetBackgroundColor(null);
123 188
                 }
124 189
                 Console.WriteLine("|");
@@ -127,6 +192,7 @@ namespace uqac_ia_aspirobot.UI
127 192
             Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
128 193
             Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
129 194
             Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
195
+            Console.WriteLine($"Think: {_agState.ThinkTimeInterval}ms");
130 196
         }
131 197
     }
132 198
 }

Loading…
Cancel
Save