示例#1
0
        public void CopyToManagedArrayCopiesElements()
        {
            using (NativeArray2D <int> src = CreateArray(2, 3))
            {
                NativeArray2D <int> srcAlias = src;
                srcAlias[0, 0] = 100;
                srcAlias[0, 1] = 200;
                srcAlias[0, 2] = 300;
                srcAlias[1, 0] = 400;
                srcAlias[1, 1] = 500;
                srcAlias[1, 2] = 600;

                int[,] dest = new int[2, 3];

                src.CopyTo(dest);

                Assert.That(dest[0, 0], Is.EqualTo(src[0, 0]));
                Assert.That(dest[0, 1], Is.EqualTo(src[0, 1]));
                Assert.That(dest[0, 2], Is.EqualTo(src[0, 2]));
                Assert.That(dest[1, 0], Is.EqualTo(src[1, 0]));
                Assert.That(dest[1, 1], Is.EqualTo(src[1, 1]));
                Assert.That(dest[1, 2], Is.EqualTo(src[1, 2]));
            }
        }
示例#2
0
 public NativeArray2DDebugView(NativeArray2D <T> array)
 {
     m_array = array;
 }
示例#3
0
 public bool Equals(NativeArray2D <T> other)
 {
     return(m_data == other.m_data);
 }
示例#4
0
 public ColorData(Color[] data, int dataLengthX, float threshold, bool useInterpolation)
 {
     this.data             = new NativeArray2D <Color>(data, dataLengthX, data.Length / dataLengthX, Allocator.Persistent);
     this.threshold        = threshold;
     this.useInterpolation = useInterpolation;
 }
示例#5
0
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                //Writeable.
                NativeArray <PathFinding> pathfindingArray       = chunk.GetNativeArray(pathFindingComponentHandle);
                BufferAccessor <PathNode> pathNodeBufferAccessor = chunk.GetBufferAccessor(pathNodeBufferHandle);

                //Read Only.
                NativeArray <CurrentTarget> currentTargetArray = chunk.GetNativeArray(currentTargetComponentHandle);
                NativeArray <Translation>   translationArray   = chunk.GetNativeArray(translationComponentHandle);
                NativeArray <Entity>        entities           = chunk.GetNativeArray(entityType);

                //Create a copy of the grid for this thread and chunk.
                NativeArray2D <MapNode> gridCopy = new NativeArray2D <MapNode>(gridRef.Length0, gridRef.Length1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

                UnsafeUtility.MemCpy(gridCopy.GetUnsafePtr(), gridRef.GetUnsafePtrReadOnly(), gridRef.Length * sizeof(MapNode));

                for (int indexInChunk = 0; indexInChunk < chunk.Count; ++indexInChunk)
                {
                    //Writable.
                    PathFinding pathfinding       = pathfindingArray[indexInChunk];
                    DynamicBuffer <PathNode> path = pathNodeBufferAccessor[indexInChunk];

                    //Read Only.
                    Entity        entity        = entities[indexInChunk];
                    CurrentTarget currentTarget = currentTargetArray[indexInChunk];
                    Translation   translation   = translationArray[indexInChunk];

                    bool hasPath = chunk.Has(hasPathComponentHandle);

                    if (!pathfinding.requestedPath)
                    {
                        //We only want to remove our has path component if we didn't request a new one, to avoid re-adding later in the job if we find a new path.
                        if (pathfinding.completedPath)
                        {
                            pathfinding.completedPath = false;
                            ecb.RemoveComponent <HasPathTag>(chunkIndex, entity);
                            pathfindingArray[indexInChunk] = pathfinding;
                        }

                        continue;
                    }

                    path.Clear();
                    pathfinding.currentIndexOnPath = 0;
                    pathfinding.completedPath      = false;
                    pathfinding.requestedPath      = false;

                    //Calculate the closest nodes to us and our target position.
                    //Don't search for path if we're already at our target node.
                    pathfinding.currentNode = FindNearestNode(translation.Value, gridCopy);
                    pathfinding.targetNode  = FindNearestNode(currentTarget.targetData.targetPos, gridCopy);
                    if (pathfinding.targetNode.Equals(pathfinding.currentNode))
                    {
                        pathfindingArray[indexInChunk] = pathfinding;
                        continue;
                    }

                    CalculateGridH(gridCopy, ref pathfinding);

                    bool pathfound = SearchForPath(pathfinding.currentNode, pathfinding.targetNode, gridCopy);

                    if (pathfound)
                    {
                        ConstructPath(gridCopy, ref pathfinding, ref path);

                        if (!hasPath)
                        {
                            ecb.AddComponent <HasPathTag>(chunkIndex, entity);
                        }
                    }
                    else if (hasPath)
                    {
                        ecb.RemoveComponent <HasPathTag>(chunkIndex, entity);
                    }

                    pathfindingArray[indexInChunk] = pathfinding;
                }

                gridCopy.Dispose();
            }
示例#6
0
        public void IsCreatedReturnsTrueForDefaultStruct()
        {
            NativeArray2D <int> array = default(NativeArray2D <int>);

            Assert.That(array.IsCreated, Is.False);
        }
示例#7
0
        private IEnumerator TrainingProcess()
        {
            var neuralNetworkConfig = new NetworkDescription()
            {
                Layers = new List <LayerParamaters>()
                {
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Input,
                        NeuronCount = InputAttributeCount,
                        NeuronType  = ENeruonType.Input
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Hidden,
                        NeuronCount = 64,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Hidden,
                        NeuronCount = 16,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Output,
                        NeuronCount = 1,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                }
            };

            var neuralNetwork = new NeuralNetwork(neuralNetworkConfig);

            neuralNetwork.InitNetworkWithRandomValues(-0.05f, 0.05f);

            var networkEvaluator = new NetworkEvaluator(neuralNetwork);


            var tempResult = new NativeArray2D <float>(1, 1);

            //Test inital accuracy
            TestInitialAccuracy(networkEvaluator, tempResult.Slice());

            //Learn over a number of iterations
            for (int epoch = 0; epoch < Epochs; epoch++)
            {
                //For each epoch, perform training
                for (int tc = 0; tc < _dataset.TrainingSetSize; tc++)
                {
                    _dataset.GetTrainingCase(tc, out var trainingInput, out var trainingResult);

                    //Evolve network
                    var jobHandle = networkEvaluator.GradientDescentBackpropigate(trainingInput, trainingResult, 1, out _);
                    jobHandle.Complete();
                }

                float totalError   = 0.0f;
                int   totalCorrect = 0;

                for (int i = 0; i < _dataset.TestingSetSize; i++)
                {
                    _dataset.GetTestCase(i, out var trainingInput, out var trainingResult);
                    networkEvaluator.Evaluate(trainingInput, tempResult.Slice(), 1).Complete();

                    var  error      = Math.Abs(tempResult[0, 0] - trainingResult[0, 0]);
                    bool wasCorrect = error < 0.5f;
                    totalCorrect += wasCorrect ? 1 : 0;
                    totalError   += error;
                }

                float averageError = totalError / (float)_dataset.TestingSetSize;
                float accuracy     = (float)totalCorrect / (float)_dataset.TestingSetSize;

                //Forward test
                Debug.Log($"Epoch {epoch}: Accuracy:{accuracy:P2}  Average Error:{averageError:F4}");

                yield return(null);
            }

            tempResult.BackingStore.Dispose();
        }
示例#8
0
 public BoolData(NativeArray2D <bool> data)
 {
     this.data = data;
 }
示例#9
0
 public BoolData(bool[,] data)
 {
     this.data = new NativeArray2D <bool>(data, Allocator.Persistent);
 }