Esempio n. 1
0
        //добавление дуг в графе
        static void addCurves(List <Vertex> list, Duga[,]  curves)
        {
            Muravey muravey = new Muravey(0);

            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (
                        (list[i].Type == MACHINE.VIRTUAL && list[j].Type == MACHINE.VIRTUAL && list[i].Branch == BRANCH.CALCULATIVE && list[j].Branch == BRANCH.CALCULATIVE) ||
                        (list[i].Type == MACHINE.VIRTUAL && list[j].Type == MACHINE.REAL && list[i].Branch == BRANCH.CALCULATIVE && list[j].Branch == BRANCH.CALCULATIVE) ||
                        (list[i].Type == MACHINE.VIRTUAL && list[j].Type == MACHINE.VIRTUAL && list[i].Branch == BRANCH.STORAGE && list[j].Branch == BRANCH.STORAGE) ||
                        (list[i].Type == MACHINE.VIRTUAL && list[j].Type == MACHINE.REAL && list[i].Branch == BRANCH.STORAGE && list[j].Branch == BRANCH.STORAGE))
                    {
                        curves[i, j] = new Duga(1, Evristic(i, j, muravey, list));
                        curves[j, i] = new Duga(1, Evristic(j, i, muravey, list));
                    }
                    if
                    (list[i].Type == MACHINE.ROOT && list[j].Type == MACHINE.VIRTUAL)
                    {
                        curves[i, j] = new Duga(1, 1);
                        curves[j, i] = new Duga(1, 1);
                    }
                }
            }
        }
Esempio n. 2
0
        //муравьиный алгорит
        public static void Algoritm(string file)
        {
            //список вершин (вирт. машин +физ. вычислительных узлов)
            List <Vertex> vertex = new List <Vertex>();

            vertex.Add(new Vertex(0, BRANCH.ROOT, MACHINE.ROOT, 0));
            //массив дуг
            Duga[,] dugi;
            theBestSolution solution;

            Muravey[] muravey = new Muravey[COUNT_MURAVEY];

            //заполнение списков vertex и  storage
            readInputfromFile(file, vertex);

            dugi     = new Duga[vertex.Count, vertex.Count];
            solution = new theBestSolution(vertex.Count);
            //добавление дуг в графе
            addCurves(vertex, dugi);

            int[] count = new int[4];
            //список вирт машин
            List <int> ListVirtual_vertex = new List <int>();

            for (int i = 0; i < vertex.Count; i++)
            {
                if (vertex[i].Type == MACHINE.VIRTUAL && vertex[i].Branch == BRANCH.CALCULATIVE)
                {
                    ListVirtual_vertex.Add(vertex[i].Num);
                    count[0]++;
                }
                if (vertex[i].Type == MACHINE.REAL && vertex[i].Branch == BRANCH.CALCULATIVE)
                {
                    count[1]++;
                }
                if (vertex[i].Type == MACHINE.VIRTUAL && vertex[i].Branch == BRANCH.STORAGE)
                {
                    count[2]++;
                }
                if (vertex[i].Type == MACHINE.REAL && vertex[i].Branch == BRANCH.STORAGE)
                {
                    count[3]++;
                }
            }
            //список вирт.элементов во всем графе
            List <int> ListVirtual_storage = new List <int>();

            for (int i = 0; i < vertex.Count; i++)
            {
                if (vertex[i].Type == MACHINE.VIRTUAL)
                {
                    ListVirtual_storage.Add(vertex[i].Num);
                }
            }

            //проход муравьями графа
            for (int m = 0; m < COUNT_MURAVEY; m++)
            {
                muravey[m] = new Muravey(0);
                //обход левой части графа(вирт.машины+физич.узлы)
                antAlhoritm(vertex, muravey[m], ListVirtual_vertex, dugi, BRANCH.CALCULATIVE);

                //Console.WriteLine();
                //обход правой части графа(storage-элементы +хранилища данных)
                antAlhoritm(vertex, muravey[m], ListVirtual_storage, dugi, BRANCH.STORAGE);

                //изменение кол-ва ферромона на дугах
                updateFerromon(vertex, muravey[m], dugi);



                // Console.WriteLine();

                //сортировка списка соответствий по ключу
                muravey[m].target = muravey[m].target.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
                solution.setTheBestWay(muravey[m]);
            }
            //оценка сходимости алгоритма
            solution.estimateWay(muravey);
            solution.theAdjacencyMatrixOfAGraph(vertex, dugi);
            //Deigstra(vertex, solution.AdjacencyMatrix);

            int[] rezult = new int[2];
            foreach (int i in solution.Target.Keys)
            {
                for (int k = 0; k < vertex.Count; k++)
                {
                    if (i == k)
                    {
                        if (vertex[i].Type == MACHINE.VIRTUAL && vertex[i].Branch == BRANCH.CALCULATIVE)
                        {
                            rezult[0]++;
                        }
                        if (vertex[i].Type == MACHINE.VIRTUAL && vertex[i].Branch == BRANCH.STORAGE)
                        {
                            rezult[1]++;
                        }
                    }
                }
            }

            Console.WriteLine(String.Format("|{0,15}|{1,7}|{2,7}|{3,11}|{4,10}|{5,8}|{6,14}|", file, count[0], count[1], rezult[0], count[2], count[3], rezult[1]));
            Console.WriteLine(String.Format("+---------------+-------+-------+-----------+----------+--------+--------------+"));
            Console.ReadKey();
        }