コード例 #1
0
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph)
        {
            StreamReader  nodetype  = new StreamReader(filepath + "_typeo.txt");
            StreamReader  threshold = new StreamReader(filepath + "_tu.txt");
            List <double> thresh    = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                thresh.Add(double.Parse(threshold.ReadLine()));
            }
            List <int> type = new List <int>();

            for (int i = 0; i < graph.numV; i++)
            {
                int flag = int.Parse(nodetype.ReadLine());
                if (flag == 0)
                {
                    type.Add(0);
                }
                else if (flag == 1)
                {
                    type.Add(2);
                }
                else
                {
                    type.Add(1);
                }
            }
            List <double> d = new List <double> {
                0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
            };
            List <double> cu = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                double t = thresh[i];
                double p = 0.0;
                if (type[i] == 1)
                {
                    p = System.Math.Pow(t, 0.5);
                }
                else if (type[i] == 2)
                {
                    p = t;
                }
                else
                {
                    p = 1 - System.Math.Pow(1 - t, 0.5);
                }
                foreach (double point in d)
                {
                    if (point < p)
                    {
                        continue;
                    }
                    cu.Add(point);
                    break;
                }
            }
            int mh = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 1.0; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 1.0)
            {
                Bipartite bg = new Bipartite(filepath, alpha, graph.numV);
                double    b  = 10.0;
                while (b <= 10.0)
                // Build a random hyper graph with mH random hyper edges.
                {
                    StreamReader initial = new StreamReader(filepath + "_ini100.txt");
                    List <int>   seed    = new List <int>();
                    for (int i = 0; i < 100; i++)
                    {
                        seed.Add(int.Parse(initial.ReadLine()));
                    }
                    DateTime          Hyper_start = DateTime.Now;
                    ICModel           icm         = new ICModel(alpha);
                    CoordinateDescent cd          = new CoordinateDescent(graph, bg, seed, 0.0, type, 20, alpha, mh);
                    double            bused       = 0.0;
                    while (bused <= b)
                    {
                        int           flag     = seed[0];
                        double        maxE     = 0.0;
                        List <int>    bestseed = new List <int>();
                        List <double> bestallo = new List <double>();
                        foreach (int u in seed)
                        {
                            List <int> nrlist = graph.newreach(new List <int> {
                                u
                            }, cd.x);
                            double b2cub1 = 5 * cu[u];
                            if (b2cub1 >= 0.5 && nrlist.Count >= 1)
                            {
                                Tuple <List <int>, List <double>, double> result1 = cd.bestone(nrlist, b2cub1);
                                if (result1.Item3 / cu[u] >= maxE / cu[flag])
                                {
                                    maxE     = result1.Item3;
                                    flag     = u;
                                    bestseed = new List <int>();
                                    foreach (int point in result1.Item1)
                                    {
                                        bestseed.Add(point);
                                    }
                                    bestallo = new List <double>();
                                    foreach (double resource in result1.Item2)
                                    {
                                        bestallo.Add(resource);
                                    }
                                }
                            }
                            if (b2cub1 >= 1.0 && nrlist.Count >= 2)
                            {
                                Tuple <List <int>, List <double>, double> result2 = cd.besttwo(nrlist, b2cub1);
                                if (result2.Item3 / cu[u] >= maxE / cu[flag])
                                {
                                    maxE     = result2.Item3;
                                    flag     = u;
                                    bestseed = new List <int>();
                                    foreach (int point in result2.Item1)
                                    {
                                        bestseed.Add(point);
                                    }
                                    bestallo = new List <double>();
                                    foreach (double resource in result2.Item2)
                                    {
                                        bestallo.Add(resource);
                                    }
                                }
                            }
                            if (b2cub1 >= 1.5 && nrlist.Count >= 3)
                            {
                                Tuple <List <int>, List <double>, double> result3 = cd.bestone(nrlist, b2cub1);
                                if (result3.Item3 / cu[u] >= maxE / cu[flag])
                                {
                                    maxE     = result3.Item3;
                                    flag     = u;
                                    bestseed = new List <int>();
                                    foreach (int point in result3.Item1)
                                    {
                                        bestseed.Add(point);
                                    }
                                    bestallo = new List <double>();
                                    foreach (double resource in result3.Item2)
                                    {
                                        bestallo.Add(resource);
                                    }
                                }
                            }
                        }
                        if (bused + cu[flag] > b)
                        {
                            break;
                        }
                        bused += cu[flag];
                        seed.Remove(flag);
                        List <int> maxnr = graph.newreach(new List <int> {
                            flag
                        }, cd.x);
                        for (int u = 0; u < bestseed.Count; u++)
                        {
                            if (bused + bestallo[u] > b)
                            {
                                break;
                            }
                            cd.ChangeAllocation(bestseed[u], bestallo[u]);
                            cd.x.Add(bestseed[u]);
                            bused += bestallo[u];
                        }
                        Console.WriteLine("newreach:" + Convert.ToString(cd.C.Sum()) + "total:" + Convert.ToString(bused));
                    }
                    DateTime Hyper_end  = DateTime.Now;
                    double   Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;

                    Hyper_start = DateTime.Now;
                    List <double> prob = new List <double>();
                    foreach (int u in cd.x)
                    {
                        prob.Add(cd.SeedProb(u, cd.C[u]));
                    }
                    Tuple <double, double> results = icm.InfluenceSpread(graph, cd.x, prob, 200);
                    Hyper_end = DateTime.Now;
                    FileStream   outfile = new FileStream(filepath + "_8o.txt", FileMode.Append);
                    StreamWriter writer  = new StreamWriter(outfile);
                    writer.Write("Choose time:" + Hyper_time + "\t");
                    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                    writer.Write("Propagation time:" + Hyper_time + "\t");
                    writer.Write("a:" + alpha + "\tb:" + b + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "\n");
                    writer.Flush();
                    writer.Close();
                    b += 10.0;
                }
                alpha += 0.2;
            }
        }
