Esempio n. 1
0
        /// <summary>
        /// After the quality matrix has been computed,
        /// it can be used to find an optimal path from any starting state to the goal state.
        /// the method assumes that the goal state is reachable from the starting state
        /// </summary>
        /// <param name="qMaze"></param>
        /// <param name="quality"></param>
        private static void Walk(QMaze qMaze, double[][] quality)
        {
            int curr = qMaze.Start; int next;

            /* original algorithm assumed fixed start. removed due to needing random sequence.
             * this however prevents the ability to walk the maze after the solution is found because.
             * the alogrithm does not set quality values for the starting rank.
             * instead, will use no-op as the fixed staring pointing*/
            /*
             * int maxI = 0;
             * int maxK = 0;
             * var bestQ = double.MinValue;
             * for (var i = 0; i < quality.Length; i++)
             * {
             * for (var k = 0; k < quality[i].Length; k++)
             * {
             *     if (quality[i][k] > bestQ)
             *     {
             *         bestQ = quality[i][k];
             *         maxI = i;
             *         maxK = k;
             *     }
             * }
             * }
             * var opCodeI = QOpCodeLearingGenerator.OpCodes[maxI];
             * var opCodeK = QOpCodeLearingGenerator.OpCodes[maxK];
             */
            var           opCode   = QOpCodeLearingGenerator.OpCodes[curr];
            List <OpCode> solution = new List <OpCode>();

            solution.Add(opCode);
            Console.Write(opCode + "->");
            while (curr != qMaze.Goal)
            {
                next   = ArgMax(quality[curr]);
                opCode = QOpCodeLearingGenerator.OpCodes[next];
                solution.Add(opCode);
                Console.Write(opCode + "->");
                curr = next;
            }

            var writer = new ILInstructionWriter(solution);
            List <ILInstruction> ilInstructions = writer.GetInstructionStream();
            var engine = new ILInstructionEngine();

            dynamic[] args   = new dynamic[] { 1 };
            var       result = engine.ExecuteTyped(ilInstructions, args: args);

            Console.WriteLine("done");
        }
Esempio n. 2
0
        private static ExecutionResult ExecuteOpCodes(List <OpCode> l, int timeoutSeconds = 3, params dynamic[] args)
        {
            ExecutionResult result = new ExecutionResult();

            try
            {
                var executionOpCodes = l.ToArray().Concat(new[] { OpCodes.Ret }).ToList();
                var writer           = new ILInstructionWriter(executionOpCodes);
                List <ILInstruction> instructions = writer.GetInstructionStream();
                result.ExecutionState = new ExecutionState()
                {
                    ilInstructions = instructions, Arguments = args
                };
                TimeSpan timeout = TimeSpan.FromSeconds(timeoutSeconds);
                using (var src = new CancellationTokenSource())
                {
                    var startParams = new ParameterizedThreadStart(ExecuteInstructions);
                    var t           = new Thread(startParams);
                    t.Start(result.ExecutionState);
                    var threadTask = Task.Run(() => t.Join());
                    Task.WaitAny(threadTask, Task.Delay(timeout, src.Token));
                    if (threadTask.IsCompleted)
                    {
                        src.Cancel();
                        if (result.ExecutionState.Error != null)
                        {
                            result.Error    = result.ExecutionState.Error;
                            result.TimedOut = result.ExecutionState.Error is TimeoutException;
                            result.Success  = false;
                        }
                        else
                        {
                            result.Success = true;
                            result.Result  = result.ExecutionState.Result;
                        }
                    }
                    else
                    {
                        t.Abort();
                        throw new ExecutionEngineTimeoutException(result.ExecutionState, timeout);
                    }
                }

                //return result;
            }
            catch (ExecutionEngineTimeoutException eete)
            {
                result.Error   = eete;
                result.Success = false;

                System.Diagnostics.Debug.WriteLine($"Error executing opcodes: {eete.Message}");
            }
            catch (ExecutionEngineException engExc)
            {
                result.Error   = engExc;
                result.Success = false;
                System.Diagnostics.Debug.WriteLine($"Error executing opcodes: {engExc.InnerException.Message}");
            }
            catch (Exception ex)
            {
                result.Error   = ex;
                result.Success = false;
                System.Diagnostics.Debug.WriteLine($"Error executing opcodes: {ex.Message}");
            }
            return(result);
        }