Exemplo n.º 1
0
        private static void TestResidualNetwork()
        {
            //Create graph from Figure 6.10
            FlowNetwork fn = new FlowNetwork();
            Vertex      v1 = new Vertex();
            Vertex      v2 = new Vertex();
            Vertex      v3 = new Vertex();
            Vertex      v4 = new Vertex();

            fn.AddVertex(v1);
            fn.AddVertex(v2);
            fn.AddVertex(v3);
            fn.AddVertex(v4);

            fn.SetSource(v1);
            fn.SetSink(v4);

            fn.AddEdge(new FlowEdge(v1, v3, 3, 4));
            fn.AddEdge(new FlowEdge(v1, v2, 2, 2));
            fn.AddEdge(new FlowEdge(v2, v3, 2, 3));
            fn.AddEdge(new FlowEdge(v3, v4, 5, 5));
            fn.AddEdge(new FlowEdge(v2, v4, 0, 1));

            Graph g = GraphAlgorithms.CreateResidualNetwork(fn);
        }
Exemplo n.º 2
0
        private static void TestMaxFlow()
        {
            FlowNetwork fn = new FlowNetwork();

            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();

            fn.AddVertex(v1);
            fn.AddVertex(v2);
            fn.AddVertex(v3);
            fn.AddVertex(v4);
            fn.AddVertex(v5);
            fn.AddVertex(v6);

            fn.SetSource(v1);
            fn.SetSink(v6);

            fn.AddEdge(new FlowEdge(v1, v2, 0f, 2f));
            fn.AddEdge(new FlowEdge(v1, v3, 0f, 2f));
            fn.AddEdge(new FlowEdge(v2, v4, 0f, 2f));
            fn.AddEdge(new FlowEdge(v3, v4, 1f, 2f));
            fn.AddEdge(new FlowEdge(v3, v5, 0f, 2f));
            fn.AddEdge(new FlowEdge(v4, v6, 1f, 2f));
            fn.AddEdge(new FlowEdge(v5, v6, 0f, 2f));

            //float maxFlow = GraphAlgorithms.MaximumFlow(fn);

            //Console.WriteLine("Max flow is " + maxFlow);
        }
Exemplo n.º 3
0
        private FlowNetwork FromFn(FileInfo file)
        {
            FlowNetwork g = new FlowNetwork();
            int         source = -1, target = -1;

            try
            {
                System.IO.StreamReader fs =
                    new System.IO.StreamReader(file.FullName);
                string line;
                while ((line = fs.ReadLine()) != null)
                {
                    if (line.StartsWith("SOURCE"))
                    {
                        var s = line.Split(':');
                        source = int.Parse(s[1]) - 1;
                    }
                    if (line.StartsWith("TARGET"))
                    {
                        var s = line.Split(':');
                        target = int.Parse(s[1]) - 1;
                    }
                    if (char.IsLetter(line[0]))
                    {
                        continue;
                    }
                    var d  = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    int n0 = int.Parse(d[0]) - 1; // My numbering starts with 0, not 1
                    if (n0 == -2)
                    {
                        break;
                    }
                    int n1 = int.Parse(d[1]) - 1; // My numbering starts with 0, not 1
                    int w  = int.Parse(d[2]);     // weight
                    g.AddEdge(n0, n1, w);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex.Message, Log.ERROR);
            }

            g.Source = source;
            g.Target = target;
            var validation = g.Validate();

            if (validation.Count > 0)
            {
                foreach (var error in validation)
                {
                    Log.Write(error, Log.ERROR);
                }
                return(null);
            }
            return(g);
        }
