public static NetModel GetNetModel(string NetID)
        {
            List <object> FetchedNet    = Querys.SelectQuery("*", "NETS", "NetID = " + NetID).Result[0];
            NetModel      FetchedNetOrg = new NetModel(Convert.ToString(FetchedNet[0]), Convert.ToInt64(FetchedNet[1]), Convert.ToInt64(FetchedNet[2]));

            return(FetchedNetOrg);
        }
示例#2
0
 /// <summary>
 /// 序列化数据.
 /// </summary>
 /// <param name="mod">数据对象.</param>
 private byte[] Serialize(NetModel mod)
 {
     try
     {
         //内存流实例
         using (MemoryStream ms = new MemoryStream())
         {
             //ProtoBuf协议序列化数据对象
             ProtoBuf.Serializer.Serialize <NetModel>(ms, mod);
             //创建临时结果数组
             byte[] result = new byte[ms.Length];
             //调整游标位置为0
             ms.Position = 0;
             //开始读取,从0到尾
             ms.Read(result, 0, result.Length);
             //返回结果
             return(result);
         }
     }
     catch (Exception ex)
     {
         MyGameServer.LogInfo("Error: " + ex.ToString());
         return(null);
     }
 }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("NetClient");
            Socket     socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.3.3"), 6555);

            socket.Connect(endPoint);

            //while (true)
            //{
            //    string message = Console.ReadLine();
            //    if (message == "c")
            //    {
            //        socket.Close();
            //        break;
            //    }
            //    NetModel netModel = new NetModel();
            //    netModel.Message = message;
            //    byte[] data = Serialize<NetModel>(netModel);
            //    byte[] newData = NetMessage.GetBytes(data);
            //    socket.Send(newData);
            //}

            for (int i = 0; i < 1000; i++)
            {
                NetModel netModel = new NetModel();
                netModel.Message = "客户端发送消息给服务器:" + i.ToString();
                byte[] data    = Serialize <NetModel>(netModel);
                byte[] newData = NetMessage.GetBytes(data);
                socket.Send(newData);
            }
            Console.ReadKey();
        }
示例#4
0
    /// <summary>
    /// 异步接收消息回调
    /// </summary>
    /// <param name="ar">Ar.</param>
    void AsyncReceive(System.IAsyncResult ar)
    {
        //获取消息套接字
        Socket workingClient = ar.AsyncState as Socket;
        //完成接收
        int msgLength = workingClient.EndReceive(ar);

        //如果接收到了数据
        if (msgLength > 0)
        {
            //消息接收提示
            ReceiveCallback("ReceiveData : " + msgLength + "bytes");
            //临时缓冲区
            byte[] tempBuffer = new byte[msgLength];
            //拷贝数据到临时缓冲区
            Buffer.BlockCopy(buffer, 0, tempBuffer, 0, msgLength);
            //数据放到缓存数据组队尾
            cache.AddRange(tempBuffer);
            //拆包解析
            byte[] result = LengthDecode(ref cache);
            //如果已经接收完全部数据
            if (result != null)
            {
                //开始反序列化数据
                NetModel resultModel = DeSerialize(result);
                //TODO:Object Processing!
                //数据对象结果提示
                ReceiveCallback("Object Result IP : " + resultModel.senderIp);
                ReceiveCallback("Object Result Content : " + resultModel.content);
                ReceiveCallback("Object Result Time : " + resultModel.time);
            }
            //消息未接收全,继续接收
            tcpClient.Client.BeginReceive(buffer, 0, 1024, SocketFlags.None, AsyncReceive, tcpClient.Client);
        }
    }
