示例#1
0
    IEnumerator EvaluateBatch(int batchIndex, Phenotype[] batch, Orientations orientation)
    {
        IList <EvaluationBehaviour> evaluations = new List <EvaluationBehaviour>();

        var layout = new TransformLayout(28.0f, 18.0f, batchSize, Mathf.FloorToInt(Mathf.Sqrt(batchSize)))
                     .GetEnumerator();

        foreach (var phenotype in batch)
        {
            layout.MoveNext();

            var t = PoolManager.Pools["Evaluations"].Spawn(prefab, layout.Current, Quaternion.identity, transform);

            var controllerBehaviour = t.GetComponent <ControllerBehaviour>();
            controllerBehaviour.Network = NetworkPorts.FromGenotype(phenotype.Genotype);

            var evaluationBehaviour = t.GetComponent <EvaluationBehaviour>();
            evaluationBehaviour.Phenotype = phenotype;
            evaluationBehaviour.BeginTrial(orientation, Time.time);

            evaluations.Add(evaluationBehaviour);
        }

        // Wait for evaluations to complete
        while (evaluations.Any(ev => !ev.IsComplete))
        {
            if (BestEvaluation != null)
            {
                var ordered = evaluations.OrderByDescending(ev => ev.CurrentTrial.Fitness);
                var best    = ordered.First();
                BestEvaluation(best);
            }
            yield return(new WaitForFixedUpdate());
        }

        // Cleanup
        List <Transform> children = new List <Transform>(transform.childCount);

        foreach (Transform child in transform)
        {
            if (child.gameObject.activeInHierarchy)
            {
                children.Add(child);
            }
        }
        foreach (Transform child in children)
        {
            PoolManager.Pools["Evaluations"].Despawn(child, null);
        }
    }
示例#2
0
    void Awake()
    {
        upper      = transform.Find("Cart/Upper").GetComponent <Rigidbody2D>();
        lower      = transform.Find("Cart/Lower").GetComponent <Rigidbody2D>();
        wheel      = transform.Find("Cart/Wheel").GetComponent <Rigidbody2D>();
        wheelJoint = wheel.transform.GetComponentInChildren <WheelJoint2D>();
        evaluation = GetComponent <EvaluationBehaviour>();

        if (json != null)
        {
            var genotype = NEAT.Genotype.FromJSON(JSON.Deserialize(json.text));
            Assert.AreEqual(JSON.Serialize(genotype.ToJSON()), json.text.Trim(),
                            "JSON should be compatible round-trip");
            Network = NetworkPorts.FromGenotype(genotype);
        }
    }