Exemplo n.º 1
0
 public void ReadMnetFile(string FileName)
 {
     nodes = new List<Node>();
     wires = new List<Wire>();
     string[] tstr = System.IO.File.ReadAllLines(FileName);
     for (int i = 0; i < tstr.Length; i++)
     {
         if (tstr[i].Split(':')[0] == "NODE")
         {
             Node N = new Node();
             N.ReadFromString(tstr[i]);
             nodes.Add(N);
         }
         if (tstr[i].Split(':')[0] == "WIRE")
         {
             Wire W = new Wire();
             W.ReadFromString(tstr[i]);
             wires.Add(W);
         }
     }
 }
Exemplo n.º 2
0
        private static void PlaceOptimal(Mnet MainNetwork, RouteUtils.Node[] mcNodes, out int PlaceLayer, out int BaseSize)
        {
            //Расчитать baseSize
            PlaceLayer = 60;
            int xStep = mcNodes.Select(t => t.SizeX).Max() + dolled;
            int yStep = mcNodes.Select(t => t.SizeY).Max() + dolled;

            BaseSize = (new int[] { Convert.ToInt32(Math.Sqrt(mcNodes.Length)) * xStep, Convert.ToInt32(Math.Sqrt(mcNodes.Length)) * yStep}).Max() + 60;

            //Разместить порты
            var ports = MainNetwork.nodes.Where(t => t.NodeType.Contains("Port"));
            int lastxcoord = 1;
            foreach (var port in ports)
            {
                port.placed = true;
                port.x = lastxcoord;
                port.y = 1;
                port.z = PlaceLayer;
                lastxcoord += mcNodes.FirstOrDefault(t => t.NodeName == port.NodeName).SizeX + dolled;
            }
            //Расчитать матрицу связоности
            int[,] connectionMatrix = new int[MainNetwork.nodes.Count, MainNetwork.nodes.Count];
            for (int i = 0; i < MainNetwork.nodes.Count; i++)
            {
                for (int j = 0; j < MainNetwork.nodes.Count; j++)
                {
                    connectionMatrix[i, j] = MainNetwork.wires.Where(t => t.SrcName == MainNetwork.nodes[i].NodeName && t.DistName == MainNetwork.nodes[j].NodeName ||
                                                                          t.SrcName == MainNetwork.nodes[j].NodeName && t.DistName == MainNetwork.nodes[i].NodeName).Count();
                }
            }

            //Разместить ноды в ячейки
            var unPlaced = MainNetwork.nodes.Where(t => !t.placed);
            while (unPlaced.Count() > 0)
            {
                //Поиск нода максимально связанного с установленными
                int[] localConnectionMatrix = new int[MainNetwork.nodes.Count];
                for (int i = 0; i < MainNetwork.nodes.Count; i++)
                {
                    localConnectionMatrix[i] += MainNetwork.wires.Where(t => !MainNetwork.nodes[i].placed &&
                                                                             (t.SrcName == MainNetwork.nodes[i].NodeName && MainNetwork.nodes.FirstOrDefault(n=>n.NodeName == t.DistName).placed)).Count();
                    localConnectionMatrix[i] += MainNetwork.wires.Where(t => !MainNetwork.nodes[i].placed &&
                                                                             (t.DistName == MainNetwork.nodes[i].NodeName && MainNetwork.nodes.FirstOrDefault(n => n.NodeName == t.SrcName).placed)).Count();
                }
                int maxConections = localConnectionMatrix.Max();
                Node nodeToPlace = new Node();
                for (int i = 0; i < MainNetwork.nodes.Count; i++)
                {
                    if (localConnectionMatrix[i] == maxConections)
                    {
                        nodeToPlace = MainNetwork.nodes[i];
                    }
                }
                if (maxConections == 0)
                {
                    nodeToPlace = unPlaced.FirstOrDefault();
                }
                //Поиск места для установки
                int[,] placeMatrix = new int[BaseSize,BaseSize];
                var connectionsa = MainNetwork.wires.Where(k => k.SrcName == nodeToPlace.NodeName).Select(s => MainNetwork.nodes.FirstOrDefault(q => s.DistName == q.NodeName)).Where(l => l.placed);
                var connectionsb = MainNetwork.wires.Where(k => k.DistName == nodeToPlace.NodeName).Select(s => MainNetwork.nodes.FirstOrDefault(q => s.SrcName == q.NodeName)).Where(l => l.placed);
                var connections = connectionsa.Union(connectionsb);
                var mcNode = mcNodes.FirstOrDefault(k => k.NodeName == nodeToPlace.NodeName);
                int sizex = mcNode.SizeX + dolled;
                int sizey = mcNode.SizeY + dolled;
                var placedNodes = MainNetwork.nodes.Where(t=>t.placed);

                int step = 5;

                char[,] mask = new char[BaseSize, BaseSize];
                foreach (var node in placedNodes)
                {
                    DrawAtMask(mask, node.x, node.y, node.mcNode.SizeX, node.mcNode.SizeY);
                }

                for (int i = 0; i < BaseSize; i+=step)
                {
                    for (int j = 0; j < BaseSize; j+=step)
                    {
                        //bool collision = placedNodes.Where(t => CheckCollision(t, t.mcNode, i, j, mcNode)).Count() > 0;
                        if (BaseSize > i + sizex + 1 && BaseSize > j + sizey + 1)
                            if(!CheckMaskCollision(mask, nodeToPlace,i,j))
                                {
                                    foreach (Node n in connections)
                                    {
                                        placeMatrix[i, j] += (n.x - i) * (n.x - i) + (n.y - j) * (n.y - j);
                                    }
                                }
                    }
                }
                //Установка
                int pcondMin = 90000000;
                int xplace = 0;
                int yplace = 0;
                for (int i = 1; i < BaseSize; i++)
                {
                    for (int j = 1; j < BaseSize; j++)
                    {
                        if (placeMatrix[i, j] < pcondMin && placeMatrix[i, j]>0)
                        {
                            xplace = i;
                            yplace = j;
                            pcondMin = placeMatrix[i, j];
                        }
                    }
                }
                nodeToPlace.x = xplace;
                nodeToPlace.y = yplace;
                nodeToPlace.z = PlaceLayer;
                nodeToPlace.placed = true;
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("{0} - Осталось:{1}", nodeToPlace.NodeName, unPlaced.Count());
                Console.ForegroundColor = ConsoleColor.White;
             }
            //foreach(Node node in )

            BaseSize += PlaceLayer;
            //Debug Draw Image
            System.Drawing.Image im = new System.Drawing.Bitmap(BaseSize, BaseSize);
            System.Drawing.Graphics Gr = System.Drawing.Graphics.FromImage(im);
            Gr.Clear(System.Drawing.Color.Black);
            foreach(var node in MainNetwork.nodes)
            {
                Gr.DrawRectangle(System.Drawing.Pens.Green, node.x, node.y, node.mcNode.SizeX, node.mcNode.SizeY);
            }
            im.Save("1.png");

            //Увеличить плотность
            //throw new NotImplementedException();
        }
