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

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
-using System.Linq;
1
+using System;
2
+using System.Linq;
2
 using System.Threading;
3
 using System.Threading;
3
 using Microsoft.Extensions.DependencyInjection;
4
 using Microsoft.Extensions.DependencyInjection;
4
 using Microsoft.Extensions.Options;
5
 using Microsoft.Extensions.Options;
73
             _state = state;
74
             _state = state;
74
             _ui = ui;
75
             _ui = ui;
75
             _options = options.Value;
76
             _options = options.Value;
76
-            _state.SleepTime = _options.SleepTime;
77
+            _state.LastThinkTime = DateTime.MinValue;
78
+            _state.ThinkTimeInterval = _options.ThinkTimeInterval;
77
         }
79
         }
78
 
80
 
79
         private void Run()
81
         private void Run()
85
 
87
 
86
             while (running)
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
                 UpdateSensors();
94
                 UpdateSensors();
93
                 _ui.Update();
95
                 _ui.Update();
104
 
106
 
105
         public void UpdateState()
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
         public void Think()
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
 {
2
 {
3
     public class AgConfig
3
     public class AgConfig
4
     {
4
     {
5
-        public bool AutoAdjustSleepTime { get; set; }
5
+        public bool AutoAdjustThinkTimeInterval { get; set; }
6
 
6
 
7
         public int SleepTime { get; set; }
7
         public int SleepTime { get; set; }
8
 
8
 
9
+        public int ThinkTimeInterval { get; set; }
10
+
9
         public int StartX { get; set; }
11
         public int StartX { get; set; }
10
 
12
 
11
         public int StartY { get; set; }
13
         public int StartY { get; set; }
12
 
14
 
13
         public void CopyTo(AgConfig other)
15
         public void CopyTo(AgConfig other)
14
         {
16
         {
17
+            other.AutoAdjustThinkTimeInterval = AutoAdjustThinkTimeInterval;
15
             other.SleepTime = SleepTime;
18
             other.SleepTime = SleepTime;
19
+            other.ThinkTimeInterval = ThinkTimeInterval;
16
             other.StartX = StartX;
20
             other.StartX = StartX;
17
             other.StartY = StartY;
21
             other.StartY = StartY;
18
         }
22
         }

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

1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
2
 using uqac_ia_aspirobot.Interfaces;
3
 using uqac_ia_aspirobot.Interfaces;
3
 
4
 
4
 namespace uqac_ia_aspirobot.Agent
5
 namespace uqac_ia_aspirobot.Agent
5
 {
6
 {
6
     public class AgState
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
         public IList<IRoom> DustyRooms { get; set; }
13
         public IList<IRoom> DustyRooms { get; set; }
11
 
14
 

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

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

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

1
 using System;
1
 using System;
2
-using System.Collections.Generic;
3
 using System.IO;
2
 using System.IO;
4
 using System.Text;
3
 using System.Text;
5
 
4
 
7
 {
6
 {
8
     public class ArStreamString
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
         public ArStreamString(Stream ioStream)
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
         public string ReadString()
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
             return str;
26
             return str;
31
         }
27
         }
32
 
28
 
44
 
40
 
45
         public int Write(string outString)
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
             return outBuffer.Length + 2;
54
             return outBuffer.Length + 2;
60
         }
55
         }

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

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Linq;
3
 using System.Linq;
4
+using uqac_ia_aspirobot.Agent;
4
 using uqac_ia_aspirobot.Agent.Effectors;
5
 using uqac_ia_aspirobot.Agent.Effectors;
5
 using uqac_ia_aspirobot.Common;
6
 using uqac_ia_aspirobot.Common;
6
 using uqac_ia_aspirobot.Interfaces;
7
 using uqac_ia_aspirobot.Interfaces;
140
             return engine.X == x && engine.Y == y;
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
         public static bool IsInRoom(this AgEngineEffector engine, IRoom room)
149
         public static bool IsInRoom(this AgEngineEffector engine, IRoom room)
144
         {
150
         {
145
             return engine.IsInPosition(room.X, room.Y);
151
             return engine.IsInPosition(room.X, room.Y);

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

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

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

14
         private readonly AgPickedSensor _agPickedSensor;
14
         private readonly AgPickedSensor _agPickedSensor;
15
         private readonly AgBatterySensor _agBatterySensor;
15
         private readonly AgBatterySensor _agBatterySensor;
16
         private readonly AgVaccumSensor _agVaccumSensor;
16
         private readonly AgVaccumSensor _agVaccumSensor;
17
+        private readonly AgState _agState;
17
 
18
 
18
         public UiConsole(IEnvironment environment, AgEngineEffector agEngineEffector, AgPickedSensor agPickedSensor,
19
         public UiConsole(IEnvironment environment, AgEngineEffector agEngineEffector, AgPickedSensor agPickedSensor,
19
-            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor)
20
+            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor, AgState agState)
20
         {
21
         {
21
             _environment = environment;
22
             _environment = environment;
22
             _agEngineEffector = agEngineEffector;
23
             _agEngineEffector = agEngineEffector;
23
             _agPickedSensor = agPickedSensor;
24
             _agPickedSensor = agPickedSensor;
24
             _agBatterySensor = agBatterySensor;
25
             _agBatterySensor = agBatterySensor;
25
             _agVaccumSensor = agVaccumSensor;
26
             _agVaccumSensor = agVaccumSensor;
27
+            _agState = agState;
26
         }
28
         }
27
 
29
 
28
         public void SetBackgroundColor(ConsoleColor? color)
30
         public void SetBackgroundColor(ConsoleColor? color)
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
         public void Clear()
128
         public void Clear()
78
         {
129
         {
79
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
130
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
118
                         color = ConsoleColor.Red;
169
                         color = ConsoleColor.Red;
119
                     }
170
                     }
120
                     SetBackgroundColor(color);
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
                     SetBackgroundColor(null);
187
                     SetBackgroundColor(null);
123
                 }
188
                 }
124
                 Console.WriteLine("|");
189
                 Console.WriteLine("|");
127
             Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
192
             Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
128
             Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
193
             Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
129
             Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
194
             Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
195
+            Console.WriteLine($"Think: {_agState.ThinkTimeInterval}ms");
130
         }
196
         }
131
     }
197
     }
132
 }
198
 }

Loading…
Cancel
Save