Beispiel #1
0
        public override float EvaluateTestCase(GAIndividual inIndividual, object inInput, object inOutput)
        {
            _effort++;
            _interpreter.ClearStacks();
            _currentInput = (float)inInput;
            FloatStack fstack = _interpreter.FloatStack();

            fstack.Push(_currentInput);
            // Must be included in order to use the input stack.
            _interpreter.InputStack().Push(_currentInput);
            _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
            float result = fstack.Top();

            // System.out.println( _interpreter + " " + result );
            //trh

            /*
             * System.out.println("\nevaluations according to interpreter " +
             * Interpreter.GetEvaluationExecutions());
             * System.out.println("evaluations according to effort " + _effort);
             */
            // Penalize individual if there is no result on the stack.
            if (fstack.Size() == 0)
            {
                return(_noResultPenalty);
            }
            return(result - ((float)inOutput));
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public override void Execute(Interpreter inI)
        {
            ObjectStack codeStack = inI.CodeStack();
            FloatStack  fStack    = inI.FloatStack();

            if (fStack.Size() > 0)
            {
                codeStack.Push(fStack.Pop());
            }
        }
Beispiel #4
0
        protected void setUp()
        {
            interpreter = new Interpreter();
            Program instructionList = new Program("( )");

            interpreter.randProgram.SetInstructions(interpreter, instructionList);
            istack = new IntStack();
            fstack = new FloatStack();
            bstack = new BooleanStack();
        }
Beispiel #5
0
        public override void Execute(Interpreter interpeter)
        {
            FloatStack stack = interpeter.FloatStack();

            if (stack.Size() > 0)
            {
                Console.Error.WriteLine(stack.Top());
            }
            else
            {
                Console.Error.WriteLine("empty");
            }
        }
        public override float EvaluateTestCase(GAIndividual inIndividual, object inInput, object inOutput)
        {
            _interpreter.ClearStacks();
            _currentInput = (float)inInput;
            FloatStack stack = _interpreter.FloatStack();

            stack.Push(_currentInput);
            _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
            float result = stack.Top();

            // System.out.println( _interpreter + " " + result );
            return(result - ((float)inOutput));
        }
Beispiel #7
0
        public override void Execute(Interpreter interpeter)
        {
            FloatStack stack = interpeter.FloatStack();

            if (stack.Size() >= 1)
            {
                // we're good
                float slope = stack.Pop();
                stack.Push((float)Math.Atan(slope));
            }
            else
            {
                stack.Push(0);
            }
        }
        public override float EvaluateTestCase(GAIndividual inIndividual, object inInput, object inOutput)
        {
            _interpreter.ClearStacks();
            float      currentInput = (float)inInput;
            FloatStack stack        = _interpreter.FloatStack();

            stack.Push(currentInput);
            // Must be included in order to use the input stack.
            _interpreter.InputStack().Push(currentInput);
            _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
            float result = stack.Top();

            // Penalize individual if there is no result on the stack.
            if (stack.Size() == 0)
            {
                return(_noResultPenalty);
            }
            return(result - ((float)inOutput));
        }
        public virtual float GetIndividualTestCaseResult(GAIndividual inIndividual, GATestCase inTestCase)
        {
            _interpreter.ClearStacks();
            float      currentInput = (float)inTestCase._input;
            FloatStack stack        = _interpreter.FloatStack();

            stack.Push(currentInput);
            // Must be included in order to use the input stack.
            _interpreter.InputStack().Push(currentInput);
            _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
            float result = stack.Top();

            // If no result, return 0
            if (stack.Size() == 0)
            {
                return(0);
            }
            return(result);
        }
Beispiel #10
0
        public void PushInput(Interpreter inI, int n)
        {
            ObjectStack _stack = inI.InputStack();

            if (_stack.Size() > n)
            {
                object inObject = _stack.DeepPeek(n);
                if (inObject is int)
                {
                    IntStack istack = inI.IntStack();
                    istack.Push((int)inObject);
                }
                else
                {
                    // if (inObject is Number)
                    // {
                    //   FloatStack fstack = inI.FloatStack();
                    //   fstack.Push(((Number)inObject).FloatValue());
                    // }
                    //else
                    if (inObject is float)
                    {
                        FloatStack fstack = inI.FloatStack();
                        fstack.Push((float)inObject);
                    }
                    else
                    {
                        if (inObject is bool)
                        {
                            BooleanStack bstack = inI.BoolStack();
                            bstack.Push((bool)inObject);
                        }
                        else
                        {
                            Console.Error.WriteLine("Error during input.index - object " + inObject.GetType() +
                                                    " is not a legal object according to " + this.GetType() + ".");
                        }
                    }
                }
            }
        }