Exemplo n.º 3
0
 private static List<Wire> FindAllWiresTo(List<Wire> Wlist, Node node)
 {
     List<Wire> WO = new List<Wire>();
     for (int i = 0; i < Wlist.Count; i++)
     {
         if (Wlist[i].DistName == node.NodeName)
             WO.Add(Wlist[i]);
         if (Wlist[i].SrcName == node.NodeName)
             WO.Add(Wlist[i]);
     }
     return WO;
 }
Exemplo n.º 4
0
 private static bool CheckMaskCollision(char[,] mask, Node node,int x,int y)
 {
     for (int i = 0; i < node.mcNode.SizeX; i++)
     {
         for (int j = 0; j < node.mcNode.SizeY; j++)
         {
             if (mask[x + i, y + j] == 'X') return true;
         }
     }
     return false;
 }
Exemplo n.º 5
0
        private static bool CheckCollision(Node t, RouteUtils.Node node1, int i, int j, RouteUtils.Node node2)
        {
            bool result = false;
            result = result || CheckCollisionBoxAndPoint(t.x, t.y, node1.SizeX + dolled, node1.SizeY + dolled, i, j);
            result = result || CheckCollisionBoxAndPoint(t.x, t.y, node1.SizeX + dolled, node1.SizeY + dolled, i + node2.SizeX, j);
            result = result || CheckCollisionBoxAndPoint(t.x, t.y, node1.SizeX + dolled, node1.SizeY + dolled, i, j + node2.SizeY);
            result = result || CheckCollisionBoxAndPoint(t.x, t.y, node1.SizeX + dolled, node1.SizeY + dolled, i + node2.SizeX, j + node2.SizeY);

            result = result || CheckCollisionBoxAndPoint(i, j, node2.SizeX + dolled, node2.SizeY + dolled, t.x, t.y);
            result = result || CheckCollisionBoxAndPoint(i, j, node2.SizeX + dolled, node2.SizeY + dolled, t.x + node1.SizeX, t.y);
            result = result || CheckCollisionBoxAndPoint(i, j, node2.SizeX + dolled, node2.SizeY + dolled, t.x, t.y + node1.SizeY);
            result = result || CheckCollisionBoxAndPoint(i, j, node2.SizeX + dolled, node2.SizeY + dolled, t.x + node1.SizeX, t.y + node1.SizeY);

            return result;
        }