示例#5
0
        static void Main(string[] args)
        {
            var network = NetModel
                          .Load("model.json")
                          .NetChain
                          .Compile <Vector, Vector>();

            Console.WriteLine(network.Process(new double[] { 1, 1 }));

            var sw  = System.Diagnostics.Stopwatch.StartNew();
            var vec = new Vector(2);

            for (var i = 0; i < 10000000; i++)
            {
                var b1 = i & 1;
                var b2 = (i >> 1) & 1;
                vec[0] = b1;
                vec[1] = b2;

                var ans = network.Process(vec);
                //Console.WriteLine($"{b1} ^ {b2} = {Math.Round(ans[0], 2)}");
            }
            sw.Stop();
            Console.WriteLine($"Elapsed: {sw.Elapsed}");
        }
        public void ApplyMutation(ref NetModel net)
        {
            var(fromId, toId) = GetRandomNodeIds(net);

            if (HasConnection(net, fromId, toId) is { } id)
            {
                ToggleConnection(ref net, id);
                return;
            }

            // Finds innovation number based on connection locations: from, to
            var innovation = _innovationController.FetchWithInsertOnFail(new InnovationModel
            {
                BatchId    = net.BatchId,
                NodeFromId = fromId,
                NodeToId   = toId,
                Type       = InnovationType.Connection
            });

            // Creates new connections
            var rnd        = new Random();
            var connection = new ConnectionModel
            {
                FromId       = fromId,
                ToId         = toId,
                InnovationId = innovation.Id,
                NetworkId    = net.Id,
                Weight       = rnd.NextDouble() * 2 - 1 // Range of -1 to 1
            };

            // Saves and Adds new connection to network
            _connectionProcessor.Save(ref connection);
            net.Connections.Add(connection);
        }
示例#7
0
 public static NetModel GetInstance()
 {
     if (_instance == null)
     {
         _instance = new NetModel();
     }
     return(_instance);
 }
示例#8
0
        public double[] GetOutputValues(NetModel target)
        {
            var output = new double[Config.OutputLayerSize];
            var offset = Config.InputLayerSize;

            for (var i = 0; i < Config.OutputLayerSize; i++)
            {
                output[i] = target.Nodes[i + offset].Value;
            }

            return(output);
        }
示例#9
0
    private void Test()
    {
        NetModel netModel = new NetModel()
        {
            Message = "Unity"
        };

        byte[]   bytes = Serialize <NetModel>(netModel);
        NetModel temp  = DeSerialize <NetModel>(bytes);

        Debug.Log(temp.Message);
    }
        /// <summary>
        /// Finds two potential nodes for connections, so that - node from can not be output layers node
        /// and node to can not be input layers node
        /// </summary>
        /// <param name="net">Where to search nodes</param>
        /// <returns>Tuple containing from and to nodes innovation ids</returns>
        private static (int fromId, int toId) GetRandomNodeIds(NetModel net)
        {
            var rnd = new Random();

            // Output layer can't connect to other other nodes, thus:
            var from = rnd.Next(0, net.Nodes.Count - Config.OutputLayerSize);

            // Input layer can't be targeted by other nodes, thus:
            var to = rnd.Next(Config.InputLayerSize, net.Nodes.Count);

            return(net.Nodes[from].InnovationId, net.Nodes[to].InnovationId);
        }
示例#11
0
        public bool FeedForward(NetModel target, double[] input)
        {
            // In network model input layer and output layers are stored in sequence
            // Example for 3in and 1out: List<Nodes>[in, in, in, out, hid, hid, hid ..]

            #region Prepairing for feed forward

            var(inputCount, outputCount) = (Config.InputLayerSize, Config.OutputLayerSize);

            if (input.Length != inputCount)
            {
                throw new Exception($"{nameof(input)} should be {inputCount} long.");
            }

            for (var i = 0; i < inputCount; i++)
            {
                target.Nodes[i].Value = input[i];
            }

            #endregion


            #region FeedForward implementation

            // Because of the algorithm the network can be non-linear, meaning that
            // it's possible to create connection between nodes in layers that are not
            // next to one another, for example input layer can connect to output layer
            // even thou there are hidden layers in between.

            var currentLayersNodes = new HashSet <int>();

            // First run - input layer
            for (var i = 0; i < inputCount; i++)
            {
                currentLayersNodes.Add(target.Nodes[i].InnovationId);
            }

            var runCount = 0;
            do
            {
                runCount++;
                currentLayersNodes = UpdateLayerAndGetNext(ref target, currentLayersNodes, runCount == 1);

                if (runCount > target.Connections.Count + 1)
                {
                    return(false);
                }
            } while (currentLayersNodes.Count != 0);

            return(true);

            #endregion
        }
 private static int?HasConnection(NetModel net, int fromId, int toId)
 {
     // If connection exists in one way that means that you can not add the connection other way
     // because it will create an infinity loop.
     for (var i = 0; i < net.Connections.Count; i++)
     {
         var c = net.Connections[i];
         if ((c.FromId == fromId && c.ToId == toId) || (c.FromId == toId && c.ToId == fromId))
         {
             return(i);
         }
     }
     return(null);
 }
