Exemple #1
0
    void FixedUpdate()
    {
        if (frame == 0)
        {
            // Wait for problem before destroying objects and advancing frame. Unable to destroy and reset in same frame for some reason.
            if (!_signal.WaitOne(5000))
            {
                Debug.LogError("Time out waiting for problem");
                ecj.Abort ();
                Debug.Break();
            }
            destroyAll();
            frame++;
            return;
        } else if (frame == 1)
        {
            resetAll();
        } else if (frame == 3000)
        {
            fitness = Mathf.Abs(Vector3.Distance(ball.gameObject.transform.position, target.gameObject.transform.position));

            problem._signal.Set();
            Debug.Log("End of simulation (frame " + frame + ") time " + (Time.time - time));
            time = Time.time;
            Time.timeScale = scale;
            frame = 0;
            return;
        }

        if ((frame - 1) % 10 == 0)
        {
            problem.currentValue = (float)frame;
            try {
            problem.evaluate();
            } catch (Exception e) {
                ecj.Abort ();
                Debug.Break();

            }
            result = (float)((RegressionData)problem.input).x;
            leftJoint = GameObject.Find("LeftWheel").GetComponent<HingeJoint>();
            rightJoint = GameObject.Find("RightWheel").GetComponent<HingeJoint>();
            if (float.IsInfinity(result) || float.IsNaN(result))
            {
                result = 0;
            }
            if (result > 100f)
                result = 100f;
            else if (result < -100f)
                result = -100f;
            JointMotor leftMotor = leftJoint.motor;
            leftMotor.force = Math.Abs(result);
            leftMotor.targetVelocity = -100f + result * 1.1f;
            leftJoint.motor = leftMotor;
            JointMotor rightMotor = rightJoint.motor;
            rightMotor.targetVelocity = -100f + -result * 1.1f;
            rightMotor.force = Math.Abs(result);
            rightJoint.motor = rightMotor;

            //Debug.Log("Result " + result);
        }

        if (ecj != null)
        {
            if (ecj.Update())
            {
                ecj = null;
                Debug.Log ("Evolution complete");
                Debug.Break();
            }
        }
        frame++;
    }
Exemple #2
0
    void Start()
    {
        Application.runInBackground = true;

        bat = new GameObjectHelper(GameObject.Find("Bat"));
        ball = new GameObjectHelper(GameObject.Find("Ball"));
        target = new GameObjectHelper(GameObject.Find("Target"));
        this.car = new GameObjectHelper(GameObject.Find("Car"));

        Time.timeScale = scale;

        ecj = new EcjJob(this);
        ecj.Start();
    }