Esempio n. 1
0
    private void CreatePredictRequest()
    {
        var predict_x = new List <float>();

        // Find the group middle position
        Vector3 startPos = new Vector3();

        foreach (var waterDrop in waterDrops)
        {
            startPos += waterDrop.transform.position;
        }
        startPos /= waterDrops.Count();

        foreach (var waterDrop in waterDrops)
        {
            var reducedPosition = (waterDrop.transform.position - startPos) / positionReduceFactor;
            var reducedVelocity = waterDrop.GetComponent <WaterDropBody>().Velocity / velocityReduceFactor;

            predict_x.Add(reducedPosition.x);
            predict_x.Add(reducedPosition.y);
            predict_x.Add(reducedPosition.z);
        }

        foreach (var plane in planes)
        {
            var        verticeList    = plane.GetComponent <MeshFilter>().sharedMesh.vertices;
            List <int> verticeIndexes = new List <int> {
                0, 10, 110, 120
            };
            foreach (var verticeIndex in verticeIndexes)
            {
                var corner = (plane.transform.TransformPoint(verticeList[verticeIndex]) - startPos) / positionReduceFactor;
                predict_x.Add(corner.x);
                predict_x.Add(corner.y);
                predict_x.Add(corner.z);
            }
        }

        var request = TelegramFactory.CreatePredictRequest(predict_x);

        externalCommunication.SendAsynch(request, CreatePredictRequestAnswer);
    }
    void NotifyAllFixedUpdateSet(UpdateTracker.UpdateEventType updateEventType)
    {
        if (DateTime.Now - scriptStart < TimeSpan.FromMilliseconds(delayStartMs))
        {
            return;
        }

        float positionReduceFactor = 1000;
        float velocityReduceFactor = 1;
        var   training_x           = new List <float>();
        var   training_y           = new List <float>();

        // Find the group middle position
        Vector3 startPos = new Vector3();

        foreach (var waterDrop in waterDrops)
        {
            startPos += waterDrop.transform.position;
        }
        startPos /= waterDrops.Count();

        foreach (var waterDrop in waterDrops)
        {
            var stateHistory    = waterDrop.GetComponent <StateHistory>();
            var rigidBody       = waterDrop.GetComponent <Rigidbody>();
            var positionHistory = stateHistory.GetHistory <Vector3>(StateHistory.HistoryParameterType.Position);
            var velocityHistory = stateHistory.GetHistory <Vector3>(StateHistory.HistoryParameterType.Velocity);

            if (velocityHistory.Count() == stepsToPredict)
            {
                var lastPosReduced         = (positionHistory.Dequeue() - startPos) / positionReduceFactor;
                var earlierVelocity        = velocityHistory.Dequeue();
                var reducedEarlierVelocity = earlierVelocity / velocityReduceFactor;
                //training_x.Add(reducedEarlierVelocity.x);
                //training_x.Add(reducedEarlierVelocity.y);
                //training_x.Add(reducedEarlierVelocity.z);
                training_x.Add(lastPosReduced.x);
                training_x.Add(lastPosReduced.y);
                training_x.Add(lastPosReduced.z);

                foreach (var laterVelocity in velocityHistory)
                {
                    training_y.Add(laterVelocity.x - earlierVelocity.x);
                    training_y.Add(laterVelocity.y - earlierVelocity.y);
                    training_y.Add(laterVelocity.z - earlierVelocity.z);
                    earlierVelocity = laterVelocity;
                }
                var velocityNow = rigidBody.velocity;
                training_y.Add(velocityNow.x - earlierVelocity.x);
                training_y.Add(velocityNow.y - earlierVelocity.y);
                training_y.Add(velocityNow.z - earlierVelocity.z);
            }
        }

        //VerticeListToShow.Clear();
        foreach (var plane in planes)
        {
            var        verticeList    = plane.GetComponent <MeshFilter>().sharedMesh.vertices;
            List <int> verticeIndexes = new List <int> {
                0, 10, 110, 120
            };
            foreach (var verticeIndex in verticeIndexes)
            {
                var corner = (plane.transform.TransformPoint(verticeList[verticeIndex]) - startPos) / positionReduceFactor;
                training_x.Add(corner.x);
                training_x.Add(corner.y);
                training_x.Add(corner.z);
            }

            //VerticeListToShow.Add(plane.transform.TransformPoint(verticeList[0]));
            //VerticeListToShow.Add(plane.transform.TransformPoint(verticeList[10]));
            //VerticeListToShow.Add(plane.transform.TransformPoint(verticeList[110]));
            //VerticeListToShow.Add(plane.transform.TransformPoint(verticeList[120]));
        }

        bool badTraining = false;

        foreach (var trainingData in training_y)
        {
            if (trainingData > 10 || trainingData < -10)
            {
                badTraining = true;
                Debug.Log("Bad training data: " + trainingData);
            }
        }

        if (!badTraining)
        {
            if (Math.Abs(training_y[1]) < 0.01)
            {
                Debug.LogError("Bad training data which is being sent: " + training_y[1]);
            }
            var request = TelegramFactory.CreateAddTrainingDataRequest(training_x.ToArray(), training_y.ToArray());
            externalCommunication.SendAsynch(request);
        }
    }