示例#13
0
    private void ParseData(int length)
    {
        startIndex += length;
        while (true)
        {
            if (startIndex <= 4)
            {
                return;
            }
            int count = System.BitConverter.ToInt32(buffer, 0);
            if ((startIndex - 4) >= count)
            { //解析出来一条新的数据
                //string s = System.Text.Encoding.UTF8.GetString(buffer, 4, count);
                byte[] ss = new byte[count];
                for (int i = 0; i < count; i++)
                {
                    ss[i] = buffer[i + 4];
                }
                NetModel netModel = DeSerialize <NetModel>(ss);
                UnityEngine.Debug.Log("接收到消息:" + netModel.Message);
                System.Buffer.BlockCopy(buffer, count + 4, buffer, 0, startIndex - count - 4);
                startIndex -= (count + 4);
            }
            else
            {
                break;
            }
        }

        //while (true) {
        //    if (startIndex <= 4) return;
        //    int count = System.BitConverter.ToInt32(buffer, 0);
        //    if ((startIndex - 4) >= count)
        //    { //解析出来一条新的数据
        //        //string s = System.Text.Encoding.UTF8.GetString(buffer, 4, count);
        //        byte[] ss = new byte[count];
        //        for (int i = 0; i < count; i++) {
        //            ss[i] = buffer[i+4];
        //        }
        //        byte[] s = DeSerialize<byte[]>(ss);
        //        UnityEngine.Debug.Log("接收到消息:" + System.Text.Encoding.UTF8.GetString(s));
        //        System.Buffer.BlockCopy(buffer, count + 4, buffer, 0, startIndex - count - 4);
        //        startIndex -= (count + 4);
        //    }
        //    else {
        //        break;
        //    }
        //}
    }
示例#14
0
    // Use this for initialization
    void Start()
    {
        //创建对象
        NetModel item = new NetModel()
        {
            ID = 1, Commit = "马三", Message = "Unity"
        };

        //序列化对象
        byte[] temp = Serialize(item);
        Debug.Log("序列化数组长度:" + temp.Length);
        //反序列化为对象
        NetModel result = DeSerialize(temp);

        Debug.Log(result.ID + "," + result.Commit + "," + result.Message);
    }
示例#15
0
    void Start()
    {
        NetModel item = new NetModel()
        {
            ID = 1, Commit = "Hello", Message = "Unity"
        };

        //序列化对象
        byte[] temp = Serialize(item);
        Debug.Log(temp.Length);

        //反序列化为对象
        NetModel result = DeSerialize(temp);

        Debug.Log(result.Message);
    }
        public void ApplyMutation(ref NetModel net)
        {
            if (net.Connections.Count == 0)
            {
                return;
            }

            // Shifts weight
            var rnd          = new Random();
            var connectionId = rnd.Next(0, net.Connections.Count);

            net.Connections[connectionId].Weight *= rnd.NextDouble();

            // Saves change
            _connectionProcessor.Update(net.Connections[connectionId]);
        }
示例#17
0
 /// <summary>
 /// 反序列化数据.
 /// </summary>
 /// <returns>数据对象.</returns>
 /// <param name="data">源数据.</param>
 private NetModel DeSerialize(byte[] data)
 {
     try {
         //内存流实例
         using (MemoryStream ms = new MemoryStream(data)) {
             //调整游标位置
             ms.Position = 0;
             //ProtoBuf协议反序列化数据
             NetModel mod = ProtoBuf.Serializer.Deserialize <NetModel> (ms);
             //返回数据对象
             return(mod);
         }
     } catch (Exception ex) {
         Debug.Log("Error: " + ex.ToString());
         return(null);
     }
 }
示例#18
0
    private NetModel DeSerialize(byte[] msg)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(msg, 0, msg.Length);
                ms.Position = 0;
                NetModel result = ProtoBuf.Serializer.Deserialize <NetModel>(ms);
                return(result);
            }
        }

        catch (Exception ex)
        {
            Debug.Log("反序列化失败: " + ex.ToString());
            return(null);
        }
    }
