/// <summary> Calculates the changes that must be made to the network based on the given <paramref name="error"/>. Adds the changes to the given <paramref name="networkChange"/>. </summary> /// <param name="networkChange"> The changes to be made to the network, any changes calculated here are added to this parameter. </param> /// <param name="error"> The error of the network's last output. </param> /// <remarks> Note that this uses the state of the <see cref="NeuralNetwork"/>, so this should be called immediately after the input is processed. </remarks> public void CalculateChanges(NetworkChange networkChange, IReadOnlyList <float> error) { // Ensure the given array is of the correct length. if (error.Count != outputLayer.Count) { throw new ArgumentException("Given error collection does not match the number of output neurons in the network."); } // Set the current delta to the given error. IReadOnlyList <float> currentDelta = error; // Go over each non-input layer and calculate the required changes. for (uint i = (uint)(neuronLayers.Length - 1); i >= 1; i--) { // Calculate the changes to this layer, saving the delta. neuronLayers[i].CalculateChanges(networkChange.GetNeuronLayerChange(i), networkChange.GetWeightLayerChange(i - 1), currentDelta); // Set the current delta to the delta of the current layer that was just calculated. if (i < neuronLayers.Length - 1) { currentDelta = networkChange.GetNeuronLayerChange(i).Delta; } } }
public async static Task ProcessClient(EngineContext engineContext, SocketContext socketContext, SocketContext socketContextAsync) { socketContext.AddPacketHandler <DownloadFileQuery>( async(packet) => { var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Open, VirtualFileAccess.Read); var data = new byte[stream.Length]; await stream.ReadAsync(data, 0, data.Length); stream.Close(); socketContext.Send(new DownloadFileAnswer { StreamId = packet.StreamId, Data = data }); }); socketContext.AddPacketHandler <UploadFilePacket>( async(packet) => { var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Create, VirtualFileAccess.Write); await stream.WriteAsync(packet.Data, 0, packet.Data.Length); stream.Close(); }); var viewModelGlobalContext = new ViewModelGlobalContext(); selectedEntitiesContext = new ViewModelContext(viewModelGlobalContext); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new EntityComponentEnumerator(engineContext)); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new RenderPassPluginEnumerator()); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); //selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new EffectPropertyEnumerator(engineContext)); var renderPassHierarchyContext = new ViewModelContext(viewModelGlobalContext); renderPassHierarchyContext.ChildrenPropertyEnumerators.Add(new RenderPassHierarchyEnumerator()); renderPassHierarchyContext.Root = new ViewModelNode("Root", engineContext.RenderContext.RootRenderPass).GenerateChildren(renderPassHierarchyContext); var renderPassPluginsContext = new ViewModelContext(viewModelGlobalContext); renderPassPluginsContext.ChildrenPropertyEnumerators.Add(new RenderPassPluginsEnumerator { SelectedRenderPassPluginContext = selectedEntitiesContext }); renderPassPluginsContext.Root = new ViewModelNode("Root", new EnumerableViewModelContent <ViewModelReference>( () => engineContext.RenderContext.RenderPassPlugins.Select(x => new ViewModelReference(x, true)))); var entityHierarchyEnumerator = new EntityHierarchyEnumerator(engineContext.EntityManager, selectedEntitiesContext); var entityHierarchyContext = new ViewModelContext(viewModelGlobalContext); entityHierarchyContext.ChildrenPropertyEnumerators.Add(entityHierarchyEnumerator); entityHierarchyContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); entityHierarchyContext.Root = new ViewModelNode("EntityHierarchyRoot", new EnumerableViewModelContent <ViewModelReference>( () => engineContext.EntityManager.Entities .Where(x => { var transformationComponent = x.Transformation; return(transformationComponent == null || transformationComponent.Parent == null); }) .Select(x => new ViewModelReference(x, true)))); entityHierarchyEnumerator.SelectedEntities.CollectionChanged += (sender, args) => { SelectEntity(entityHierarchyEnumerator.SelectedEntities); }; //entityHierarchyContext.Root.Children.Add(new ViewModelNode("SelectedItems", EnumerableViewModelContent.FromUnaryLambda<ViewModelReference, ViewModelReference>(new NullViewModelContent(), // (x) => { return new[] { new ViewModelReference(pickingSystem.SelectedEntity) }; }))); /*(value) => * { * var entityModelView = value != null ? entityHierarchyContext.GetModelView(value.Guid) : null; * var entity = entityModelView != null ? (Entity)entityModelView.NodeValue : null; * SelectEntity(entity); * })));*/ entityHierarchyContext.Root.Children.Add(new ViewModelNode("DropEntity", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) => { var dropParameters = (DropCommandParameters)parameter; var movedItem = dropParameters.Data is Guid ? entityHierarchyContext.GetModelView((Guid)dropParameters.Data) : null; var newParent = dropParameters.Parent is Guid ? entityHierarchyContext.GetModelView((Guid)dropParameters.Parent) : null; if (newParent == null || movedItem == null) { return; } var parent = ((Entity)newParent.NodeValue).Transformation; if (dropParameters.TargetIndex > parent.Children.Count) { return; } var transformationComponent = ((Entity)movedItem.NodeValue).Transformation; transformationComponent.Parent = null; parent.Children.Insert(dropParameters.TargetIndex, transformationComponent); })))); entityHierarchyContext.Root.Children.Add(new ViewModelNode("DropAsset", new RootViewModelContent((ExecuteCommand)(async(viewModel2, parameter) => { var dropParameters = (DropCommandParameters)parameter; var assetUrl = (string)dropParameters.Data; /*var newParent = entityHierarchyContext.GetModelView((Guid)dropParameters.Parent); * * if (newParent == null || assetUrl == null) * return; * * var parent = ((Entity)newParent.NodeValue).Transformation; * if (dropParameters.ItemIndex > parent.Children.Count) * return;*/ engineContext.Scheduler.Add(async() => { // Load prefab entity var loadedEntityPrefab = await engineContext.AssetManager.LoadAsync <Entity>(assetUrl + "#"); // Build another entity from prefab var loadedEntity = Prefab.Inherit(loadedEntityPrefab); // Add it to scene engineContext.EntityManager.AddEntity(loadedEntity); if (loadedEntity.ContainsKey(AnimationComponent.Key)) { Scheduler.Current.Add(() => AnimScript.AnimateFBXModel(engineContext, loadedEntity)); } }); })))); var scriptEngineContext = new ViewModelContext(viewModelGlobalContext); scriptEngineContext.ChildrenPropertyEnumerators.Add(new ScriptAssemblyEnumerator(engineContext)); scriptEngineContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); scriptEngineContext.Root = new ViewModelNode(new EnumerableViewModelContent <ViewModelReference>( () => engineContext.ScriptManager.ScriptAssemblies.Select(x => new ViewModelReference(x, true)))); scriptEngineContext.Root.Children.Add(new ViewModelNode("RunScript", new RootViewModelContent((ExecuteCommand)(async(viewModel2, parameter) => { var scriptName = (string)parameter; var matchingScript = engineContext.ScriptManager.Scripts.Where(x => x.TypeName + "." + x.MethodName == scriptName); if (matchingScript.Any()) { var scriptEntry = matchingScript.Single(); var microThread = engineContext.ScriptManager.RunScript(scriptEntry, null); } })))); var runningScriptsContext = new ViewModelContext(viewModelGlobalContext); runningScriptsContext.ChildrenPropertyEnumerators.Add(new MicroThreadEnumerator(selectedEntitiesContext)); runningScriptsContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); runningScriptsContext.Root = new ViewModelNode("MicroThreads", new EnumerableViewModelContent <ViewModelReference>( () => engineContext.Scheduler.MicroThreads.Select(x => new ViewModelReference(x, true)) )); var effectsContext = new ViewModelContext(viewModelGlobalContext); effectsContext.ChildrenPropertyEnumerators.Add(new EffectEnumerator(selectedEntitiesContext)); effectsContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); effectsContext.Root = new ViewModelNode("Effects", new EnumerableViewModelContent <ViewModelReference>( () => engineContext.RenderContext.Effects.Select(x => new ViewModelReference(x, true)) )); //effectsContext.Root.Children.Add(new ViewModelNode("PluginDefinitions", new RootViewModelContent())); var assetBrowserContext = new ViewModelContext(viewModelGlobalContext); assetBrowserContext.ChildrenPropertyEnumerators.Add(new AssetBrowserEnumerator(engineContext)); assetBrowserContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); assetBrowserContext.Root = new ViewModelNode("Root", "Root").GenerateChildren(assetBrowserContext); var editorContext = new ViewModelContext(viewModelGlobalContext); editorContext.Root = new ViewModelNode("Root"); editorContext.Root.Children.Add(new ViewModelNode("SwitchSelectionMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.None; }))); editorContext.Root.Children.Add(new ViewModelNode("SwitchTranslationMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.Translation; }))); editorContext.Root.Children.Add(new ViewModelNode("SwitchRotationMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.Rotation; }))); var contexts = new Dictionary <string, Tuple <ViewModelContext, ViewModelState> >(); contexts.Add("Editor", Tuple.Create(editorContext, new ViewModelState())); contexts.Add("RenderPassPlugins", Tuple.Create(renderPassPluginsContext, new ViewModelState())); contexts.Add("RenderPasses", Tuple.Create(renderPassHierarchyContext, new ViewModelState())); contexts.Add("SelectedEntities", Tuple.Create(selectedEntitiesContext, new ViewModelState())); contexts.Add("EntityHierarchy", Tuple.Create(entityHierarchyContext, new ViewModelState())); contexts.Add("ScriptEngine", Tuple.Create(scriptEngineContext, new ViewModelState())); contexts.Add("MicroThreads", Tuple.Create(runningScriptsContext, new ViewModelState())); contexts.Add("AssetBrowser", Tuple.Create(assetBrowserContext, new ViewModelState())); contexts.Add("Effects", Tuple.Create(effectsContext, new ViewModelState())); int lastAckPacket = 0; var entitiesChangePackets = new ConcurrentQueue <EntitiesChangePacket>(); socketContext.AddPacketHandler <EntitiesChangePacket>( (packet) => { entitiesChangePackets.Enqueue(packet); entitiesChangePacketEvent.Set(); }); Action asyncThreadStart = () => { while (true) { Thread.Sleep(100); foreach (var context in contexts) { // Process async data Guid[] path = null; object value = null; lock (context.Value.Item1) { var pendingNode = context.Value.Item1.GetNextPendingAsyncNode(); if (pendingNode != null) { value = pendingNode.Value; path = ViewModelController.BuildPath(pendingNode); } } if (path != null) { // Temporary encoding through our serializer (until our serializer are used for packets) var memoryStream = new MemoryStream(); var writer = new BinarySerializationWriter(memoryStream); writer.SerializeExtended(null, value, ArchiveMode.Serialize); var change = new NetworkChange { Path = path.ToArray(), Type = NetworkChangeType.ValueUpdateAsync, Value = memoryStream.ToArray() }; var packet = new EntitiesChangePacket { GroupKey = context.Key, Changes = new NetworkChange[] { change } }; socketContextAsync.Send(packet); break; } } } }; new Thread(new ThreadStart(asyncThreadStart)).Start(); // TODO: Move some of this code directly inside ViewModelContext/Controller classes while (true) { await TaskEx.WhenAny(TaskEx.Delay(250), entitiesChangePacketEvent.WaitAsync()); EntitiesChangePacket packet; while (entitiesChangePackets.TryDequeue(out packet)) { ViewModelController.NetworkApplyChanges(contexts[packet.GroupKey].Item1, packet.Changes); lastAckPacket = packet.Index; } // Wait a single frame so that network updates get applied properly by all rendering systems for next update await Scheduler.Current.NextFrame(); // If entity disappeared, try to replace it with new one (happen during file reload) // It's little bit cumbersome to test, need some simplification of this specific entity view model root. if (selectedEntitiesContext.Root != null && selectedEntitiesContext.Root.Parent != null && selectedEntitiesContext.Root.Parent.NodeValue is Entity) { var entity = (Entity)selectedEntitiesContext.Root.Parent.NodeValue; if (!engineContext.EntityManager.Entities.Contains(entity)) { entity = engineContext.EntityManager.Entities.FirstOrDefault(x => x.Guid == entity.Guid); if (entity != null) { selectedEntitiesContext.ViewModelByGuid.Clear(); selectedEntitiesContext.Root = selectedEntitiesContext.GetModelView(entity).Children.First(x => x.PropertyName == "Components"); } } } var data = new Dictionary <string, byte[]>(); foreach (var context in contexts) { lock (context.Value.Item1) { if (context.Value.Item1.Root != null) { context.Value.Item1.AddModelView(context.Value.Item1.Root); } ViewModelController.UpdateReferences(context.Value.Item1, true); data[context.Key] = ViewModelController.NetworkSerialize(context.Value.Item1, context.Value.Item2); } } viewModelGlobalContext.UpdateObjects(contexts.Select(x => x.Value.Item1)); //Console.WriteLine("DataSize: {0}", data.Sum(x => x.Value.Length)); await Task.Factory.StartNew(() => socketContext.Send(new EntitiesUpdatePacket { AckIndex = lastAckPacket, Data = data })); } }
public static bool GetIsNetworkAvailable() { return(NetworkChange._moonlight_cbinding_moon_network_service_get_is_network_available(NetworkChange._moonlight_cbinding_runtime_get_network_service())); }
public static bool GetIsNetworkAvailable() { return(NetworkChange.moon_network_service_get_is_network_available(NetworkChange.runtime_get_network_service())); }
public async static Task ProcessClient(EngineContext engineContext, SocketContext socketContext, SocketContext socketContextAsync) { socketContext.AddPacketHandler<DownloadFileQuery>( async (packet) => { var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Open, VirtualFileAccess.Read); var data = new byte[stream.Length]; await stream.ReadAsync(data, 0, data.Length); stream.Close(); socketContext.Send(new DownloadFileAnswer { StreamId = packet.StreamId, Data = data }); }); socketContext.AddPacketHandler<UploadFilePacket>( async (packet) => { var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Create, VirtualFileAccess.Write); await stream.WriteAsync(packet.Data, 0, packet.Data.Length); stream.Close(); }); var viewModelGlobalContext = new ViewModelGlobalContext(); selectedEntitiesContext = new ViewModelContext(viewModelGlobalContext); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new EntityComponentEnumerator(engineContext)); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new RenderPassPluginEnumerator()); selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); //selectedEntitiesContext.ChildrenPropertyEnumerators.Add(new EffectPropertyEnumerator(engineContext)); var renderPassHierarchyContext = new ViewModelContext(viewModelGlobalContext); renderPassHierarchyContext.ChildrenPropertyEnumerators.Add(new RenderPassHierarchyEnumerator()); renderPassHierarchyContext.Root = new ViewModelNode("Root", engineContext.RenderContext.RootRenderPass).GenerateChildren(renderPassHierarchyContext); var renderPassPluginsContext = new ViewModelContext(viewModelGlobalContext); renderPassPluginsContext.ChildrenPropertyEnumerators.Add(new RenderPassPluginsEnumerator { SelectedRenderPassPluginContext = selectedEntitiesContext }); renderPassPluginsContext.Root = new ViewModelNode("Root", new EnumerableViewModelContent<ViewModelReference>( () => engineContext.RenderContext.RenderPassPlugins.Select(x => new ViewModelReference(x, true)))); var entityHierarchyEnumerator = new EntityHierarchyEnumerator(engineContext.EntityManager, selectedEntitiesContext); var entityHierarchyContext = new ViewModelContext(viewModelGlobalContext); entityHierarchyContext.ChildrenPropertyEnumerators.Add(entityHierarchyEnumerator); entityHierarchyContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); entityHierarchyContext.Root = new ViewModelNode("EntityHierarchyRoot", new EnumerableViewModelContent<ViewModelReference>( () => engineContext.EntityManager.Entities .Where(x => { var transformationComponent = x.Transformation; return (transformationComponent == null || transformationComponent.Parent == null); }) .Select(x => new ViewModelReference(x, true)))); entityHierarchyEnumerator.SelectedEntities.CollectionChanged += (sender, args) => { SelectEntity(entityHierarchyEnumerator.SelectedEntities); }; //entityHierarchyContext.Root.Children.Add(new ViewModelNode("SelectedItems", EnumerableViewModelContent.FromUnaryLambda<ViewModelReference, ViewModelReference>(new NullViewModelContent(), // (x) => { return new[] { new ViewModelReference(pickingSystem.SelectedEntity) }; }))); /*(value) => { var entityModelView = value != null ? entityHierarchyContext.GetModelView(value.Guid) : null; var entity = entityModelView != null ? (Entity)entityModelView.NodeValue : null; SelectEntity(entity); })));*/ entityHierarchyContext.Root.Children.Add(new ViewModelNode("DropEntity", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) => { var dropParameters = (DropCommandParameters)parameter; var movedItem = dropParameters.Data is Guid ? entityHierarchyContext.GetModelView((Guid)dropParameters.Data) : null; var newParent = dropParameters.Parent is Guid ? entityHierarchyContext.GetModelView((Guid)dropParameters.Parent) : null; if (newParent == null || movedItem == null) return; var parent = ((Entity)newParent.NodeValue).Transformation; if (dropParameters.TargetIndex > parent.Children.Count) return; var transformationComponent = ((Entity)movedItem.NodeValue).Transformation; transformationComponent.Parent = null; parent.Children.Insert(dropParameters.TargetIndex, transformationComponent); })))); entityHierarchyContext.Root.Children.Add(new ViewModelNode("DropAsset", new RootViewModelContent((ExecuteCommand)(async (viewModel2, parameter) => { var dropParameters = (DropCommandParameters)parameter; var assetUrl = (string)dropParameters.Data; /*var newParent = entityHierarchyContext.GetModelView((Guid)dropParameters.Parent); if (newParent == null || assetUrl == null) return; var parent = ((Entity)newParent.NodeValue).Transformation; if (dropParameters.ItemIndex > parent.Children.Count) return;*/ engineContext.Scheduler.Add(async () => { // Load prefab entity var loadedEntityPrefab = await engineContext.AssetManager.LoadAsync<Entity>(assetUrl + "#"); // Build another entity from prefab var loadedEntity = Prefab.Inherit(loadedEntityPrefab); // Add it to scene engineContext.EntityManager.AddEntity(loadedEntity); if (loadedEntity.ContainsKey(AnimationComponent.Key)) { Scheduler.Current.Add(() => AnimScript.AnimateFBXModel(engineContext, loadedEntity)); } }); })))); var scriptEngineContext = new ViewModelContext(viewModelGlobalContext); scriptEngineContext.ChildrenPropertyEnumerators.Add(new ScriptAssemblyEnumerator(engineContext)); scriptEngineContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); scriptEngineContext.Root = new ViewModelNode(new EnumerableViewModelContent<ViewModelReference>( () => engineContext.ScriptManager.ScriptAssemblies.Select(x => new ViewModelReference(x, true)))); scriptEngineContext.Root.Children.Add(new ViewModelNode("RunScript", new RootViewModelContent((ExecuteCommand)(async (viewModel2, parameter) => { var scriptName = (string)parameter; var matchingScript = engineContext.ScriptManager.Scripts.Where(x => x.TypeName + "." + x.MethodName == scriptName); if (matchingScript.Any()) { var scriptEntry = matchingScript.Single(); var microThread = engineContext.ScriptManager.RunScript(scriptEntry, null); } })))); var runningScriptsContext = new ViewModelContext(viewModelGlobalContext); runningScriptsContext.ChildrenPropertyEnumerators.Add(new MicroThreadEnumerator(selectedEntitiesContext)); runningScriptsContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); runningScriptsContext.Root = new ViewModelNode("MicroThreads", new EnumerableViewModelContent<ViewModelReference>( () => engineContext.Scheduler.MicroThreads.Select(x => new ViewModelReference(x, true)) )); var effectsContext = new ViewModelContext(viewModelGlobalContext); effectsContext.ChildrenPropertyEnumerators.Add(new EffectEnumerator(selectedEntitiesContext)); effectsContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); effectsContext.Root = new ViewModelNode("Effects", new EnumerableViewModelContent<ViewModelReference>( () => engineContext.RenderContext.Effects.Select(x => new ViewModelReference(x, true)) )); //effectsContext.Root.Children.Add(new ViewModelNode("PluginDefinitions", new RootViewModelContent())); var assetBrowserContext = new ViewModelContext(viewModelGlobalContext); assetBrowserContext.ChildrenPropertyEnumerators.Add(new AssetBrowserEnumerator(engineContext)); assetBrowserContext.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator()); assetBrowserContext.Root = new ViewModelNode("Root", "Root").GenerateChildren(assetBrowserContext); var editorContext = new ViewModelContext(viewModelGlobalContext); editorContext.Root = new ViewModelNode("Root"); editorContext.Root.Children.Add(new ViewModelNode("SwitchSelectionMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.None; }))); editorContext.Root.Children.Add(new ViewModelNode("SwitchTranslationMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.Translation; }))); editorContext.Root.Children.Add(new ViewModelNode("SwitchRotationMode", new CommandViewModelContent((sender, parameters) => { pickingSystem.ActiveGizmoActionMode = PickingSystem.GizmoAction.Rotation; }))); var contexts = new Dictionary<string, Tuple<ViewModelContext, ViewModelState>>(); contexts.Add("Editor", Tuple.Create(editorContext, new ViewModelState())); contexts.Add("RenderPassPlugins", Tuple.Create(renderPassPluginsContext, new ViewModelState())); contexts.Add("RenderPasses", Tuple.Create(renderPassHierarchyContext, new ViewModelState())); contexts.Add("SelectedEntities", Tuple.Create(selectedEntitiesContext, new ViewModelState())); contexts.Add("EntityHierarchy", Tuple.Create(entityHierarchyContext, new ViewModelState())); contexts.Add("ScriptEngine", Tuple.Create(scriptEngineContext, new ViewModelState())); contexts.Add("MicroThreads", Tuple.Create(runningScriptsContext, new ViewModelState())); contexts.Add("AssetBrowser", Tuple.Create(assetBrowserContext, new ViewModelState())); contexts.Add("Effects", Tuple.Create(effectsContext, new ViewModelState())); int lastAckPacket = 0; var entitiesChangePackets = new ConcurrentQueue<EntitiesChangePacket>(); socketContext.AddPacketHandler<EntitiesChangePacket>( (packet) => { entitiesChangePackets.Enqueue(packet); entitiesChangePacketEvent.Set(); }); Action asyncThreadStart = () => { while (true) { Thread.Sleep(100); foreach (var context in contexts) { // Process async data Guid[] path = null; object value = null; lock (context.Value.Item1) { var pendingNode = context.Value.Item1.GetNextPendingAsyncNode(); if (pendingNode != null) { value = pendingNode.Value; path = ViewModelController.BuildPath(pendingNode); } } if (path != null) { // Temporary encoding through our serializer (until our serializer are used for packets) var memoryStream = new MemoryStream(); var writer = new BinarySerializationWriter(memoryStream); writer.SerializeExtended(null, value, ArchiveMode.Serialize); var change = new NetworkChange { Path = path.ToArray(), Type = NetworkChangeType.ValueUpdateAsync, Value = memoryStream.ToArray() }; var packet = new EntitiesChangePacket { GroupKey = context.Key, Changes = new NetworkChange[] { change } }; socketContextAsync.Send(packet); break; } } } }; new Thread(new ThreadStart(asyncThreadStart)).Start(); // TODO: Move some of this code directly inside ViewModelContext/Controller classes while (true) { await TaskEx.WhenAny(TaskEx.Delay(250), entitiesChangePacketEvent.WaitAsync()); EntitiesChangePacket packet; while (entitiesChangePackets.TryDequeue(out packet)) { ViewModelController.NetworkApplyChanges(contexts[packet.GroupKey].Item1, packet.Changes); lastAckPacket = packet.Index; } // Wait a single frame so that network updates get applied properly by all rendering systems for next update await Scheduler.Current.NextFrame(); // If entity disappeared, try to replace it with new one (happen during file reload) // It's little bit cumbersome to test, need some simplification of this specific entity view model root. if (selectedEntitiesContext.Root != null && selectedEntitiesContext.Root.Parent != null && selectedEntitiesContext.Root.Parent.NodeValue is Entity) { var entity = (Entity)selectedEntitiesContext.Root.Parent.NodeValue; if (!engineContext.EntityManager.Entities.Contains(entity)) { entity = engineContext.EntityManager.Entities.FirstOrDefault(x => x.Guid == entity.Guid); if (entity != null) { selectedEntitiesContext.ViewModelByGuid.Clear(); selectedEntitiesContext.Root = selectedEntitiesContext.GetModelView(entity).Children.First(x => x.PropertyName == "Components"); } } } var data = new Dictionary<string, byte[]>(); foreach (var context in contexts) { lock (context.Value.Item1) { if (context.Value.Item1.Root != null) context.Value.Item1.AddModelView(context.Value.Item1.Root); ViewModelController.UpdateReferences(context.Value.Item1, true); data[context.Key] = ViewModelController.NetworkSerialize(context.Value.Item1, context.Value.Item2); } } viewModelGlobalContext.UpdateObjects(contexts.Select(x => x.Value.Item1)); //Console.WriteLine("DataSize: {0}", data.Sum(x => x.Value.Length)); await Task.Factory.StartNew(() => socketContext.Send(new EntitiesUpdatePacket { AckIndex = lastAckPacket, Data = data })); } }
/// <summary> Using the given <paramref name="trainingSet"/>, trains the neural network with the given <paramref name="batchSize"/> over the given <paramref name="epochs"/>. </summary> /// <param name="random"> The random number generator used to shuffle the <paramref name="trainingSet"/>. </param> /// <param name="trainingSet"> The <see cref="IDataSet"/> against which to train. </param> /// <param name="epochs"> The number of times to go over the <paramref name="trainingSet"/>. </param> /// <param name="batchSize"> The number of <see cref="IDataPoint"/>s to process before applying learning. </param> /// <param name="logger"> <see cref="INetworkLogger"/> used to log the progress of the training. If this is <c>null</c>, no logging will happen. </param> /// <param name="testSet"> Another <see cref="IDataSet"/>, with <see cref="IDataPoint"/>s that are not in the <paramref name="trainingSet"/>. If this is given, the network will be tested against the <paramref name="testSet"/> after every epoch. </param> public void LearnOverData(Random random, IDataSet trainingSet, int epochs, int batchSize, INetworkLogger logger = null, IDataSet testSet = null) { // Create the various required arrays once, then just fill them with data. This cuts down on memory allocations, dramatically speeding up the training. float[] input = new float[inputLayer.Count]; float[] output = new float[outputLayer.Count]; float[] desiredOutput = new float[outputLayer.Count]; float[] error = new float[outputLayer.Count]; // Calculate how many batches are required with the given batch size. int numberOfBatches = trainingSet.Count / batchSize; // Create the change to be made to the network. NetworkChange networkChange = new NetworkChange(this); // Do the given amount of epochs. for (int currentEpoch = 0; currentEpoch < epochs; currentEpoch++) { // For each epoch, shuffle the training data randomly. trainingSet.Shuffle(random); // The percentage of data correctly guessed per batch. float lastBatchPercentage = 0.0f; // Do the given amount of batches. for (int batch = 0; batch < numberOfBatches; batch++) { // How many data were correctly guessed in this batch. int batchGuesses = 0; // Do the calculated number of training data in the batch. for (int dataIndex = 0; dataIndex < batchSize; dataIndex++) { // Get the next data and set the input array to its data. IDataPoint currentData = trainingSet[batch * batchSize + dataIndex]; currentData.GetFloatData(ref input); // Process the data through the network, saving its output. ProcessInput(input, ref output); // Get the output that the network guessed with the most confidence. int bestGuess = output.GetIndexOfHighestValue(); // Create the desired output. desiredOutput.ApplyVectorisedFunction((int g) => g == currentData.Label ? 1 : 0); // If the guess was correct, track it. if (bestGuess == currentData.Label) { batchGuesses++; } // Calculate the error. CalculateError(desiredOutput, output, ref error); // Learn from the output compared to the desired output, save the results in the previous network change. CalculateChanges(networkChange, error); // If a logger has been given, log the state of the network as of this iteration. logger?.Log(currentEpoch, epochs, batch, numberOfBatches, dataIndex, batchSize, lastBatchPercentage); } // Calculate how many of the data in the batch were correctly guessed. lastBatchPercentage = (float)batchGuesses / batchSize; // Apply the changes. ApplyChanges(networkChange, batchSize); networkChange.Reset(); } // If a logger was given and should log output, log the output of the epoch. if (logger != null && (logger.OutputOptions & OutputOptions.WhenFinished) == OutputOptions.WhenFinished) { // Log that the epoch finished. logger.Log($"\nEpoch {currentEpoch} finished"); // If test data was given, test against it and log the output. if (trainingSet != null) { logger.Log($", correctly guessed {PercentageOfCorrectTestData(random, testSet, ref input, ref output):P2} of the test data"); } // Log the full stop and line end. logger.Log(".\n"); } } // If a logger was given, log that the training finished. logger?.Log("Training finished."); }