Esempio n. 3
0
    void TrainEpoch(Request requestAnswer)
    {
        var beginTrainingRequest = TelegramFactory.CreateBeginTrainingRequest();

        externalCommunication.SendAsynch(beginTrainingRequest, TrainEpoch);
    }
Esempio n. 4
0
    void Start()
    {
        externalCommunication = ExternalCommunication.GetSingleton();
        trainGameObjects      = new List <GameObject>();
        predictGameObjects    = new List <GameObject>();
        if (Train)
        {
            System.Type mType = System.Type.GetType(TrainScriptName);
            gameObject.AddComponent(mType);

            for (int i = 0; i < TrainAmount; i++)
            {
                transform.position = transform.position + new Vector3(
                    Random.Range(-MaxStartDiff, MaxStartDiff),
                    Random.Range(-MaxStartDiff, MaxStartDiff),
                    Random.Range(-MaxStartDiff, MaxStartDiff));

                var newVelocity = new Vector3(
                    Random.Range(-MaxStartVelocity, MaxStartVelocity),
                    Random.Range(-MaxStartVelocity, MaxStartVelocity),
                    Random.Range(-MaxStartVelocity, MaxStartVelocity));

                var trainObject = (Instantiate(TrainPrefab, transform.position, Quaternion.identity));
                trainObject.GetComponent <WaterDropReset>().MaxStartDiff     = MaxStartDiff;
                trainObject.GetComponent <WaterDropReset>().MaxStartVelocity = MaxStartVelocity;
                var stateHistory = trainObject.GetComponent <StateHistory>();
                stateHistory.HistoryParameterTypes.Add(StateHistory.HistoryParameterType.Position);
                stateHistory.HistoryParameterTypes.Add(StateHistory.HistoryParameterType.Velocity);
                stateHistory.StepsToRemember = StepsToRemember;
                trainGameObjects.Add(trainObject);
            }

            new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(CollectTraindataTime * 1000);
                UnityMainThreadDispatcher.Instance().Enqueue(() =>
                {
                    foreach (var trainGameObject in trainGameObjects)
                    {
                        Destroy(trainGameObject);
                    }
                    if (BeginLearningAfterDataGeneration)
                    {
                        var beginTrainingRequest = TelegramFactory.CreateBeginTrainingRequest();
                        externalCommunication.SendAsynch(beginTrainingRequest, TrainEpoch);
                    }
                });
            })).Start();
        }

        if (Predict)
        {
            System.Type mType = System.Type.GetType(PredictScriptName);
            gameObject.AddComponent(mType);

            for (int i = 0; i < PredictAmount; i++)
            {
                var predictObject = (Instantiate(PredictPrefab, transform.position, Quaternion.identity));
                predictGameObjects.Add(predictObject);
            }
        }
    }