示例#19
0
 private byte[] Serialize(NetModel model)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             ProtoBuf.Serializer.Serialize <NetModel>(ms, model);
             byte[] result = new byte[ms.Length];
             ms.Position = 0;
             ms.Read(result, 0, result.Length);
             return(result);
         }
     }
     catch (Exception ex)
     {
         Debug.Log("序列化失败: " + ex.ToString());
         return(null);
     }
 }
示例#20
0
        private HashSet <int> UpdateLayerAndGetNext(ref NetModel target, HashSet <int> currentLayer, bool isInputLayer = false)
        {
            var nextLayer     = new HashSet <int>();
            var updatedValues = new Dictionary <int, double>();

            foreach (var connection in target.Connections.Where(connection => connection.Enabled))
            {
                // Builds next layer
                if (currentLayer.Contains(connection.FromId))
                {
                    nextLayer.Add(connection.ToId);
                }

                // If it's a input layer then we have no need to update node values
                if (isInputLayer)
                {
                    continue;
                }

                // If we have reached this node it means that all previous nodes have been set
                // thus we can find all connections that are marked towards current layers node
                // to calculate its new value
                if (currentLayer.Contains(connection.ToId))
                {
                    if (!updatedValues.ContainsKey(connection.ToId))
                    {
                        updatedValues.Add(connection.ToId, 0);
                    }

                    // Basics of NN the value of node is sum of previous layer nodes * weight of
                    // connection that connects them
                    updatedValues[connection.ToId] += connection.Weight * FindNode(target, connection.FromId).Value;
                }
            }

            // After we have calculated the sum of new value we can apply it through our activation function - Tanh.
            foreach (var(innovationId, value) in updatedValues)
            {
                FindNode(target, innovationId).Value = Math.Tanh(value);
            }

            return(nextLayer);
        }
示例#21
0
 /// <summary>
 /// 读取数据
 /// </summary>
 private void ReadData()
 {
     byte[] data = NetEncode.Decode(ref receiveCache);
     //说明数据保存成功
     if (data != null)
     {
         NetModel item = DeSerilizer(data);
         UnityEngine.Debug.Log(item.Message);
         if (receiveCallBack != null)
         {
             receiveCallBack(item);
         }
         //尾递归,继续读取数据
         ReadData();
     }
     else
     {
         isReceiving = false;
     }
 }
示例#22
0
    /// <summary>
    /// 读取数据
    /// </summary>
    private void ReadData()
    {
        byte[] data = NetEncode.Decode(ref receiveCache);

        //如果数据读取成功
        if (null != data)
        {
            NetModel item = NetSerilizer.DeSerialize(data);
            Debug.Log(item.ID + "," + item.Commit + "," + item.Message);
            if (null != receiveCallback)
            {
                receiveCallback(item);
            }
            //尾递归,继续处理数据
            ReadData();
        }
        else
        {
            isReceiving = false;
        }
    }
示例#23
0
 /// <summary>
 /// 将收到的消息反序列化成对象
 /// </summary>
 /// <returns>The serialize.</returns>
 /// <param name="msg">收到的消息.</param>
 private NetModel DeSerilizer(byte[] msg)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             //将消息写入流中
             ms.Write(msg, 0, msg.Length);
             //将流的位置归0
             ms.Position = 0;
             //使用工具反序列化对象
             NetModel result = ProtoBuf.Serializer.Deserialize <NetModel>(ms);
             return(result);
         }
     }
     catch (Exception ex)
     {
         UnityEngine.Debug.Log("反序列化失败: " + ex.ToString());
         return(null);
     }
 }
示例#24
0
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <param name="model">Model.</param>
 public void SendMsg(NetModel model)
 {
     //将数据对象序列化
     buffer = Serialize(model);
     //将序列化后的数据加字节头
     buffer = LengthEncode(buffer);
     //拆分数据,多次发送
     for (int i = 0; i < buffer.Length / 1024 + 1; i++)
     {
         //满发送,1KB
         int needSendBytes = 1024;
         //最后一次发送,剩余字节
         if (i == buffer.Length / 1024)
         {
             //计算剩余字节
             needSendBytes = buffer.Length - i * 1024;
         }
         //发送本次数据
         tcpClient.GetStream().Write(buffer, i * 1024, needSendBytes);
     }
 }