Exemplo n.º 6
0
        private static int CalcTypeWeight(Node node)
        {
            if (node.NodeType.StartsWith("DUP")) return 1;

            switch (node.NodeType)
            {
                case "TRIG_D":
                    return 5;
                case "AND":
                    return 3;
                case "NOT":
                    return 2;
                case "OR":
                    return 4;
                default:
                    return 0;
            }
        }
Exemplo n.º 7
0
 private static void SortOptimize(Mnet MainNetwork)
 {
     for (int i = 0; i < MainNetwork.nodes.Count * OptimiseDeep; i++)
     {
         for (int j = 2; j < (MainNetwork.nodes.Count - 1); j++)
         {
             if (MainNetwork.nodes[j].NodeType != "INPort" && MainNetwork.nodes[j].NodeType != "OUTPort")
             {
                 List<Wire> Wlist1 = FindAllWiresTo(MainNetwork.wires, MainNetwork.nodes[j]);
                 //List<Wire> Wlist2 = FindAllWiresTo(MainNetwork.wires, MainNetwork.nodes[j+1]);
                 int ka = 0;
                 if (TypeSort)
                 {
                     int wa = CalcTypeWeight(MainNetwork.nodes[j]);
                     int wb = CalcTypeWeight(MainNetwork.nodes[j-1]);
                     if (wa > wb)
                     {
                         ka = 1;
                     }
                     else
                     {
                         ka = 0;
                     }
                 }
                 else
                 {
                     ka = CalcWireLens(Wlist1, MainNetwork);
                 }
                 //int kb = CalcWireLens(Wlist2, MainNetwork);
                 if (ka < 0)
                 {
                     if (MainNetwork.nodes[j + 1].NodeType != "INPort" && MainNetwork.nodes[j + 1].NodeType != "OUTPort")
                     {
                         Node N = new Node();
                         N = MainNetwork.nodes[j];
                         MainNetwork.nodes[j] = MainNetwork.nodes[j + 1];
                         MainNetwork.nodes[j + 1] = N;
                     }
                 }
                 if (ka > 0)
                 {
                     if (MainNetwork.nodes[j - 1].NodeType != "INPort" && MainNetwork.nodes[j - 1].NodeType != "OUTPort")
                     {
                         Node N = new Node();
                         N = MainNetwork.nodes[j];
                         MainNetwork.nodes[j] = MainNetwork.nodes[j - 1];
                         MainNetwork.nodes[j - 1] = N;
                     }
                 }
             }
         }
     }
 }