コード例 #2
0
        //public static string filepath = "D:/iiot/csharp/im/Wiki-Vote";
        public static void Main(string[] args)
        {
            Graph graph = new Graph(filepath + ".csv");
            int   mh    = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 0.6; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 0.6)
            {
                Bipartite bg    = new Bipartite(filepath, alpha, graph.numV);
                double    b     = 10.0;
                double    ratio = 4.0;
                while (b <= 50.0)
                // Build a random hyper graph with mH random hyper edges.
                {
                    StreamReader initial = new StreamReader(filepath + "_ini100.txt");
                    List <int>   seed    = new List <int>();
                    List <int>   final   = new List <int>();
                    for (int i = 0; i < 100; i++)
                    {
                        seed.Add(int.Parse(initial.ReadLine()));
                    }
                    DateTime          Hyper_start = DateTime.Now;
                    ICModel           icm         = new ICModel(alpha);
                    CoordinateDescent cd          = new CoordinateDescent(graph, bg, seed, 0.0, 10, alpha, mh);
                    double            bused       = 0.0;
                    while (bused <= b)
                    {
                        int        flag    = seed[0];
                        double     maxE    = 0.0;
                        List <int> seedset = new List <int>();
                        foreach (int u in seed)
                        {
                            List <int> nrlist = graph.newreach(new List <int> {
                                u
                            }, cd.x);
                            double b2cub1 = ratio;
                            Tuple <List <int>, double> choose = cd.gs(b2cub1, nrlist);
                            if (choose.Item2 >= maxE)
                            {
                                maxE    = choose.Item2;
                                flag    = u;
                                seedset = new List <int>();
                                foreach (int point in choose.Item1)
                                {
                                    seedset.Add(point);
                                }
                            }
                        }
                        if (bused + 1.0 > b)
                        {
                            break;
                        }
                        bused += 1.0;
                        seed.Remove(flag);
                        foreach (int u in seedset)
                        {
                            if (bused + 1.0 > b)
                            {
                                break;
                            }
                            cd.ChangeAllocation(u, 1.0);
                            cd.x.Add(u);
                            final.Add(u);
                            Console.Write(Convert.ToString(u) + " ");
                            bused += 1.0;
                        }
                        Console.WriteLine("newreach:" + Convert.ToString(cd.C.Sum()) + "total:" + Convert.ToString(bused));
                    }
                    DateTime Hyper_end  = DateTime.Now;
                    double   Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    Console.WriteLine(final.Count);

                    Hyper_start = DateTime.Now;
                    Tuple <double, double> results = icm.InfluenceSpread(graph, final, 20000, 0.7);
                    Hyper_end = DateTime.Now;
                    FileStream   outfile = new FileStream(filepath + "_9n.txt", FileMode.Append);
                    StreamWriter writer  = new StreamWriter(outfile);
                    writer.Write("Choose time:" + Hyper_time + "\t");
                    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                    writer.Write("Propagation time:" + Hyper_time + "\t");
                    writer.Write("a:" + alpha + "\tb:" + b + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "\n");
                    writer.Flush();
                    writer.Close();
                    b += 10.0;
                }
                alpha += 0.2;
            }
        }