Exemplo n.º 4
0
        private FlowNetwork FromDimac(FileInfo file)
        {
            FlowNetwork g   = new FlowNetwork();
            var         srs = -1;
            var         trg = -1;

            try
            {
                System.IO.StreamReader fs =
                    new System.IO.StreamReader(file.FullName);
                string line;
                while ((line = fs.ReadLine()) != null)
                {
                    if (line.EndsWith("s"))
                    {
                        var s = line.Split(' ');
                        srs = int.Parse(s[1]) - 1;
                    }
                    if (line.EndsWith("t"))
                    {
                        var t = line.Split(' ');
                        trg = int.Parse(t[1]) - 1;
                    }
                    if (line.StartsWith("a"))
                    {
                        var a    = line.Split(' ');
                        int from = int.Parse(a[1]) - 1;

                        int to     = int.Parse(a[2]) - 1;
                        int weight = int.Parse(a[3]);
                        g.AddEdge(from, to, weight);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex.Message, Log.ERROR);
            }
            g.Source = srs;
            g.Target = trg;
            var validation = g.Validate();

            if (validation.Count > 0)
            {
                foreach (var error in validation)
                {
                    Log.Write(error, Log.ERROR);
                }
                return(null);
            }
            return(g);
        }
Exemplo n.º 5
0
        private FlowNetwork FromCSV(FileInfo file)
        {
            FlowNetwork g = new FlowNetwork();
            int         source = -1, target = -1;

            using (TextFieldParser parser = new TextFieldParser(file.FullName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                int line = 0;
                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    line++;
                    if (line == 1)
                    {
                        //skip header
                        continue;
                    }

                    if (line == 2)
                    {
                        int.TryParse(fields[0], out source);
                        int.TryParse(fields[1], out target);
                        //parse source and target
                        continue;
                    }
                    //Process row

                    int    from, to;
                    double capacity;
                    int.TryParse(fields[0], out from);
                    int.TryParse(fields[1], out to);
                    double.TryParse(fields[2], out capacity);
                    g.AddEdge(from, to, capacity);
                }
            }
            g.Source = source;
            g.Target = target;
            var validation = g.Validate();

            if (validation.Count > 0)
            {
                foreach (var error in validation)
                {
                    Log.Write(error, Log.ERROR);
                }
                return(null);
            }
            return(g);
        }
Exemplo n.º 6
0
    void CriarNodos()
    {
        List <string> arquivoLido  = new List <string>(LerAqruivo().Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
        List <int>    numerosLidos = new List <int>();

        for (int i = 0; i < arquivoLido.Count; i++)
        {
            numerosLidos.Add(Int32.Parse(arquivoLido[i]));
        }



        for (int i = 0; i < numerosLidos.Count; i = i + 3)
        {
            //print(numerosLidos[i]);
            bool       novo;
            GameObject nodo;
            if (GameObject.Find("Nodo " + numerosLidos[i].ToString()) == null)
            {
                nodo = Instantiate(nodoPrefab, transform.position, Quaternion.identity, transform);
                novo = true;
            }
            else
            {
                nodo = GameObject.Find("Nodo " + numerosLidos[i].ToString());
                novo = false;
            }

            Nodo nodoScript = nodo.GetComponent <Nodo>();

            nodoScript.numero          = numerosLidos[i];
            nodoScript.gameObject.name = "Nodo " + numerosLidos[i].ToString();


            Conexao conexao = Instantiate(conexaoPrefab);
            conexao.transform.SetParent(nodo.transform);
            conexao.origem  = numerosLidos[i];
            conexao.destino = numerosLidos[i + 1];
            conexao.custo   = numerosLidos[i + 2];

            nodoScript.conexoes.Add(conexao);


            if (novo)
            {
                nodos.Add(nodoScript);
            }
        }

        print("numero de nodos na lista: " + nodos.Count);
        int numeroDeConexoes = 0;

        foreach (Nodo nodo in nodos)
        {
            foreach (Conexao conexao in nodo.conexoes)
            {
                numeroDeConexoes++;
            }
        }
        print(numeroDeConexoes);
        FlowNetwork flowNetwork = new FlowNetwork(numeroDeConexoes);

        foreach (Nodo nodo in nodos)
        {
            foreach (Conexao conexao in nodo.conexoes)
            {
                flowNetwork.AddEdge(new FlowEdge(nodo.numero, conexao.destino, conexao.custo));
            }
        }

        FordFulkerson fordFulkerson = new FordFulkerson(flowNetwork, 1, 99);

        print(fordFulkerson.Value);
    }