示例#25
0
        /// <summary>
        /// Finds networks innovation ranges min max
        /// </summary>
        /// <param name="net">Searchable network</param>
        /// <returns>tuple of minimum innovations id and maximum innovations id</returns>
        public (int min, int max) GetInnovationRange(NetModel net)
        {
            (int min, int max)minMxTuple = (0, 0);

            // Connections and innovations are sorted on gnn population
            // and selection from DB
            minMxTuple.min = net.Connections[0].InnovationId;
            minMxTuple.max = net.Connections.Last().InnovationId;

            //// Connections / Innovations are not sorted, thus
            //// we need to search through them in O(n) complexity
            //foreach (var connection in net.Connections)
            //{
            //    if (connection.InnovationId < minMxTuple.min)
            //        minMxTuple.min = connection.InnovationId;
            //    else if (connection.InnovationId > minMxTuple.max)
            //        minMxTuple.max = connection.InnovationId;
            //}

            return(minMxTuple);
        }
示例#26
0
    public void click()
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


        socket.Connect("192.168.4.95", 8080);
        print(11111111);

        NetModel item = new NetModel();

        item.ID      = 3;
        item.Commit  = "444";
        item.Message = "hahahah";

        byte[] temp = Serialize(item);
        Debug.Log(temp.Length);

        NetModel result = DeSerialize(temp);

        Debug.Log(result.Message);
    }
示例#27
0
 /// <summary>
 /// 把收到的消息反序列化成对象
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static NetModel DeSerialize(byte[] msg)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             //将消息写入流中
             ms.Write(msg, 0, msg.Length);
             //将流的位置归零
             ms.Position = 0;
             //使用工具反序列化对象
             NetModel result = ProtoBuf.Serializer.Deserialize <NetModel>(ms);
             return(result);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
示例#28
0
    IEnumerator ServerStart()
    {
        //加载网页数据
        WWW www = new WWW(wwwAddress);

        yield return(www);

        //编码获取内容
        string content = UTF8Encoding.UTF8.GetString(www.bytes);

        //内容测试
        Debug.Log(content);
        //待发送对象
        NetModel nm = new NetModel();

        //消息体
        nm.senderIp = "127.0.0.1";
        nm.content  = content;
        nm.time     = DateTime.Now.ToString();
        //发送数据对象
        NetUtility.Instance.SendMsg(nm);
    }
示例#29
0
 /// <summary>
 /// 将消息序列化为二进制的方法
 /// </summary>
 /// <param name="model">要序列化的对象</param>
 private byte[] Serialize(NetModel model)
 {
     try
     {
         //涉及格式转换,需要用到流,将二进制序列化到流中
         using (MemoryStream ms = new MemoryStream())
         {
             //使用ProtoBuf工具的序列化方法
             ProtoBuf.Serializer.Serialize <NetModel>(ms, model);
             //定义二级制数组,保存序列化后的结果
             byte[] result = new byte[ms.Length];
             //将流的位置设为0,起始点
             ms.Position = 0;
             //将流中的内容读取到二进制数组中
             ms.Read(result, 0, result.Length);
             return(result);
         }
     }
     catch (Exception ex)
     {
         Debug.Log("序列化失败: " + ex.ToString());
         return(null);
     }
 }
示例#30
0
 /// <summary>
 /// 将消息序列化为二进制数组
 /// </summary>
 /// <param name="netModel"></param>
 /// <returns></returns>
 public static byte[] Serialize(NetModel netModel)
 {
     try
     {
         //将二进制序列化到流中
         using (MemoryStream ms = new MemoryStream())
         {
             //使用ProtoBuf工具序列化方法
             ProtoBuf.Serializer.Serialize <NetModel>(ms, netModel);
             //保存序列化后的结果
             byte[] result = new byte[ms.Length];
             //将流的位置设置为0,起始点
             ms.Position = 0;
             //将流中的内容读取到二进制数组中
             ms.Read(result, 0, result.Length);
             return(result);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw;
     }
 }