コード例 #3
0
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph)
        {
            StreamReader  nodetype  = new StreamReader(filepath + "_typeo.txt");
            StreamReader  threshold = new StreamReader(filepath + "_tu.txt");
            List <double> thresh    = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                thresh.Add(double.Parse(threshold.ReadLine()));
            }
            List <int> type = new List <int>();

            for (int i = 0; i < graph.numV; i++)
            {
                int flag = int.Parse(nodetype.ReadLine());
                if (flag == 0)
                {
                    type.Add(0);
                }
                else if (flag == 1)
                {
                    type.Add(2);
                }
                else
                {
                    type.Add(1);
                }
            }
            List <double> d = new List <double> {
                0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
            };
            List <double> cu = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                //cu.Add(1.0);
                double t = thresh[i];
                double p = 0.0;
                if (type[i] == 1)
                {
                    p = System.Math.Pow(t, 0.5);
                }
                else if (type[i] == 2)
                {
                    p = t;
                }
                else
                {
                    p = 1 - System.Math.Pow(1 - t, 0.5);
                }
                foreach (double point in d)
                {
                    if (point < p)
                    {
                        continue;
                    }
                    cu.Add(point);
                    break;
                }
            }
            int mh = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 0.6; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 1.0)
            {
                Bipartite bg    = new Bipartite(filepath, alpha, graph.numV);
                double    b     = 10.0;
                double    ratio = 3.0;
                while (b <= 50.0)
                // Build a random hyper graph with mH random hyper edges.
                {
                    StreamReader initial = new StreamReader(filepath + "_ini100.txt");
                    List <int>   seed    = new List <int>();
                    for (int i = 0; i < 100; i++)
                    {
                        seed.Add(int.Parse(initial.ReadLine()));
                    }
                    DateTime          Hyper_start = DateTime.Now;
                    ICModel           icm         = new ICModel(alpha);
                    CoordinateDescent cd          = new CoordinateDescent(graph, bg, seed, 0.0, type, 20, alpha, mh);
                    double            bused       = 0.0;
                    while (bused <= b)
                    {
                        int        flag         = seed[0];
                        double     maxE         = 0.0;
                        int        circumstance = 0;
                        List <int> seedset      = new List <int>();
                        foreach (int u in seed)
                        {
                            List <int> nrlist = graph.newreach(new List <int> {
                                u
                            }, cd.x);
                            double b2cub1 = ratio * cu[u];
                            Tuple <List <int>, double> choose = cd.gs(b2cub1, nrlist);
                            if (choose.Item2 / cu[u] >= maxE / cu[flag])
                            {
                                maxE    = choose.Item2;
                                flag    = u;
                                seedset = new List <int>();
                                foreach (int point in choose.Item1)
                                {
                                    seedset.Add(point);
                                }
                                circumstance = 1;
                            }
                            b2cub1 = b2cub1 >= 1.0 ? 1.0 : b2cub1;
                            foreach (int v in nrlist)
                            {
                                double nowE = cd.singlepointgain(v, b2cub1);
                                if (nowE / cu[v] >= maxE / cu[flag])
                                {
                                    maxE    = nowE;
                                    flag    = u;
                                    seedset = new List <int> {
                                        v
                                    };
                                    circumstance = 2;
                                }
                            }
                        }
                        if (bused + cu[flag] > b)
                        {
                            break;
                        }
                        bused += cu[flag];
                        seed.Remove(flag);
                        List <int> maxnr = graph.newreach(new List <int> {
                            flag
                        }, cd.x);
                        if (circumstance <= 2)
                        {
                            foreach (int u in seedset)
                            {
                                double allocation = 0.0;
                                if (type[u] == 1)
                                {
                                    allocation = 1.0;
                                }
                                else
                                {
                                    allocation = 0.5;
                                }
                                //allocation = 1.0;
                                if (bused + allocation > b)
                                {
                                    break;
                                }
                                cd.ChangeAllocation(u, allocation);
                                cd.x.Add(u);
                                Console.Write(Convert.ToString(u) + " ");
                                bused += allocation;
                            }
                        }
                        else
                        {
                            double allocation = cu[flag] * ratio;
                            allocation = allocation >= 1.0 ? 1.0 : allocation;
                            if (bused + allocation > b)
                            {
                                break;
                            }
                            cd.ChangeAllocation(seedset[0], allocation);
                            cd.x.Add(seedset[0]);
                            Console.Write(Convert.ToString(seedset[0]) + "  ");
                            bused += allocation;
                        }
                        Console.WriteLine("newreach:" + Convert.ToString(cd.C.Sum()) + "total:" + Convert.ToString(bused));
                    }
                    DateTime Hyper_end  = DateTime.Now;
                    double   Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    Console.WriteLine(cd.x.Count);

                    Hyper_start = DateTime.Now;
                    List <double> prob = new List <double>();
                    foreach (int u in cd.x)
                    {
                        prob.Add(cd.SeedProb(u, cd.C[u]));
                    }
                    Tuple <double, double> results = icm.InfluenceSpread(graph, cd.x, prob, 200);
                    Hyper_end = DateTime.Now;
                    FileStream   outfile = new FileStream(filepath + "_7o.txt", FileMode.Append);
                    StreamWriter writer  = new StreamWriter(outfile);
                    writer.Write("Choose time:" + Hyper_time + "\t");
                    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                    writer.Write("Propagation time:" + Hyper_time + "\t");
                    writer.Write("a:" + alpha + "\tb:" + b + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "\n");
                    writer.Flush();
                    writer.Close();
                    b += 10.0;
                }
                alpha += 0.2;
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: zshwuhan/CIM
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph, List <int> Type, string RsltDir)
        {
            double       b      = GlobalVar.b; // Step of c of searching the best discount in th Unified Discount Algorithm
            double       alpha  = GlobalVar.Alpha;
            string       Dir    = RsltDir + "/Alpha=" + alpha;
            string       Path   = Dir + "/AllResults.txt";
            StreamWriter writer = new StreamWriter(Path);

            // Build a random hyper graph with mH random hyper edges.
            DateTime Hyper_start = DateTime.Now;
            ICModel  icm         = new ICModel(alpha);
            int      mH          = GlobalVar.mH;

            Console.WriteLine(mH);
            List <List <int> > RR = new List <List <int> >();

            for (int r = 0; r < mH; ++r)
            {
                List <int> rSet = icm.RR(graph).ToList();
                RR.Add(rSet);
                if (r > 0 && r % 100000 == 0)
                {
                    Console.WriteLine(r + " samples");
                }
            }
            Bipartite bg         = new Bipartite(RR, graph.numV);
            DateTime  Hyper_end  = DateTime.Now;
            double    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;

            Console.WriteLine("Hyper-graph has been built");

            writer.WriteLine("Hyper-graph time:\t" + Hyper_time);
            writer.Flush();

            for (int ind = GlobalVar.St; ind <= GlobalVar.End; ++ind)
            {
                double B = ind * 10.0;

                bg.Greedy((int)B);


                // Unified Discount Algorithm
                DateTime startTime = DateTime.Now;
                List <Tuple <List <int>, double> > Res = new List <Tuple <List <int>, double> >();
                for (int i = 1; i <= 20; ++i)
                {
                    double c = i * b;
                    Tuple <List <int>, double> tup = UnifiedCGreedy(graph, bg, Type, c, B, alpha);
                    Res.Add(tup);
                    Console.WriteLine("Alpha=" + alpha + "\tc=" + c + "\t" + tup.Item2);
                }


                // Discrete Influence Maximization
                double   IM_greedy                = 0;
                DateTime IM_greedy_start          = DateTime.Now;
                Tuple <List <int>, double> tup_IM = UnifiedCGreedy(graph, bg, Type, 20 * b, B, alpha);
                Res.Add(tup_IM);
                Console.WriteLine("Alpha=" + alpha + "\tc=1\t" + tup_IM.Item2);
                DateTime IM_greedy_end = DateTime.Now;
                IM_greedy = (IM_greedy_end - IM_greedy_start).TotalMilliseconds;

                //Influence Maximization results
                double     IM_sp    = Res[Res.Count - 1].Item2;
                List <int> IM_seeds = Res[Res.Count - 1].Item1;
                double     IM_ts    = Hyper_time + IM_greedy;


                // Unified Discount results (except standard deviation)
                int max_i = 0;
                for (int i = 0; i < Res.Count - 1; ++i)
                {
                    if (Res[i].Item2 > Res[max_i].Item2)
                    {
                        max_i = i;
                    }
                }
                DateTime UC_endTime = DateTime.Now;
                Console.WriteLine("Best c=" + (max_i + 1) * b + "\t" + Res[max_i].Item2);
                double     UC_sp    = Res[max_i].Item2;
                List <int> UC_seeds = Res[max_i].Item1;
                double     UC_ts    = (UC_endTime - startTime).TotalMilliseconds + Hyper_time;

                // Proceed to Coordinate Descent. Use the best result of Unified Discount as initial value
                double            init_c     = (max_i + 1) * b;
                List <int>        initNodes  = UC_seeds;
                CoordinateDescent cd         = new CoordinateDescent(graph, bg, initNodes, init_c, Type, GlobalVar.batch_num, alpha, GlobalVar.mH);
                List <double>     C          = cd.IterativeMinimize();
                DateTime          CD_endTime = DateTime.Now;
                double            CD_ts      = (CD_endTime - startTime).TotalMilliseconds + Hyper_time;


                // Evaluation using Monte Carlo Simulations
                List <Pair> IM_P = new List <Pair>();
                foreach (int u in IM_seeds)
                {
                    Pair pair = new Pair(u, 1.0);
                    IM_P.Add(pair);
                }

                List <Pair> UC_P = new List <Pair>();
                foreach (int u in UC_seeds)
                {
                    double p = 0;
                    if (Type[u] == 1)
                    {
                        p = init_c * init_c;
                    }
                    else if (Type[u] == 2)
                    {
                        p = init_c;
                    }
                    else
                    {
                        p = (2 - init_c) * init_c;
                    }
                    Pair pair = new Pair(u, p);
                    UC_P.Add(pair);
                }

                List <Pair> CD_P = new List <Pair>();
                for (int i = 0; i < graph.numV; ++i)
                {
                    double p = 0;
                    if (Type[i] == 1)
                    {
                        p = C[i] * C[i];
                    }
                    else if (Type[i] == 2)
                    {
                        p = C[i];
                    }
                    else
                    {
                        p = (2 - C[i]) * C[i];
                    }
                    Pair pair = new Pair(i, p);

                    if (p > GlobalVar.epsilon)
                    {
                        CD_P.Add(pair);
                    }
                }

                // //ICModel icm = new ICModel(alpha);
                Tuple <double, double> IM_tup = icm.InfluenceSpread(graph, IM_P, GlobalVar.MC);
                Tuple <double, double> UC_tup = icm.InfluenceSpread(graph, UC_P, GlobalVar.MC);
                Tuple <double, double> CD_tup = icm.InfluenceSpread(graph, CD_P, GlobalVar.MC);

                double Numerator = 2 * graph.numV * (1 - 1.0 / Math.E) * (Math.Log(Cnk(graph.numV, (int)B)) + Math.Log(graph.numV) + Math.Log(2.0));
                double appro     = 1 - 1.0 / Math.E - Math.Sqrt(Numerator / (IM_tup.Item1 * (double)GlobalVar.mH));
                writer.WriteLine("B=" + B);
                writer.WriteLine("IM:\t" + IM_tup.Item1 + "\t" + IM_tup.Item2 + "\t" + IM_ts + "\t" + appro);
                writer.WriteLine("UC:\t" + UC_tup.Item1 + "\t" + UC_tup.Item2 + "\t" + UC_ts);
                writer.WriteLine("CD:\t" + CD_tup.Item1 + "\t" + CD_tup.Item2 + "\t" + CD_ts);
                writer.Flush();

                string       outPath   = Dir + "/B=" + B + ".txt";
                StreamWriter outWriter = new StreamWriter(outPath);
                outWriter.WriteLine("IM\t" + IM_ts + "\t" + IM_seeds.Count);
                foreach (int u in IM_seeds)
                {
                    outWriter.WriteLine(u + "\t1\t1");
                }

                outWriter.WriteLine("UC\t" + UC_ts + "\t" + UC_seeds.Count);
                foreach (Pair pair in UC_P)
                {
                    outWriter.WriteLine(pair.id + "\t" + init_c + "\t" + pair.prob);
                }

                outWriter.WriteLine("CD\t" + CD_ts + "\t" + CD_P.Count);
                foreach (Pair pair in CD_P)
                {
                    outWriter.WriteLine(pair.id + "\t" + C[pair.id] + "\t" + pair.prob);
                }
                outWriter.Close();

                string       ccurvePath = Dir + "/curve_c(" + B + ").txt";
                StreamWriter cWriter    = new StreamWriter(ccurvePath);
                for (int i = 0; i < Res.Count; ++i)
                {
                    double c = (i + 1) * b;
                    cWriter.WriteLine(c + "\t" + Res[i].Item2);
                }
                cWriter.Close();
            }
            writer.Close();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Planet-B612/Limited_IM
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph)
        {
            StreamReader initial  = new StreamReader(filepath + "_ini100.txt");
            StreamReader nodetype = new StreamReader(filepath + "_typeo.txt");
            List <int>   type     = new List <int>();

            for (int i = 0; i < graph.numV; i++)
            {
                int flag = int.Parse(nodetype.ReadLine());
                if (flag == 0)
                {
                    type.Add(0);
                }
                else if (flag == 1)
                {
                    type.Add(2);
                }
                else
                {
                    type.Add(1);
                }
            }
            List <int> seed = new List <int>();

            for (int i = 0; i < 100; i++)
            {
                seed.Add(int.Parse(initial.ReadLine()));
            }
            int mh = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 0.6; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 0.6)
            {
                Bipartite bg = new Bipartite(filepath, alpha, graph.numV);
                int       b1 = 5;
                while (b1 <= 5)
                // Build a random hyper graph with mH random hyper edges.
                {
                    DateTime          Hyper_start = DateTime.Now;
                    ICModel           icm         = new ICModel(alpha);
                    int               count       = Convert.ToInt16(1.5 * Convert.ToDouble(b1));
                    List <int>        seed1       = graph.findlarge(seed, count);
                    CoordinateDescent cd1         = new CoordinateDescent(graph, bg, seed, seed1, 0.66667, type, 50, alpha, mh);
                    Random            random      = new Random();
                    int               b2          = 25;
                    count = Convert.ToInt16(1.5 * Convert.ToDouble(b2));
                    int i1 = 0;
                    while (i1 < 5)
                    {
                        i1++;
                        int i = seed1[random.Next(0, seed1.Count)];
                        if (Math.Abs(1.0 - cd1.C[i]) < GlobalVar.epsilon)
                        {
                            continue;
                        }
                        int j = seed1[random.Next(0, seed1.Count)];
                        if ((i == j) || Math.Abs(1.0 - cd1.C[j]) < GlobalVar.epsilon)
                        {
                            continue;
                        }
                        double bp    = cd1.C[i] + cd1.C[j];
                        double left  = (bp - 1 > 0.0) ? (bp - 1) : 0.0;
                        double right = (bp < 1.0) ? bp : 1.0;
                        if (left >= right)
                        {
                            continue;
                        }
                        double sumns = 0.0, sumnsi = 0.0, sumnsj = 0.0, sumnsij = 0.0;
                        for (int i2 = 0; i2 < 5; i2++)
                        {
                            List <int> realize = new List <int>();
                            foreach (int u in seed1)
                            {
                                if ((u == i) || (u == j))
                                {
                                    continue;
                                }
                                if (random.NextDouble() < cd1.SeedProb(u, cd1.C[u]))
                                {
                                    realize.Add(u);
                                }
                            }
                            List <int>        ns    = graph.newreach(realize, seed);
                            List <int>        seed2 = graph.findlarge(ns, count);
                            CoordinateDescent cd2   = new CoordinateDescent(graph, bg, ns, seed2, 0.66667, type, 10, alpha, mh);
                            List <double>     cns   = cd2.IterativeMinimize();
                            double            qns   = cd2.expectation();
                            sumns += qns;

                            realize.Add(i);
                            ns    = graph.newreach(realize, seed);
                            seed2 = graph.findlarge(ns, count);
                            cd2   = new CoordinateDescent(graph, bg, ns, seed2, cns, type, 10, alpha, mh);
                            List <double> cnsi = cd2.IterativeMinimize();
                            double        qnsi = cd2.expectation();
                            sumnsi += qnsi;

                            realize.Remove(i); realize.Add(j);
                            ns    = graph.newreach(realize, seed);
                            seed2 = graph.findlarge(ns, count);
                            cd2   = new CoordinateDescent(graph, bg, ns, seed2, cns, type, 10, alpha, mh);
                            List <double> cnsj = cd2.IterativeMinimize();
                            double        qnsj = cd2.expectation();
                            sumnsj += qnsj;

                            realize.Add(i);
                            ns    = graph.newreach(realize, seed);
                            seed2 = graph.findlarge(ns, count);
                            if (qnsi > qnsj)
                            {
                                cd2 = new CoordinateDescent(graph, bg, ns, seed2, cnsi, type, 10, alpha, mh);
                            }
                            else
                            {
                                cd2 = new CoordinateDescent(graph, bg, ns, seed2, cnsj, type, 10, alpha, mh);
                            }
                            List <double> cnsij = cd2.IterativeMinimize();
                            double        qnsij = cd2.expectation();
                            sumnsij += qnsij;
                        }
                        cd1.PairMinimize(i, j, sumnsi - sumns, sumnsi - sumns, sumns + sumnsij - sumnsi - sumnsj, bp, left, right);
                    }
                    DateTime Hyper_end  = DateTime.Now;
                    double   Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;

                    Hyper_start = DateTime.Now;
                    seed1       = cd1.Realize();
                    List <int>        nx = graph.newreach(seed1, seed);
                    List <int>        s  = graph.findlarge(nx, count);
                    CoordinateDescent cd = new CoordinateDescent(graph, bg, nx, s, 0.6667, type, 10, alpha, mh);
                    cd.IterativeMinimize();
                    List <double> prob = new List <double>();
                    for (int i = 0; i < nx.Count; i++)
                    {
                        prob.Add(cd.SeedProb(nx[i], cd.C[nx[i]]));
                    }
                    Tuple <double, double> results = icm.InfluenceSpread(graph, nx, prob, 200);
                    Hyper_end = DateTime.Now;
                    FileStream   outfile = new FileStream(filepath + "_4o.txt", FileMode.Append);
                    StreamWriter writer  = new StreamWriter(outfile);
                    writer.Write("Choose time:" + Hyper_time + "\t");
                    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                    writer.Write("Propagation time:" + Hyper_time + "\t");
                    writer.Write("a:" + alpha + "\tb:" + (b1 + b2) + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "\n");
                    writer.Flush();
                    writer.Close();
                    b1 += 2;
                }
                alpha += 0.0;
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: IDEAL-Lab/CIM
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph, List<int> Type, string RsltDir)
        {
            double b = GlobalVar.b; // Step of c of searching the best discount in th Unified Discount Algorithm
            double alpha = GlobalVar.Alpha;
            string Dir = RsltDir + "/Alpha=" + alpha;
            string Path = Dir + "/AllResults.txt";
            StreamWriter writer = new StreamWriter(Path);

            // Build a random hyper graph with mH random hyper edges.
            DateTime Hyper_start = DateTime.Now;
            ICModel icm = new ICModel(alpha);
            int mH = GlobalVar.mH;
            Console.WriteLine(mH);
            List<List<int>> RR = new List<List<int>>();
            for (int r = 0; r < mH; ++r)
            {
                List<int> rSet = icm.RR(graph).ToList();
                RR.Add(rSet);
                if (r > 0 && r % 100000 == 0)
                    Console.WriteLine(r + " samples");
            }
            Bipartite bg = new Bipartite(RR, graph.numV);
            DateTime Hyper_end = DateTime.Now;
            double Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
            Console.WriteLine("Hyper-graph has been built");

            writer.WriteLine("Hyper-graph time:\t" + Hyper_time);
            writer.Flush();

            for (int ind = GlobalVar.St; ind <= GlobalVar.End; ++ind)
            {
                double B = ind * 10.0;

                bg.Greedy((int)B);

                // Unified Discount Algorithm
                DateTime startTime = DateTime.Now;
                List<Tuple<List<int>, double>> Res = new List<Tuple<List<int>, double>>();
                for (int i = 1; i*b - 1.0 <= 1e-4; ++i)
                {
                    double c = i * b;
                    Tuple<List<int>, double> tup = UnifiedCGreedy(graph, bg, Type, c, B, alpha);
                    Res.Add(tup);
                    Console.WriteLine("Alpha=" + alpha + "\tc=" + c + "\t" + tup.Item2);
                }

                // Discrete Influence Maximization
                double IM_greedy = 0;
                DateTime IM_greedy_start = DateTime.Now;
                Tuple<List<int>, double> tup_IM = UnifiedCGreedy(graph, bg, Type, 20 * b, B, alpha);
                Res.Add(tup_IM);
                Console.WriteLine("Alpha=" + alpha + "\tc=1\t" + tup_IM.Item2);
                DateTime IM_greedy_end = DateTime.Now;
                IM_greedy = (IM_greedy_end - IM_greedy_start).TotalMilliseconds;

                //Influence Maximization results
                double IM_sp = Res[Res.Count - 1].Item2;
                List<int> IM_seeds = Res[Res.Count - 1].Item1;
                double IM_ts = Hyper_time + IM_greedy;

                // Unified Discount results (except standard deviation)
                int max_i = 0;
                for (int i = 0; i < Res.Count - 1; ++i)
                {
                    if (Res[i].Item2 > Res[max_i].Item2)
                        max_i = i;
                }
                DateTime UC_endTime = DateTime.Now;
                Console.WriteLine("Best c=" + (max_i + 1) * b + "\t" + Res[max_i].Item2);
                double UC_sp = Res[max_i].Item2;
                List<int> UC_seeds = Res[max_i].Item1;
                double UC_ts = (UC_endTime - startTime).TotalMilliseconds + Hyper_time;

                // Proceed to Coordinate Descent. Use the best result of Unified Discount as initial value
                double init_c = (max_i + 1) * b;
                List<int> initNodes = UC_seeds;
                CoordinateDescent cd = new CoordinateDescent(graph, bg, initNodes, init_c, Type, GlobalVar.batch_num, alpha, GlobalVar.mH);
                List<double> C = cd.IterativeMinimize();
                DateTime CD_endTime = DateTime.Now;
                double CD_ts = (CD_endTime - startTime).TotalMilliseconds + Hyper_time;

                // Evaluation using Monte Carlo Simulations
                List<Pair> IM_P = new List<Pair>();
                foreach (int u in IM_seeds)
                {
                    Pair pair = new Pair(u, 1.0);
                    IM_P.Add(pair);
                }

                List<Pair> UC_P = new List<Pair>();
                foreach (int u in UC_seeds)
                {
                    double p = 0;
                    if (Type[u] == 1)
                        p = init_c * init_c;
                    else if (Type[u] == 2)
                        p = init_c;
                    else
                        p = (2 - init_c) * init_c;
                    Pair pair = new Pair(u, p);
                    UC_P.Add(pair);
                }

                List<Pair> CD_P = new List<Pair>();
                for (int i = 0; i < graph.numV; ++i)
                {
                    double p = 0;
                    if (Type[i] == 1)
                        p = C[i] * C[i];
                    else if (Type[i] == 2)
                        p = C[i];
                    else
                        p = (2 - C[i]) * C[i];
                    Pair pair = new Pair(i, p);

                    if (p > GlobalVar.epsilon)
                        CD_P.Add(pair);
                }

                // //ICModel icm = new ICModel(alpha);
                Tuple<double, double> IM_tup = icm.InfluenceSpread(graph, IM_P, GlobalVar.MC);
                Tuple<double, double> UC_tup = icm.InfluenceSpread(graph, UC_P, GlobalVar.MC);
                Tuple<double, double> CD_tup = icm.InfluenceSpread(graph, CD_P, GlobalVar.MC);

                double Numerator = 2 * graph.numV * (1 - 1.0 / Math.E) * (Math.Log(Cnk(graph.numV, (int)B)) + Math.Log(graph.numV) + Math.Log(2.0));
                double appro = 1 - 1.0 / Math.E - Math.Sqrt(Numerator / (IM_tup.Item1*(double)GlobalVar.mH));
                writer.WriteLine("B=" + B);
                writer.WriteLine("IM:\t" + IM_tup.Item1 + "\t" + IM_tup.Item2 + "\t" + IM_ts + "\t" + appro);
                writer.WriteLine("UC:\t" + UC_tup.Item1 + "\t" + UC_tup.Item2 + "\t" + UC_ts);
                writer.WriteLine("CD:\t" + CD_tup.Item1 + "\t" + CD_tup.Item2 + "\t" + CD_ts);
                writer.Flush();

                string outPath = Dir + "/B=" + B + ".txt";
                StreamWriter outWriter = new StreamWriter(outPath);
                outWriter.WriteLine("IM\t" + IM_ts + "\t" + IM_seeds.Count);
                foreach (int u in IM_seeds)
                    outWriter.WriteLine(u + "\t1\t1");

                outWriter.WriteLine("UC\t" + UC_ts + "\t" + UC_seeds.Count);
                foreach (Pair pair in UC_P)
                    outWriter.WriteLine(pair.id + "\t" + init_c + "\t" + pair.prob);

                outWriter.WriteLine("CD\t" + CD_ts + "\t" + CD_P.Count);
                foreach (Pair pair in CD_P)
                    outWriter.WriteLine(pair.id + "\t" + C[pair.id] + "\t" + pair.prob);
                outWriter.Close();

                string ccurvePath = Dir + "/curve_c(" + B + ").txt";
                StreamWriter cWriter = new StreamWriter(ccurvePath);
                for (int i = 0; i < Res.Count; ++i)
                {
                    double c = (i + 1) * b;
                    cWriter.WriteLine(c + "\t" + Res[i].Item2);
                }
                cWriter.Close();
            }
            writer.Close();

            ProcessData4Visual.Fig3(Path, Dir + "/Fig3.txt");
            ProcessData4Visual.Fig4(Path, Dir + "/Fig4.txt");
            ProcessData4Visual.Fig5(Dir + "/curve_c(50).txt", Dir + "/Fig5.txt");
            ProcessData4Visual.Fig6(Path, Dir + "/Fig6.txt");
        }
