Exemplo n.º 1
0
        public override float EvaluateTestCase(GAIndividual inIndividual, object inInput, object inOutput)
        {
            int        timeSteps       = 1000;
            float      timeDiscritized = 0.01f;
            float      maxTime         = timeSteps * timeDiscritized;
            float      captureRadius   = 0.01f;
            ObjectPair xv       = (ObjectPair)inInput;
            float      position = (float)xv._first;
            float      velocity = (float)xv._second;

            for (int step = 1; step <= timeSteps; step++)
            {
                _interpreter.ClearStacks();
                FloatStack   fStack = _interpreter.FloatStack();
                BooleanStack bStack = _interpreter.BoolStack();
                ObjectStack  iStack = _interpreter.InputStack();
                // Position will be on the top of the stack, and velocity will be
                // second.
                fStack.Push(position);
                fStack.Push(velocity);
                // Must be included in order to use the input stack. Uses same order
                // as inputs on Float stack.
                iStack.Push(position);
                iStack.Push(velocity);
                _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
                // If there is no boolean on the stack, the program has failed to
                // return a reasonable output. So, return a penalty fitness of
                // twice the maximum time.
                if (bStack.Size() == 0)
                {
                    return(2 * maxTime);
                }
                // If there is a boolean, use it to compute the next position and
                // velocity. Then, check for termination conditions.
                // NOTE: If result == True, we will apply the force in the positive
                // direction, and if result == False, we will apply the force in
                // the negative direction.
                bool  positiveForce = bStack.Top();
                float acceleration;
                if (positiveForce)
                {
                    acceleration = 0.5f;
                }
                else
                {
                    acceleration = -0.5f;
                }
                velocity = velocity + (timeDiscritized * acceleration);
                position = position + (timeDiscritized * velocity);
                // Check for termination conditions
                if (position <= captureRadius && position >= -captureRadius && velocity <= captureRadius && velocity >= -captureRadius)
                {
                    //Cart is centered, so return time it took.
                    return(step * timeDiscritized);
                }
            }
            // If here, the cart failed to come to rest in the allotted time. So,
            // return the failed error of maxTime.
            return(maxTime);
        }
Exemplo n.º 2
0
        public static void TheProblemAgain()
        {
            var stack = new ObjectStack();

            stack.Push("Hello World");
            stack.Push(100);

            Console.WriteLine(100 * (int)stack.Pop());
            Console.WriteLine(100 * (int)stack.Pop());
        }
Exemplo n.º 3
0
        /// <summary>
        ///     回收对象
        /// </summary>
        /// <param name="obj"></param>
        public void ReleaseObject(T obj)
        {
            if (ReleaseObjectRunTime != null)
            {
                obj = ReleaseObjectRunTime(obj, this);

                if (obj == null)
                {
                    return;
                }
            }
#if lockStack == true
            lock (lockStack)
            {
#endif

            if (ObjectStack.Count >= MaxObjectCount)
            {
                if (obj is IDisposable)
                {
                    ((IDisposable)obj).Dispose();
                }
            }
            else
            {
                I3++;
                ObjectStack.Push(obj);
            }


#if lockStack == true
        }
#endif
        }
 public static void FillData()
 {
     for (var i = 0; i < 10; i++)
     {
         ObjectStack.Push(i + 1);
     }
 }
Exemplo n.º 5
0
    static void Main(string[] args)
    {
        // create a new IntStack
        ObjectStack stack = new ObjectStack();

        stack.Push(2);
        stack.Push("apple");
        stack.Push(8);

        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Pop value: {0}", stack.Pop());
        }

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Exemplo n.º 6
0
        public void InvalidCastExceptionTest()
        {
            // Now suppose we want a stack that stores just integers:
            ObjectStack stack = new ObjectStack();

            // It's easy to make mistakes:
            stack.Push("s");          // Wrong type, but no error!
            int i = (int)stack.Pop(); // Downcast - runtime error!
        }
Exemplo n.º 7
0
        public void StartObject(ISerializable obj)
        {
            bool isSameAsPreviousObject = IsObjectSameAsPreviousObject(obj);

            ObjectStack.Push(obj);
            if (!isSameAsPreviousObject)
            {
                JsonWriter.WriteStartObject();
                SerializationManager.StartObjectSerialization(this, obj);
            }
        }
Exemplo n.º 8
0
        public void InvalidCastExceptionTest()
        {
            // Now suppose we want a stack that stores just integers:
            var intStack = new ObjectStack();

            // It's easy to make mistakes:
            intStack.Push("s"); // Wrong type, but no error!

            Assert.Throws <System.InvalidCastException>(() =>
            {
                _ = (int)intStack.Pop();  // Downcast - runtime error!
            });
        }
        private static void OnExceptionRestoreStack()
        {
            foreach (var i in BackUpList)
            {
                ObjectStack.Push(i);
            }
            var initialCount = ObjectStack.Count;

            Console.WriteLine(Environment.NewLine);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Restored Stack Contents Count {0}", ObjectStack.Count);
            Console.WriteLine("Contents from Restored Stack");
            for (var i = 0; i < initialCount; i++)
            {
                Console.WriteLine("Restored value {0} ,  at Position {1}", ObjectStack.Pop(), i);
            }
            Console.WriteLine("Restored Stack To Initial State");
        }
Exemplo n.º 10
0
        public static void Main()
        {
            string      input;
            ObjectStack os = new ObjectStack(1);

            do
            {
                System.Console.WriteLine($"Length: {os.Size}");
                System.Console.Write("Enter a string: ");
                input = Console.ReadLine();
                if (input != "")
                {
                    os.Push(input);
                }
            } while(input != "");

            Console.WriteLine("\nPopping the stack!");
            while (!os.Empty)
            {
                Console.WriteLine(os.Pop());
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///     回收对象
        /// </summary>
        /// <param name="obj"></param>
        public void ReleaseObject(T obj)
        {
            if (ReleaseObjectRunTime != null)
            {
                obj = ReleaseObjectRunTime(obj, this);

                if (obj == null)
                {
                    return;
                }
            }

            if (ObjectStack.Count >= MaxObjectCount)
            {
                if (obj is IDisposable)
                {
                    ((IDisposable)obj).Dispose();
                }
            }
            else
            {
                ObjectStack.Push(obj);
            }
        }