Exemplo n.º 1
0
        public static Network Load(string file)
        {
            NetworkSaveData data = null;

            try {
                data = JsonConvert.DeserializeObject <NetworkSaveData>(File.ReadAllText(file));
            } catch (Exception exception) {
                //TODO: Logging?
                throw exception;
            }
            Network network = new Network();

            network.ActivationFunctionType = (ActivationFunctionType)data.ActivationFunctionType;
            network.learningRate           = data.LearningRate;
            network.NeuronCurrentId        = data.NeuronCurrentId;
            network.ConnectionCurrentId    = data.ConnectionCurrentId;
            network.Inputs  = data.Inputs.Select(x => new Neuron(network, x)).ToList();
            network.Hidden  = data.Hidden.Select(x => x.Select(y => new Neuron(network, y)).ToList()).ToList();
            network.Outputs = data.Output.Select(x => new Neuron(network, x)).ToList();
            List <Neuron> all = network.Inputs.Select(x => x).ToList();

            foreach (List <Neuron> column in network.Hidden)
            {
                all.AddRange(column);
            }
            all.AddRange(network.Outputs);
            network.Connections = data.Connections.Select(x => new Connection(all.First(y => y.Id == x.InId), all.First(y => y.Id == x.OutId), x)).ToList();
            return(network);
        }
Exemplo n.º 2
0
        public static void Save(Network network, string file)
        {
            NetworkSaveData data = new NetworkSaveData()
            {
                ActivationFunctionType = (int)network.ActivationFunctionType,
                LearningRate           = network.learningRate,
                NeuronCurrentId        = network.NeuronCurrentId,
                ConnectionCurrentId    = network.ConnectionCurrentId,
                Inputs = network.Inputs.Select(x => new NeuronSaveData()
                {
                    Id = x.Id
                }).ToList(),
                Hidden = network.Hidden.Select(x => x.Select(y => new NeuronSaveData()
                {
                    Id = y.Id
                }).ToList()).ToList(),
                Output = network.Outputs.Select(x => new NeuronSaveData()
                {
                    Id = x.Id
                }).ToList(),
                Connections = network.Connections.Select(x => new ConnectionSaveData()
                {
                    Id = x.Id, InId = x.In.Id, OutId = x.Out.Id, Weight = x.Weight
                }).ToList()
            };

            try {
                File.WriteAllText(file, JsonConvert.SerializeObject(data, Formatting.None));
            } catch (Exception exception) {
                //TODO: Logging?
                throw exception;
            }
        }