예제 #1
0
        /// <summary>
        /// Points and Movement.
        /// </summary>
        /// <param name="gameObject"></param>
        /// <param name="id"></param>
        public void UpdateCar(GameObject gameObject)
        {
            Rigidbody rigidbody = gameObject.GetComponent <Rigidbody>();

            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = new Vector3(0, 0, 0);

            Sensores(gameObject);
            mutationUpdate();

            //Update stats
            //Used to create points
            NeuralChildObject _stats = GetStats(gameObject);

            time = Time.time - _stats.DriveTime;
            if (timeLimit != 0)
            {
                if (time > timeLimit)
                {
                    GameOver(gameObject, null, true);
                }
            }

            if (_stats.AverageSpeed == 0)
            {
                _stats.AverageSpeed = rigidbody.velocity.magnitude;
            }
            else
            {
                _stats.AverageSpeed = (_stats.AverageSpeed + rigidbody.velocity.magnitude) / 2;
            }

            //End Update stats
            if (newNetworkVersion)
            {
                try
                {
                    results = newNetworks.Compute(sensors);
                }
                catch (Exception)
                {
                    newNetworks = new NeuralNetwork.NetworkModels.Network(parameters[0], new int[] { 8, 8, 8 }, 4, 2, 1);
                }
            }
            else
            {
                try
                {
                    results = networks[_stats.Id].process(sensors);
                }
                catch (Exception)
                {
                    networks[_stats.Id] = new Network(parameters);
                    results             = networks[_stats.Id].process(sensors);
                }
            }

            frontForce  = (float)results[0];
            backForce   = (float)results[1];
            leftForce   = (float)results[2];
            leftForce  += (float)results[3];
            rightForce  = (float)results[4];
            rightForce += (float)results[5];

            if (frontForce >= maxSpeed)
            {
                frontForce = maxSpeed;
            }
            if (backForce >= maxSpeed)
            {
                backForce = maxSpeed;
            }

            _stats.Speed += (frontForce / 14);
            _stats.Speed -= (backForce / 8) / (_stats.Speed * 16);
            speed         = _stats.Speed;
            if (_stats.Speed <= -maxSpeed)
            {
                _stats.Speed = -maxSpeed;
            }
            else if (_stats.Speed >= maxSpeed)
            {
                _stats.Speed = maxSpeed;
            }

            _stats.DriveDistance = Vector3.Distance(gameObject.transform.position, startPosition);

            _stats.LastPosition = gameObject.transform.position;

            gameObject.transform.Rotate(0, (leftForce - rightForce) * 2, 0);

            gameObject.transform.Translate(0, 0, _stats.Speed);

            //gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(0,0, _stats.speed), 0.5f * Time.deltaTime);
            _stats.SenoreHistory.Add(sensors);
            _stats.ResultHistory.Add(results);
            SetStats(gameObject, _stats);
            if (ignoreCollide)
            {
                ignoreCollide = false;
            }
        }
예제 #2
0
        // Use this for initialization
        void Start()
        {
            childFolder      = new GameObject();
            childFolder.name = "[" + transform.root.gameObject.name + "] Childs";
            stats            = new NeuralChildObject();
            InvokeRepeating("mutationUpdate", 1, 1);
            startPosition      = transform.position;
            startRotation      = transform.rotation;
            stats.LastPosition = startPosition;

            ignoreCollide = ignoreFirstCollide;

            Debug.Log("[" + transform.root.gameObject.name + "] Generation " + generation);

            results = new double[4];
            points  = new double[population];
            sensors = new double[10];

            networks             = new Network[population];
            newNetworks          = new NeuralNetwork.NetworkModels.Network(parameters[0], new int[] { 8, 8, 8 }, 4, 2, 1);
            gameObjectsChilds    = new GameObject[population];
            gameObjectsChilds[0] = this.gameObject;
            NeuralWeights loadedWeightsValues = null;

            if (LoadLastNerual)
            {
                string m_Path = Application.dataPath + "/" + transform.root.gameObject.name + ".xml";
                print(m_Path);
                loadedWeightsValues = Load(m_Path);
                if (loadedWeightsValues != null)
                {
                    bestScore  = loadedWeightsValues.bestScore;
                    generation = loadedWeightsValues.Generation;
                    print("[" + transform.root.gameObject.name + "] LoadedWeights");
                }
            }

            if (newNetworkVersion)
            {
                if (loadedWeightsValues != null)
                {
                    newNetworks.HiddenLayers = loadedWeightsValues.HiddenLayers;
                    newNetworks.InputLayer   = loadedWeightsValues.InputLayer;
                    newNetworks.OutputLayer  = loadedWeightsValues.OutputLayer;
                }
                for (int i = 1; i < population; i++)
                {
                    //Spawn childs if wanted and can
                    if (spawnChild && child != null)
                    {
                        SpawnChild();
                    }
                }
            }
            else
            {
                networks[0] = new Network(parameters);

                for (int i = 1; i < population; i++)
                {
                    //Spawn childs if wanted and can
                    if (spawnChild && child != null)
                    {
                        SpawnChild();
                    }
                    if (loadedWeightsValues == null)
                    {
                        networks[i] = new Network(parameters);
                    }
                    else
                    {
                        networks[i] = new Network(parameters);
                        networks[i].setWeights(loadedWeightsValues.weights);
                        networks[i] = new Network(this, networks[i]);
                    }
                }
            }
        }