コード例 #7
0
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph)
        {
            StreamReader  nodetype  = new StreamReader(filepath + "_typeo.txt");
            StreamReader  threshold = new StreamReader(filepath + "_tu.txt");
            List <double> thresh    = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                thresh.Add(double.Parse(threshold.ReadLine()));
            }
            List <int> type = new List <int>();

            for (int i = 0; i < graph.numV; i++)
            {
                int flag = int.Parse(nodetype.ReadLine());
                if (flag == 0)
                {
                    type.Add(0);
                }
                else if (flag == 1)
                {
                    type.Add(2);
                }
                else
                {
                    type.Add(1);
                }
            }
            List <double> d = new List <double> {
                0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
            };
            List <double> cu = new List <double>();

            for (int i = 0; i < graph.numV; i++)
            {
                double t = thresh[i];
                double p = 0.0;
                if (type[i] == 1)
                {
                    p = System.Math.Pow(t, 0.5);
                }
                else if (type[i] == 2)
                {
                    p = t;
                }
                else
                {
                    p = 1 - System.Math.Pow(1 - t, 0.5);
                }
                foreach (double point in d)
                {
                    if (point < p)
                    {
                        continue;
                    }
                    cu.Add(point);
                    break;
                }
            }
            int mh = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 1.0; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 1.0)
            {
                Bipartite     bg     = new Bipartite(filepath, alpha, graph.numV);
                double        btotal = 30.0;
                List <double> ratio  = new List <double> {
                    1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0
                };
                while (btotal <= 30.0)
                // Build a random hyper graph with mH random hyper edges.
                {
                    foreach (double r in ratio)
                    {
                        double       b1      = btotal / (1.0 + r);
                        StreamReader initial = new StreamReader(filepath + "_ini100.txt");
                        List <int>   seed    = new List <int>();
                        for (int i = 0; i < 100; i++)
                        {
                            seed.Add(int.Parse(initial.ReadLine()));
                        }
                        DateTime          Hyper_start = DateTime.Now;
                        ICModel           icm         = new ICModel(alpha);
                        CoordinateDescent cd          = new CoordinateDescent(graph, bg, seed, 0.0, type, 10, alpha, mh);
                        double            b1used      = 0.0;
                        while (b1used < b1)
                        {
                            int           flag = seed[0];
                            double        maxE = 0.0;
                            List <double> maxC = new List <double>();
                            foreach (int u in seed)
                            {
                                List <int> nrlist = graph.newreach(new List <int> {
                                    u
                                }, cd.x);
                                double     b2cub1 = r * cu[u];
                                int        count  = Convert.ToInt16(Math.Ceiling(1.5 * b2cub1));
                                double     init_c = b2cub1 / Convert.ToDouble(count);
                                List <int> choose = graph.findlarge(nrlist, count);
                                foreach (int v in choose)
                                {
                                    cd.ChangeAllocation(v, init_c);
                                }
                                cd.initNodes = nrlist;
                                cd.IterativeMinimize();
                                double nowE = cd.expectation();
                                if (nowE / cu[u] >= maxE / cu[flag])
                                {
                                    flag = u;
                                    maxE = nowE;
                                    maxC = new List <double>();
                                    for (int pos = 0; pos < cd.C.Count; pos++)
                                    {
                                        maxC.Add(cd.C[pos]);
                                    }
                                }
                                foreach (int v in nrlist)
                                {
                                    cd.ChangeAllocation(v, 0.0);
                                }
                            }
                            Console.WriteLine(cd.C.Sum());
                            b1used += cu[flag];
                            if (b1used > b1)
                            {
                                break;
                            }
                            seed.Remove(flag);
                            List <int> maxnr = graph.newreach(new List <int> {
                                flag
                            }, cd.x);
                            foreach (int u in maxnr)
                            {
                                cd.ChangeAllocation(u, maxC[u]);
                                cd.x.Add(u);
                            }
                        }
                        DateTime Hyper_end  = DateTime.Now;
                        double   Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;

                        Hyper_start = DateTime.Now;
                        List <double> prob = new List <double>();
                        foreach (int u in cd.x)
                        {
                            prob.Add(cd.SeedProb(u, cd.C[u]));
                        }
                        Tuple <double, double> results = icm.InfluenceSpread(graph, cd.x, prob, 200);
                        Hyper_end = DateTime.Now;
                        FileStream   outfile = new FileStream(filepath + "_6o.txt", FileMode.Append);
                        StreamWriter writer  = new StreamWriter(outfile);
                        writer.Write("Choose time:" + Hyper_time + "\t");
                        Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                        string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                        writer.Write("Propagation time:" + Hyper_time + "\t");
                        writer.Write("a:" + alpha + "\tb:" + btotal + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "ratio" + Convert.ToString(r) + "\n");
                        writer.Flush();
                        writer.Close();
                    }
                    btotal += 10.0;
                }
                alpha += 0.2;
            }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: Planet-B612/Limited_IM
        public static void CoordinateDescentAlgCommonHyperGraphOneAlpha(Graph graph)
        {
            StreamReader initial  = new StreamReader(filepath + "_ini1000.txt");
            StreamReader nodetype = new StreamReader(filepath + "_typeo.txt");
            List <int>   type     = new List <int>();

            for (int i = 0; i < graph.numV; i++)
            {
                int flag = int.Parse(nodetype.ReadLine());
                if (flag == 0)
                {
                    type.Add(0);
                }
                else if (flag == 1)
                {
                    type.Add(2);
                }
                else
                {
                    type.Add(1);
                }
            }
            List <int> initNodes = new List <int>();

            for (int i = 0; i < 1000; i++)
            {
                initNodes.Add(int.Parse(initial.ReadLine()));
            }
            int mh = 0;

            if (filepath.Contains("Wiki"))
            {
                mh = 250000;
            }
            else if (filepath.Contains("CA"))
            {
                mh = 2000000;
            }
            else if (filepath.Contains("dblp"))
            {
                mh = 20000000;
            }
            else
            {
                mh = 40000000;
            }
            double alpha = 1.0; // Step of c of searching the best discount in th Unified Discount Algorithm

            while (alpha <= 1.0)
            {
                Bipartite bg = new Bipartite(filepath, alpha, graph.numV);
                int       b  = 10;
                while (b <= 50)
                // Build a random hyper graph with mH random hyper edges.
                {
                    DateTime   Hyper_start = DateTime.Now;
                    ICModel    icm         = new ICModel(alpha);
                    int        count       = Convert.ToInt16(1.5 * Convert.ToDouble(b));
                    List <int> degrees     = new List <int>();
                    for (int i = 0; i < 1000; i++)
                    {
                        degrees.Add(graph.adj[initNodes[i]].Count);
                    }
                    List <int> seedNodes = new List <int>();
                    while (count > 0)
                    {
                        int flag = 0;
                        for (int i = 1; i < 1000; i++)
                        {
                            if (degrees[i] > degrees[flag])
                            {
                                flag = i;
                            }
                        }
                        seedNodes.Add(initNodes[flag]);
                        degrees[flag] = -1;
                        count--;
                    }
                    CoordinateDescent cd         = new CoordinateDescent(graph, bg, initNodes, seedNodes, 1 / 1.5, type, 50, alpha, mh);
                    List <double>     allocation = cd.IterativeMinimize();
                    DateTime          Hyper_end  = DateTime.Now;
                    double            Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    FileStream        outfile    = new FileStream(filepath + "_3l.txt", FileMode.Append);
                    StreamWriter      writer     = new StreamWriter(outfile);
                    writer.Write("Choose time:" + Hyper_time + "\t");

                    List <double> prob = new List <double>();
                    for (int i = 0; i < initNodes.Count; i++)
                    {
                        prob.Add(cd.SeedProb(initNodes[i], allocation[initNodes[i]]));
                    }

                    Hyper_start = DateTime.Now;
                    Tuple <double, double> results = icm.InfluenceSpread(graph, initNodes, prob, 20000);
                    Hyper_end = DateTime.Now;
                    string mem = Convert.ToString(Process.GetCurrentProcess().WorkingSet64 / 8 / 1024 / 1024);
                    Hyper_time = (Hyper_end - Hyper_start).TotalMilliseconds;
                    writer.Write("Propagation time:" + Hyper_time + "\t");
                    writer.Write("a:" + alpha + "\tb:" + b + "\tave:" + results.Item1 + "\tstd:" + results.Item2 + "\tmemory:" + mem + "\n");
                    writer.Flush();
                    writer.Close();
                    b += 10;
                }
                alpha += 0.2;
            }
        }