Пример #1
0
        public void Run(int start_seed, int final_seed)
        {
            string save_folder = this.LogFolder;
            var    graphs      = this.MyGraphs;
            var    algos       = this.MyAlgos;

            int    op_dim_size = this.DimSize;
            double sensor_rate = this.SensorRate;


            int max = 0;

            for (int size = this.StartSize; size <= FinalSize; size += DurationSize)
            {
                foreach (var select_graph in graphs)
                {
                    for (int seed = start_seed; seed <= final_seed; seed++)
                    {
                        foreach (var algo in algos)
                        {
                            foreach (var weight in this.CommonCuriocities)
                            {
                                max++;
                            }
                        }
                    }
                }
            }

            var pb = new ExtendProgressBar(max);

            for (int size = this.StartSize; size <= FinalSize; size += DurationSize)
            {
                GraphGeneratorBase graph_generator = new Null_GraphGenerator();

                foreach (var select_graph in graphs)
                {
                    for (int seed = start_seed; seed <= final_seed; seed++)
                    {
                        switch (select_graph)
                        {
                        case GraphEnum.WS:
                            graph_generator = new WS_GraphGenerator().SetNodeSize(size).SetNearestNeighbors(6).SetRewireP(0.01);
                            break;

                        case GraphEnum.BA:
                            graph_generator = new BA_GraphGenerator().SetNodeSize(size).SetAttachEdges(2);
                            break;

                        case GraphEnum.Hexagonal:
                            graph_generator = new Hexagonal_GraphGenerator().SetNodeSize(size);
                            break;

                        case GraphEnum.Grid2D:
                            graph_generator = new Grid2D_GraphGenerator().SetNodeSize(size);
                            break;

                        case GraphEnum.Triangular:
                            graph_generator = new Triangular_GraphGenerator().SetNodeSize(size);
                            break;

                        default:
                            new Exception();
                            return;
                        }
                        var graph  = new RawGraph();
                        var layout = new Layout();

                        lock (lock_object)
                        {
                            graph  = graph_generator.Generate(seed, pb);
                            layout = new Circular_LayoutGenerator(graph).Generate(pb);
                        }


                        var init_belief_gene = new InitBeliefGenerator()
                                               .SetInitBeliefMode(mode: InitBeliefMode.NormalNarrow);

                        var subject_test = new OpinionSubject("test", op_dim_size);


                        var osm_env = new OpinionEnvironment()
                                      .SetSubject(subject_test)
                                      .SetCorrectDim(0)
                                      .SetSensorWeight(sensor_rate);

                        var subject_manager = new SubjectManager()
                                              .AddSubject(subject_test)
                                              .SetEnvironment(osm_env);


                        var op_form_threshold = 0.9;

                        var sample_agent_test = new SampleAgent()
                                                .SetInitBeliefGene(init_belief_gene)
                                                .SetThreshold(op_form_threshold)
                                                .SetSubject(subject_test)
                                                .SetInitOpinion(Vector <double> .Build.Dense(op_dim_size, 0.0));

                        var sensor_gene = new SensorGenerator();
                        if (this.SensorSizeFixMode)
                        {
                            sensor_gene.SetSensorSize(this.SensorSize);
                        }
                        else
                        {
                            sensor_gene.SetSensorSize((int)(this.SensorSizeRate * graph.Nodes.Count));
                        }

                        int agent_gene_seed = seed;
                        var agent_gene_rand = new ExtendRandom(agent_gene_seed);


                        var agent_network = new AgentNetwork()
                                            .SetRand(agent_gene_rand)
                                            .GenerateNetworkFrame(graph)
                                            .ApplySampleAgent(sample_agent_test, mode: SampleAgentSetMode.RemainSet)
                                            .GenerateSensor(sensor_gene)
                                            .SetLayout(layout);

                        int update_step_seed = seed;

                        foreach (var algo in algos)
                        {
                            OSMBase osm = new OSM_Only();

                            foreach (var cc in this.CommonCuriocities)
                            {
                                switch (algo)
                                {
                                case AlgoEnum.IWTori:
                                    var osm_iwtori = new IWTori_OSM();
                                    osm_iwtori.SetCommonCuriocity(cc);
                                    osm_iwtori.SetTargetH(this.TargetH);
                                    osm = osm_iwtori;
                                    break;

                                case AlgoEnum.IWTorionly:
                                    var osm_iwtorionly = new IWTorionly_OSM();
                                    osm_iwtorionly.SetCommonWeight(this.CommonWeight);
                                    osm_iwtorionly.SetCommonCuriocity(cc);
                                    osm = osm_iwtorionly;
                                    break;

                                default:
                                    break;
                                }

                                var update_step_rand_tmp = new ExtendRandom(update_step_seed);
                                osm.SetRand(update_step_rand_tmp);
                                osm.SetAgentNetwork(agent_network);
                                osm.SetSubjectManager(subject_manager);
                                osm.SetInitWeightsMode(mode: CalcWeightMode.FavorMyOpinion);
                                osm.SetOpinionIntroInterval(10);
                                osm.SetOpinionIntroRate(0.1);
                                osm.SimpleRecordFlag = true;
                                osm.SetBeliefUpdater(this.MyBeliefUpdater);


                                pb.Tag = $"{select_graph.ToString()} {size.ToString()} {algo.ToString()} {seed}";
                                osm.UpdateRounds(this.Rounds, this.Steps, pb);

                                string sensor_size_mode = "";
                                if (this.SensorSizeFixMode)
                                {
                                    sensor_size_mode = "fix" + this.SensorSize.ToString();
                                }
                                else
                                {
                                    sensor_size_mode = "rate" + Math.Round(this.SensorSizeRate, 3).ToString();
                                }

                                string sensor_weight_mode = $"{this.MyBeliefUpdater.SensorWeightMode}";

                                var output_pass = Properties.Settings.Default.OutputLogPath
                                                  + $"/{save_folder}/"
                                                  + select_graph.ToString()
                                                  + $"_{size.ToString()}"
                                                  + $"_{sensor_size_mode}"
                                                  + $"_{op_dim_size.ToString()}"
                                                  + $"_{sensor_rate.ToString()}"
                                                  + $"_{algo.ToString()}"
                                                  + $"_{sensor_weight_mode}"
                                                  + $"_{this.Rounds}"
                                                  + $"_{this.Steps}"
                                                  + $"_{cc}";

                                Output.OutputRounds(output_pass, osm.MyRecordRounds, seed.ToString());
                                pb.Next();
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        void Test()
        {
            this.osm_setting = new OSMSetting(ExperimentType.AAT_SW_Normal_Random_Exponential);
            int      agent_size            = osm_setting.agent_size;
            int      dim                   = osm_setting.dim;
            int      correct_dim           = osm_setting.correct_dim;
            int      malicious_dim         = osm_setting.malicious_dim;
            AlgoEnum algo                  = osm_setting.algo;
            double   targeth               = osm_setting.targeth;
            double   common_weight         = osm_setting.common_weight;
            double   common_curiocity      = osm_setting.common_curiocity;
            double   sensor_weight         = osm_setting.sensor_weight;
            double   dist_weight           = osm_setting.dist_weight;
            double   malicious_dist_weight = osm_setting.malicious_dist_weight;
            int      sensor_size           = osm_setting.sensor_size;
            //int malicious_sensor_size = (int)(0.04 * agent_size);
            int malicious_sensor_size = osm_setting.malicious_sensor_size;
            var op_form_threshold     = osm_setting.op_form_threshold;
            int sample_size           = osm_setting.sample_size;
            int change_round          = osm_setting.change_round;

            bool bad_sensor_mode             = osm_setting.bad_sensor_mode;
            int  opinion_share_num           = osm_setting.opinion_share_num;
            bool is_add_share_only_community = osm_setting.add_share_only_community;

            EnvDistributionEnum      env_distribution = osm_setting.env_distribution;
            BeliefUpdateFunctionEnum belief_update    = osm_setting.belief_update;

            var belief_updater = new BeliefUpdater(belief_update).SetSensorWeightMode(SensorWeightEnum.DependSensorRate);

            GraphGeneratorBase graph_generator;

            switch (osm_setting.select_graph)
            {
            case GraphEnum.WS:
                graph_generator = new WS_GraphGenerator().SetNodeSize(agent_size).SetNearestNeighbors(6).SetRewireP(0.01);
                break;

            //case GraphEnum.PowerLawCluster:
            //graph_generator = new PC_GraphGenerator().SetNodeSize(500).SetRandomEdges(3).SetAddTriangleP(0.1);
            // break;
            case GraphEnum.BA:
                graph_generator = new BA_GraphGenerator().SetNodeSize(agent_size).SetAttachEdges(2);
                break;

            case GraphEnum.Grid2D:
                graph_generator = new Grid2D_GraphGenerator().SetNodeSize(agent_size);
                break;

            //case GraphEnum.ER:
            //graph_generator = new ER_GraphGenerator().SetNodeSize(agent_size).SetEdgeCreateP(0.01);
            //break;
            case GraphEnum.LFR:
                graph_generator = new LFR_GraphGenerator().SetNodeSize(agent_size);
                break;

            default:
                graph_generator = new LFR_GraphGenerator().SetNodeSize(agent_size);
                break;
            }
            graph_generator.SetOsmSetting(osm_setting);
            var pb     = new ExtendProgressBar(100);
            var graph  = graph_generator.Generate(0, pb);
            var layout = new KamadaKawai_LayoutGenerator(graph).Generate(pb);
            //var layout = new Circular_LayoutGenerator(graph).Generate();

            //LFRの時のみ
            List <List <int> > communityList = new List <List <int> >();

            if (osm_setting.select_graph == GraphEnum.LFR)
            {
                communityList = graph_generator.GetCommunity();
            }
            var init_belief_gene = new InitBeliefGenerator()
                                   .SetInitBeliefMode(mode: InitBeliefMode.NormalNarrow);
            //.SetInitBeliefMode(mode: InitBeliefMode.NormalWide);

            var subject_test = new OpinionSubject("test", dim);

            var sample_agent_test = new SampleAgent()
                                    .SetInitBeliefGene(init_belief_gene)
                                    .SetThreshold(op_form_threshold)
                                    .SetSubject(subject_test)
                                    .SetInitOpinion(Vector <double> .Build.Dense(dim, 0.0));

            var sensor_gene = new SensorGenerator()
                              //                .SetSensorSize((int)5);
                              .SetSensorSize(sensor_size, malicious_sensor_size);

            int agent_gene_seed = 11;//4
            var agent_gene_rand = new ExtendRandom(agent_gene_seed);


            var agent_network = new AgentNetwork()
                                .SetRand(agent_gene_rand)
                                .GenerateNetworkFrame(graph)
                                //.ApplySampleAgent(sample_agent_1, mode: SampleAgentSetMode.RandomSetRate, random_set_rate: 0.5)
                                //.ApplySampleAgent(sample_agent_2, mode: SampleAgentSetMode.RemainSet)
                                .ApplySampleAgent(sample_agent_test, mode: SampleAgentSetMode.RemainSet)
                                .SetBadSensorMode(osm_setting.bad_sensor_mode)
                                .SetSensorArrange(osm_setting.sensor_arrange)
                                .SetCommnityList(communityList)
                                .GenerateSensor(sensor_gene)
                                .SetLayout(layout);
            var bad_community_index = agent_network.GetBadCommunityIndex();

            int update_step_seed = 1;
            var update_step_rand = new ExtendRandom(update_step_seed);

            OSMBase osm = new OSM_Only();

            switch (algo)
            {
            case AlgoEnum.AAT:
                var osm_aat = new AAT_OSM();
                osm_aat.SetTargetH(targeth);
                osm = osm_aat;
                break;

            default:
                break;
            }
            osm.SetRand(update_step_rand);
            osm.SetAgentNetwork(agent_network, opinion_share_num);
            var subject_mgr_dic = new Dictionary <int, SubjectManager>();

            subject_mgr_dic.Add(0, new SubjectManagerGenerator()
                                .Generate(subject_test, dist_weight, correct_dim, sensor_weight, env_distribution, malicious_dim, malicious_dist_weight));
            //for (int i = 0; i < 1; i++)
            //{
            //  subject_mgr_dic.Add(i * change_round, new SubjectManagerGenerator().Generate(subject_test, dist_weight, i % dim, sensor_rate, EnvDistributionEnum.Turara));
            //}
            osm.SetSubjectManagerDic(subject_mgr_dic);
            osm.SetInitWeightsMode(mode: CalcWeightMode.FavorMyOpinion);
            osm.SetOpinionIntroInterval(osm_setting.op_intro_interval);
            osm.SetOpinionIntroRate(osm_setting.op_intro_rate);
            //osm.SetSensorCommonWeight(0.70);
            osm.SetBeliefUpdater(belief_updater);
            osm.SetAddShareCommunityOnly(is_add_share_only_community);

            osm.SetStopRound(osm_setting.stop_rounds);

            this.MyOSM = osm;
            this.MyAnimationForm.RegistOSM(osm, osm_setting.opinion_share_num, communityList, bad_community_index);
        }
Пример #3
0
        public void Run(int start_seed, int final_seed)
        {
            string save_folder = this.LogFolder;
            var    graphs      = this.MyGraphs;
            var    algos       = this.MyAlgos;
            var    dims        = this.MyDims;

            //int op_dim_size = this.DimSize;
            double sensor_weight = this.SensorWeight;


            int max = 0;

            foreach (var size in this.NetworkSizes)                             //リスト中のサイズを一つ取り出す
            {
                foreach (var select_graph in graphs)                            //リスト中のグラフを一つ取り出す
                {
                    for (int seed = start_seed; seed <= final_seed; seed++)     //スタートからファイナルまでシードをインクリメント
                    {
                        foreach (var algo in algos)                             //リスト中の意見共有アルゴリズムを一つ取り出す
                        {
                            foreach (var dim in dims)                           //リスト中の「意見数」を一つ取り出す
                            {
                                foreach (var target_h in this.TargetHs)         //リスト中の目標意見形成率を一つ取り出す
                                {
                                    foreach (var is_dynamic in this.IsDynamics) //リスト中のダイナミクスを一つ取り出す
                                    {
                                        foreach (var env_dist_mode in this.EnvDisModes)
                                        {
                                            foreach (var env_dist_weight in this.EnvDistWeights)                  //環境からセンサエージェントへのリンクの重み
                                            {
                                                foreach (var mal_env_dist_weight in this.MaliciousEnvDistWeights) //誤った環境からセンサエージェントへのリンクの重み
                                                {
                                                    foreach (var info_weight_rate in this.InfoWeightRates)
                                                    {
                                                        foreach (var round in this.Rounds)                                          //リスト中のラウンドを一つ取り出す
                                                        {
                                                            foreach (var sensor_size_rate in this.SensorSizeRates)                  //環境観測センサの比率
                                                            {
                                                                foreach (var mal_sensor_size_rate in this.MaliciousSensorSizeRates) //誤情報環境観測センサの比率
                                                                {
                                                                    foreach (var common_weight in this.CommonWeights)
                                                                    {
                                                                        foreach (var bad_sensor_mode in this.BadSensorMode)
                                                                        {
                                                                            foreach (var opinion_share_num in this.OpinionShareNum)
                                                                            {
                                                                                foreach (var sensor_arrangement in this.SensorArrangementMode)
                                                                                {
                                                                                    foreach (var add_share_only_community in this.IsAdditionalShareCommunityOnly)
                                                                                    {
                                                                                        max++;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var pb = new ExtendProgressBar(max);                                //maxが総実行数.プログレスバーに100%の実行数を渡す

            foreach (var size in this.NetworkSizes)                             //サイズはネットワークのノード数
            {
                GraphGeneratorBase graph_generator = new Null_GraphGenerator(); // GraphGeneratorBase型として生成

                foreach (var select_graph in graphs)
                {
                    foreach (var op_dim_size in dims)  //意見の種類
                    {
                        foreach (var is_diynamic in this.IsDynamics)
                        {
                            foreach (var env_dist_mode in this.EnvDisModes)
                            {
                                foreach (var env_dist_weight in this.EnvDistWeights)
                                {
                                    foreach (var mal_env_dist_weight in this.MaliciousEnvDistWeights)
                                    {
                                        foreach (var info_weight_rate in this.InfoWeightRates)
                                        {
                                            foreach (var round in this.Rounds)                         //設定した「ラウンド数」のリストから一つ取り出す
                                            {
                                                foreach (var sensor_size_rate in this.SensorSizeRates) //センササイズ率
                                                {
                                                    foreach (var mal_sensor_size_rate in this.MaliciousSensorSizeRates)
                                                    {
                                                        foreach (var common_weight in this.CommonWeights)
                                                        {
                                                            foreach (var bad_sensor_mode in this.BadSensorMode)
                                                            {
                                                                foreach (var opinion_share_num in this.OpinionShareNum)
                                                                {
                                                                    foreach (var sensor_arrangement in this.SensorArrangementMode)
                                                                    {
                                                                        foreach (var add_share_only_community in this.IsAdditionalShareCommunityOnly)
                                                                        {
                                                                            for (int seed = start_seed; seed <= final_seed; seed++)
                                                                            {
                                                                                switch (select_graph)
                                                                                {
                                                                                case GraphEnum.WS:
                                                                                    graph_generator = new WS_GraphGenerator().SetNodeSize(size).SetNearestNeighbors(6).SetRewireP(0.01);
                                                                                    //graph_generator = new WS_GraphGenerator().SetNodeSize(size).SetNearestNeighbors(15).SetRewireP(0.1);
                                                                                    break;

                                                                                case GraphEnum.BA:
                                                                                    graph_generator = new BA_GraphGenerator().SetNodeSize(size).SetAttachEdges(2);
                                                                                    //graph_generator = new BA_GraphGenerator().SetNodeSize(size).SetAttachEdges(10);
                                                                                    break;

                                                                                case GraphEnum.Hexagonal:
                                                                                    graph_generator = new Hexagonal_GraphGenerator().SetNodeSize(size);
                                                                                    break;

                                                                                case GraphEnum.Grid2D:
                                                                                    graph_generator = new Grid2D_GraphGenerator().SetNodeSize(size);    //ノード数を渡す
                                                                                    break;

                                                                                case GraphEnum.Triangular:
                                                                                    graph_generator = new Triangular_GraphGenerator().SetNodeSize(size);
                                                                                    break;

                                                                                case GraphEnum.KarateClub:
                                                                                    graph_generator = new KarateClub_GraphGenerator().SetNodeSize(size);
                                                                                    break;

                                                                                case GraphEnum.LFR:
                                                                                    graph_generator = new LFR_GraphGenerator().SetNodeSize(size);
                                                                                    break;

                                                                                default:
                                                                                    new Exception();
                                                                                    return;
                                                                                }
                                                                                var graph  = new RawGraph();
                                                                                var layout = new Layout();
                                                                                graph_generator.SetOsmSetting(osm_setting);

                                                                                lock (lock_object)
                                                                                {
                                                                                    graph  = graph_generator.Generate(seed, pb);               //pythonを使ってグラフ(ノードとリンク)生成
                                                                                    layout = new Circular_LayoutGenerator(graph).Generate(pb); //グラフに合わせたレイアウト生成
                                                                                }

                                                                                List <List <int> > communityList = new List <List <int> >(); //LFRでないときはnullでok
                                                                                if (bad_sensor_mode && select_graph == GraphEnum.LFR)
                                                                                {
                                                                                    communityList = graph_generator.GetCommunity();
                                                                                }


                                                                                var init_belief_gene = new InitBeliefGenerator() //信念値更新式生成
                                                                                                       .SetInitBeliefMode(mode: InitBeliefMode.NormalNarrow);

                                                                                var subject_test = new OpinionSubject(this.SubjectName, op_dim_size);                    //今回はName=test.OpinionSubjectにそのままセットしてインスタンス化

                                                                                var sample_agent_test = new SampleAgent()                                                //信念値生成器,意見形成率,意見数,意見をもつ
                                                                                                        .SetInitBeliefGene(init_belief_gene)                             //信念値生成器をセット
                                                                                                        .SetThreshold(this.OpinionThreshold)                             //意見形成の閾値をセット
                                                                                                        .SetSubject(subject_test)                                        //サブジェクトをセット
                                                                                                        .SetInitOpinion(Vector <double> .Build.Dense(op_dim_size, 0.0)); //InitOpinionに意見数分,0.0の初期値でinit

                                                                                var sensor_gene = new SensorGenerator();                                                 //エージェント生成器
                                                                                if (this.SensorSizeFixMode)
                                                                                {
                                                                                    //you will add malicious sensor size
                                                                                    sensor_gene.SetSensorSize(this.SensorSize);
                                                                                }
                                                                                else
                                                                                {
                                                                                    int sensor_size     = (int)(sensor_size_rate * graph.Nodes.Count);     //nodeの数×センササイズ比=(正情報)環境観測センサの数
                                                                                    int mal_sensor_size = (int)(mal_sensor_size_rate * graph.Nodes.Count); //nodeの数×誤情報センササイズ比=誤情報環境観測センサの数
                                                                                    sensor_gene.SetSensorSize(sensor_size, mal_sensor_size);               //そのままセンササイズをセット
                                                                                }

                                                                                int agent_gene_seed = seed;                              //シードをセット
                                                                                var agent_gene_rand = new ExtendRandom(agent_gene_seed); //ランダム生成器


                                                                                var agent_network = new AgentNetwork()                                                       //エージェントとリンクを生成
                                                                                                    .SetRand(agent_gene_rand)                                                //ランダム生成器セット
                                                                                                    .GenerateNetworkFrame(graph)                                             //pythonで生成したエージェントとリンクをAgentsクラス,AgentLinksクラスに結びつける
                                                                                                    .ApplySampleAgent(sample_agent_test, mode: SampleAgentSetMode.RemainSet) //サンプルエージェント,セットモード

                                                                                                                                                                             /*センサの設定*/
                                                                                                    .SetBadSensorMode(bad_sensor_mode)
                                                                                                    .SetCommnityList(communityList)
                                                                                                    .SetCommunityId()
                                                                                                    .GenerateSensor(sensor_gene) //各エージェントに対して,一定の確率で選ばれたエージェントを正情報センサ,誤情報センサとする
                                                                                                    .SetLayout(layout);          //AgenetNetworkにレイアウトをセット

                                                                                /***************************************多分ここで環境設定終わり**********************************************/

                                                                                int update_step_seed = seed;                //シードセット

                                                                                foreach (var algo in algos)                 //設定したアルゴリズムを順番に試すよ
                                                                                {
                                                                                    OSMBase osm = new OSM_Only();           //とりあえずベースとなるクラスで初期化
                                                                                    foreach (var target_h in this.TargetHs) //意見形成目標率を順番に試すよ
                                                                                    {
                                                                                        var sample_size       = 10;         //現在は使ってない説
                                                                                        var awa_rate_window   = 100;        //こっちは使ってない?
                                                                                        var info_value_window = 50;
                                                                                        //var info_value_window = 100;
                                                                                        var op_intro_interval = osm_setting.op_intro_interval;
                                                                                        var op_intro_rate     = 0.1;
                                                                                        var dinamic_interval  = 25;        //is_dinamicの時じゃないと使わん説

                                                                                        var algo_extend = algo.ToString(); //文字列化
                                                                                        switch (algo)
                                                                                        {
                                                                                        case AlgoEnum.OSMonly:
                                                                                            var osm_only = new OSM_Only();
                                                                                            osm_only.SetCommonWeight(common_weight);
                                                                                            osm = osm_only;
                                                                                            break;

                                                                                        case AlgoEnum.AAT:
                                                                                            var osm_aat = new AAT_OSM();  //AATをセット
                                                                                            osm_aat.SetTargetH(target_h); //AATモデルに意見形成目標率をセット
                                                                                            osm = osm_aat;                //オーバーライドされる関数が出てくることに注意
                                                                                            break;

                                                                                        case AlgoEnum.CCAAT:
                                                                                            var osm_ccaat = new CCAAT_OSM(); //CCAATをセット
                                                                                            osm_ccaat.SetTargetH(target_h);  //CCAATモデルに意見形成目標率をセット
                                                                                            osm = osm_ccaat;                 //オーバーライドされる関数が出てくることに注意
                                                                                            break;
                                                                                        }

                                                                                        var update_step_rand_tmp = new ExtendRandom(update_step_seed);     //ランダム生成器
                                                                                        var subject_mgr_dic      = new Dictionary <int, SubjectManager>(); //subjectManagerディクショナリを生成
                                                                                        if (is_diynamic)                                                   //ダイナミックなら
                                                                                        {
                                                                                            for (int i = 0; i < 100; i++)
                                                                                            {
                                                                                                subject_mgr_dic.Add(i * dinamic_interval, new SubjectManagerGenerator().Generate(subject_test, env_dist_weight, i % op_dim_size, sensor_weight, env_dist_mode));//実験タイトル,
                                                                                            }
                                                                                        }
                                                                                        else //subject_testはsubjectNameと意見数を含むOpinion.envDistweightは環境からセンサへの重み.sensor_weightは
                                                                                        {
                                                                                            subject_mgr_dic.Add(0, new SubjectManagerGenerator().Generate(subject_test, env_dist_weight, 0, sensor_weight, env_dist_mode, 1, mal_env_dist_weight)); //正解次元,誤り次元を0,1とするsubjectManagerを生成
                                                                                        }
                                                                                        osm.SetSubjectManagerDic(subject_mgr_dic);                   //サブジェクトマネージャーディクショナリをセット(OSM環境などが含まれる)
                                                                                        osm.SetRand(update_step_rand_tmp);                           // Update_Step_Randにシードを設定
                                                                                        osm.SetAgentNetwork(agent_network, opinion_share_num);       //agenet_network(リンク,ノード,グラフ,センサ情報など)をセット.各エージェントについて重み候補集合を作成しリンクに初期重みをセット
                                                                                        osm.SetInitWeightsMode(mode: CalcWeightMode.FavorMyOpinion); //ウェイトinitの方法をセット
                                                                                        osm.SetOpinionIntroInterval(op_intro_interval);              //環境を観測する間隔.オピニオンインターバルを追加(単純に上で指定)
                                                                                        osm.SetOpinionIntroRate(op_intro_rate);                      //実際に環境を観測するセンサの割合.オピニオンイントロ率をセット(単純に上で指定)
                                                                                        osm.SimpleRecordFlag = true;
                                                                                        osm.SetBeliefUpdater(this.MyBeliefUpdater);                  //信念値更新モデルセットするだけ(MainFormで指定)

                                                                                        osm.SetAddShareCommunityOnly(add_share_only_community);

                                                                                        if (bad_sensor_mode && select_graph == GraphEnum.LFR)
                                                                                        {
                                                                                            osm.SetCommunityList(communityList);
                                                                                        }

                                                                                        pb.Tag = $"{select_graph.ToString()} {size.ToString()} {algo_extend.ToString()} {op_dim_size} {seed}"; //グラフの形状,ノード数,アルゴリズム,意見数,シードを文字列に

                                                                                        /***************実験***************/
                                                                                        osm.Execute(round, this.Steps, bad_sensor_mode, opinion_share_num, pb); //ここで1セット分の実験を行う.ラウンド数,ステップ数,プログレスバー
                                                                                        /**********************************/

                                                                                        string sensor_size_mode = "";
                                                                                        if (this.SensorSizeFixMode)
                                                                                        {
                                                                                            sensor_size_mode = "fix" + this.SensorSize.ToString();
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            sensor_size_mode = "rate" + Math.Round(sensor_size_rate, 3).ToString(); //math.Roundは少数第三位までに丸め込む
                                                                                        }

                                                                                        string sensor_weight_mode = $"{this.MyBeliefUpdater.SensorWeightMode}";                     //センサウェイトモードの名前を文字列に

                                                                                        var exp_setting = new ExperimentSetting();                                                  //実験設定
                                                                                        exp_setting.Algorithm         = algo.ToString();                                            //アルゴリズム名を文字列に
                                                                                        exp_setting.AwaRateWindowSize = awa_rate_window;
                                                                                        exp_setting.BeliefUpdater     = this.MyBeliefUpdater.MyBeliefUpdateFunctionMode.ToString(); //センサの信念値更新式を文字列に
                                                                                        exp_setting.CommonCuriocity   = this.CommonCuriocity;                                       //MainFormでセット
                                                                                        exp_setting.Dim                 = op_dim_size;                                              //意見数
                                                                                        exp_setting.EnvDistWeight       = env_dist_weight;
                                                                                        exp_setting.EnvDistMode         = env_dist_mode.ToString();
                                                                                        exp_setting.GraphType           = select_graph.ToString(); //グラフの名前
                                                                                        exp_setting.IsDynamic           = is_diynamic;
                                                                                        exp_setting.InfoWeightRate      = info_weight_rate;
                                                                                        exp_setting.NetworkSize         = size; //ノード数
                                                                                        exp_setting.OpinionIntroInteval = op_intro_interval;
                                                                                        exp_setting.OpinionIntroRate    = op_intro_rate;
                                                                                        exp_setting.Round               = round;
                                                                                        exp_setting.SampleSize          = sample_size;
                                                                                        exp_setting.SensorRate          = sensor_weight;
                                                                                        exp_setting.SensorSize          = sensor_gene.SensorSize;
                                                                                        exp_setting.SensorSizeMode      = this.SensorSizeFixMode.ToString();
                                                                                        exp_setting.SensorWeightMode    = sensor_weight_mode;
                                                                                        exp_setting.Step                = this.Steps;
                                                                                        exp_setting.TargetAwareness     = target_h;

                                                                                        var json_exp_each_setting = JsonConvert.SerializeObject(exp_setting, Formatting.Indented);

                                                                                        var additional_setting = "";
                                                                                        if (bad_sensor_mode)
                                                                                        {
                                                                                            additional_setting += "BM";
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            additional_setting += "NM";
                                                                                        }

                                                                                        var output_path = Properties.Settings.Default.OutputLogPath //ファイル出力パス
                                                                                                          + $"/{save_folder}/data/"
                                                                                                          + $"S{seed}_"
                                                                                                          + select_graph.ToString()
                                                                                                          + $"_{additional_setting}"
                                                                                                          + $"_{size.ToString()}"
                                                                                                          + $"_SN{opinion_share_num.ToString()}"
                                                                                                          + $"_{algo_extend.ToString()}"
                                                                                                          //+ $"_{op_dim_size}"
                                                                                                          //+ $"_{is_diynamic}"
                                                                                                          + $"_{target_h}"
                                                                                                          + $"_{env_dist_mode.ToString()}"
                                                                                                          //+ $"_{env_dist_weight}"
                                                                                                          + $"_{info_weight_rate}"
                                                                                                          + $"_{round}"
                                                                                                          + $"_{sensor_size_mode}"
                                                                                                          //+ $"_{mal_sensor_size_rate}"
                                                                                                          + $"_{common_weight.ToString()}";
                                                                                        //+ $"_{est_func.ToString()}"
                                                                                        //+ $"_";

                                                                                        //Output.OutputRounds(output_pass, osm.MyRecordRounds, seed.ToString());
                                                                                        Output.OutputRounds(output_path, osm.MyRecordRounds, json_exp_each_setting, seed.ToString());

                                                                                        string   python_path = "C:/Users/nekko/Documents/TakadamaLab/OSM/OSM2020/OSM2020/bin/Debug/";
                                                                                        string[] path_split  = output_path.Split('/'); //[1]にOutputLog,[2]に条件付きファイル名
                                                                                        python_path += path_split[1] + '/' + osm_setting.python_name + " " + path_split[2];
                                                                                        this.PythonCsvPath.Add(python_path);

                                                                                        pb.Next();
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Output.GraphicalizeWithPython(this.PythonCsvPath);
        }
Пример #4
0
        public void Run(int start_seed, int final_seed)
        {
            string save_folder = this.LogFolder;
            var    graphs      = this.MyGraphs;
            var    algos       = this.MyAlgos;
            var    dims        = this.MyDims;

            //int op_dim_size = this.DimSize;
            double sensor_weight = this.SensorWeight;


            int max = 0;

            foreach (var size in this.NetworkSizes)
            {
                foreach (var select_graph in graphs)
                {
                    for (int seed = start_seed; seed <= final_seed; seed++)
                    {
                        foreach (var algo in algos)
                        {
                            foreach (var dim in dims)
                            {
                                foreach (var target_h in this.TargetHs)
                                {
                                    foreach (var is_dynamic in this.IsDynamics)
                                    {
                                        foreach (var env_dist_mode in this.EnvDisModes)
                                        {
                                            foreach (var env_dist_weight in this.EnvDistWeights)
                                            {
                                                foreach (var mal_env_dist_weight in this.MaliciousEnvDistWeights)
                                                {
                                                    foreach (var info_weight_rate in this.InfoWeightRates)
                                                    {
                                                        foreach (var round in this.Rounds)
                                                        {
                                                            foreach (var sensor_size_rate in this.SensorSizeRates)
                                                            {
                                                                foreach (var mal_sensor_size_rate in this.MaliciousSensorSizeRates)
                                                                {
                                                                    foreach (var common_weight in this.CommonWeights)
                                                                    {
                                                                        max++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var pb = new ExtendProgressBar(max);

            foreach (var size in this.NetworkSizes)
            {
                GraphGeneratorBase graph_generator = new Null_GraphGenerator();

                foreach (var select_graph in graphs)
                {
                    foreach (var op_dim_size in dims)
                    {
                        foreach (var is_diynamic in this.IsDynamics)
                        {
                            foreach (var env_dist_mode in this.EnvDisModes)
                            {
                                foreach (var env_dist_weight in this.EnvDistWeights)
                                {
                                    foreach (var mal_env_dist_weight in this.MaliciousEnvDistWeights)
                                    {
                                        foreach (var info_weight_rate in this.InfoWeightRates)
                                        {
                                            foreach (var round in this.Rounds)
                                            {
                                                foreach (var sensor_size_rate in this.SensorSizeRates)
                                                {
                                                    foreach (var mal_sensor_size_rate in this.MaliciousSensorSizeRates)
                                                    {
                                                        foreach (var common_weight in this.CommonWeights)
                                                        {
                                                            for (int seed = start_seed; seed <= final_seed; seed++)
                                                            {
                                                                switch (select_graph)
                                                                {
                                                                case GraphEnum.WS:
                                                                    graph_generator = new WS_GraphGenerator().SetNodeSize(size).SetNearestNeighbors(6).SetRewireP(0.01);
                                                                    //graph_generator = new WS_GraphGenerator().SetNodeSize(size).SetNearestNeighbors(15).SetRewireP(0.1);
                                                                    break;

                                                                case GraphEnum.BA:
                                                                    graph_generator = new BA_GraphGenerator().SetNodeSize(size).SetAttachEdges(2);
                                                                    //graph_generator = new BA_GraphGenerator().SetNodeSize(size).SetAttachEdges(10);
                                                                    break;

                                                                case GraphEnum.Hexagonal:
                                                                    graph_generator = new Hexagonal_GraphGenerator().SetNodeSize(size);
                                                                    break;

                                                                case GraphEnum.Grid2D:
                                                                    graph_generator = new Grid2D_GraphGenerator().SetNodeSize(size);
                                                                    break;

                                                                case GraphEnum.Triangular:
                                                                    graph_generator = new Triangular_GraphGenerator().SetNodeSize(size);
                                                                    break;

                                                                default:
                                                                    new Exception();
                                                                    return;
                                                                }
                                                                var graph  = new RawGraph();
                                                                var layout = new Layout();

                                                                lock (lock_object)
                                                                {
                                                                    graph  = graph_generator.Generate(seed, pb);
                                                                    layout = new Circular_LayoutGenerator(graph).Generate(pb);
                                                                }

                                                                var init_belief_gene = new InitBeliefGenerator()
                                                                                       .SetInitBeliefMode(mode: InitBeliefMode.NormalNarrow);

                                                                var subject_test = new OpinionSubject(this.SubjectName, op_dim_size);

                                                                var sample_agent_test = new SampleAgent()
                                                                                        .SetInitBeliefGene(init_belief_gene)
                                                                                        .SetThreshold(this.OpinionThreshold)
                                                                                        .SetSubject(subject_test)
                                                                                        .SetInitOpinion(Vector <double> .Build.Dense(op_dim_size, 0.0));

                                                                var sensor_gene = new SensorGenerator();
                                                                if (this.SensorSizeFixMode)
                                                                {
                                                                    //you will add malicious sensor size
                                                                    sensor_gene.SetSensorSize(this.SensorSize);
                                                                }
                                                                else
                                                                {
                                                                    int sensor_size     = (int)(sensor_size_rate * graph.Nodes.Count);
                                                                    int mal_sensor_size = (int)(mal_sensor_size_rate * graph.Nodes.Count);
                                                                    sensor_gene.SetSensorSize(sensor_size, mal_sensor_size);
                                                                }

                                                                int agent_gene_seed = seed;
                                                                var agent_gene_rand = new ExtendRandom(agent_gene_seed);


                                                                var agent_network = new AgentNetwork()
                                                                                    .SetRand(agent_gene_rand)
                                                                                    .GenerateNetworkFrame(graph)
                                                                                    .ApplySampleAgent(sample_agent_test, mode: SampleAgentSetMode.RemainSet)
                                                                                    .GenerateSensor(sensor_gene)
                                                                                    .SetLayout(layout);

                                                                int update_step_seed = seed;

                                                                foreach (var algo in algos)
                                                                {
                                                                    OSMBase osm = new OSM_Only();
                                                                    foreach (var target_h in this.TargetHs)
                                                                    {
                                                                        var sample_size       = 10;
                                                                        var awa_rate_window   = 100;
                                                                        var info_value_window = 50;
                                                                        //var info_value_window = 100;
                                                                        var op_intro_interval = 10;
                                                                        var op_intro_rate     = 0.1;
                                                                        var dinamic_interval  = 25;

                                                                        var algo_extend = algo.ToString();
                                                                        switch (algo)
                                                                        {
                                                                        case AlgoEnum.OSMonly:
                                                                            var osm_only = new OSM_Only();
                                                                            osm_only.SetCommonWeight(common_weight);
                                                                            osm = osm_only;
                                                                            break;

                                                                        case AlgoEnum.AAT:
                                                                            var osm_aat = new AAT_OSM();
                                                                            osm_aat.SetTargetH(target_h);
                                                                            osm = osm_aat;
                                                                            break;

                                                                        case AlgoEnum.AATG:
                                                                            var osm_aatg = new AATG_OSM();
                                                                            osm_aatg.SetTargetH(target_h);
                                                                            osm = osm_aatg;
                                                                            break;

                                                                        case AlgoEnum.AATfix:
                                                                            var osm_aatfix = new AATfix_OSM();
                                                                            osm_aatfix.SetTargetH(target_h);
                                                                            osm = osm_aatfix;
                                                                            break;

                                                                        case AlgoEnum.IWTori:
                                                                            var osm_iwtori = new IWTori_OSM();
                                                                            osm_iwtori.SetCommonCuriocity(this.CommonCuriocity);
                                                                            osm_iwtori.SetTargetH(target_h);
                                                                            osm = osm_iwtori;
                                                                            break;

                                                                        case AlgoEnum.AATparticle:
                                                                            var osm_aatpar = new AATparticle_OSM();
                                                                            osm_aatpar.SetSampleSize(sample_size);
                                                                            osm_aatpar.SetTargetH(target_h);
                                                                            osm = osm_aatpar;
                                                                            break;

                                                                        case AlgoEnum.AATwindow:
                                                                            var osm_window = new AATwindow_OSM();
                                                                            osm_window.SetTargetH(target_h);
                                                                            osm_window.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm = osm_window;
                                                                            break;

                                                                        case AlgoEnum.AATwindowparticle:
                                                                            var osm_window_particle = new AATwindow_particle_OSM();
                                                                            osm_window_particle.SetTargetH(target_h);
                                                                            osm_window_particle.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_window_particle.SetSampleSize(sample_size);
                                                                            osm = osm_window_particle;
                                                                            break;

                                                                        case AlgoEnum.AATfunction:
                                                                            var osm_function = new AATfunction_OSM();
                                                                            osm_function.SetTargetH(target_h);
                                                                            osm_function.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm = osm_function;
                                                                            break;

                                                                        case AlgoEnum.AATfunctioniwt:
                                                                            var osm_function_iwt = new AATfunction_iwt_OSM();
                                                                            osm_function_iwt.SetTargetH(target_h);
                                                                            osm_function_iwt.SetCommonCuriocity(this.CommonCuriocity);
                                                                            osm_function_iwt.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm = osm_function_iwt;
                                                                            break;

                                                                        case AlgoEnum.SWT:
                                                                            var osm_aat_info = new SWT_OSM();
                                                                            osm_aat_info.SetTargetH(target_h);
                                                                            osm_aat_info.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_aat_info.SetLinkInfoValueWindowSize(info_value_window);
                                                                            osm_aat_info.SetInfoWeightRate(info_weight_rate);
                                                                            osm = osm_aat_info;
                                                                            break;

                                                                        case AlgoEnum.SWTstep:
                                                                            var osm_aat_info_step = new SWT_step_OSM();
                                                                            osm_aat_info_step.SetTargetH(target_h);
                                                                            osm_aat_info_step.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_aat_info_step.SetLinkInfoValueWindowSize(info_value_window);
                                                                            osm_aat_info_step.SetInfoWeightRate(info_weight_rate);
                                                                            osm_aat_info_step.SetInfoLearningRate(0.2);
                                                                            osm = osm_aat_info_step;
                                                                            break;

                                                                        case AlgoEnum.GDWTsigW:
                                                                            var osm_gdwt_sigw = new GDWT_OSM();
                                                                            osm_gdwt_sigw.SetTargetH(target_h);
                                                                            osm_gdwt_sigw.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_gdwt_sigw.SetEstimateFunction(new Sigmoid_weight_EstFunc(0.01, 0.0, 5));
                                                                            //osm_gdwt_sigw.SetEstimateFunction(new Sigmoid_weight_EstFunc(1.0, 0.0, 3));
                                                                            osm = osm_gdwt_sigw;
                                                                            break;

                                                                        case AlgoEnum.GDWTsigH:
                                                                            var osm_gdwt_sigh = new GDWT_OSM();
                                                                            osm_gdwt_sigh.SetTargetH(target_h);
                                                                            osm_gdwt_sigh.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_gdwt_sigh.SetEstimateFunction(new Sigmoid_awa_EstFunc(0.05, 0, 20));
                                                                            osm = osm_gdwt_sigh;
                                                                            break;

                                                                        case AlgoEnum.GDWTpowerH:
                                                                            var osm_gdwt_powh = new GDWT_OSM();
                                                                            osm_gdwt_powh.SetTargetH(target_h);
                                                                            osm_gdwt_powh.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_gdwt_powh.SetEstimateFunction(new Power_awa_EstFunc(3.0, 0));
                                                                            osm = osm_gdwt_powh;
                                                                            break;

                                                                        case AlgoEnum.GDWTpowerW:
                                                                            var osm_gdwt_poww = new GDWT_OSM();
                                                                            osm_gdwt_poww.SetTargetH(target_h);
                                                                            osm_gdwt_poww.SetAwaRateWindowSize(awa_rate_window);
                                                                            osm_gdwt_poww.SetEstimateFunction(new Power_weight_EstFunc(3.0, 0));
                                                                            osm = osm_gdwt_poww;
                                                                            break;

                                                                        default:
                                                                            break;
                                                                        }

                                                                        var update_step_rand_tmp = new ExtendRandom(update_step_seed);
                                                                        var subject_mgr_dic      = new Dictionary <int, SubjectManager>();
                                                                        if (is_diynamic)
                                                                        {
                                                                            for (int i = 0; i < 100; i++)
                                                                            {
                                                                                subject_mgr_dic.Add(i * dinamic_interval, new SubjectManagerGenerator().Generate(subject_test, env_dist_weight, i % op_dim_size, sensor_weight, env_dist_mode));
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            subject_mgr_dic.Add(0, new SubjectManagerGenerator().Generate(subject_test, env_dist_weight, 0, sensor_weight, env_dist_mode, 1, mal_env_dist_weight));
                                                                        }
                                                                        osm.SetSubjectManagerDic(subject_mgr_dic);
                                                                        osm.SetRand(update_step_rand_tmp);
                                                                        osm.SetAgentNetwork(agent_network);
                                                                        osm.SetInitWeightsMode(mode: CalcWeightMode.FavorMyOpinion);
                                                                        osm.SetOpinionIntroInterval(op_intro_interval);
                                                                        osm.SetOpinionIntroRate(op_intro_rate);
                                                                        osm.SimpleRecordFlag = true;
                                                                        osm.SetBeliefUpdater(this.MyBeliefUpdater);

                                                                        pb.Tag = $"{select_graph.ToString()} {size.ToString()} {algo_extend.ToString()} {op_dim_size} {seed}";
                                                                        osm.UpdateRounds(round, this.Steps, pb);

                                                                        string sensor_size_mode = "";
                                                                        if (this.SensorSizeFixMode)
                                                                        {
                                                                            sensor_size_mode = "fix" + this.SensorSize.ToString();
                                                                        }
                                                                        else
                                                                        {
                                                                            sensor_size_mode = "rate" + Math.Round(sensor_size_rate, 3).ToString();
                                                                        }

                                                                        string sensor_weight_mode = $"{this.MyBeliefUpdater.SensorWeightMode}";

                                                                        var exp_setting = new ExperimentSetting();
                                                                        exp_setting.Algorithm         = algo.ToString();
                                                                        exp_setting.AwaRateWindowSize = awa_rate_window;
                                                                        exp_setting.BeliefUpdater     = this.MyBeliefUpdater.MyBeliefUpdateFunctionMode.ToString();
                                                                        exp_setting.CommonCuriocity   = this.CommonCuriocity;
                                                                        exp_setting.Dim                 = op_dim_size;
                                                                        exp_setting.EnvDistWeight       = env_dist_weight;
                                                                        exp_setting.EnvDistMode         = env_dist_mode.ToString();
                                                                        exp_setting.GraphType           = select_graph.ToString();
                                                                        exp_setting.IsDynamic           = is_diynamic;
                                                                        exp_setting.InfoWeightRate      = info_weight_rate;
                                                                        exp_setting.NetworkSize         = size;
                                                                        exp_setting.OpinionIntroInteval = op_intro_interval;
                                                                        exp_setting.OpinionIntroRate    = op_intro_rate;
                                                                        exp_setting.Round               = round;
                                                                        exp_setting.SampleSize          = sample_size;
                                                                        exp_setting.SensorRate          = sensor_weight;
                                                                        exp_setting.SensorSize          = sensor_gene.SensorSize;
                                                                        exp_setting.SensorSizeMode      = this.SensorSizeFixMode.ToString();
                                                                        exp_setting.SensorWeightMode    = sensor_weight_mode;
                                                                        exp_setting.Step                = this.Steps;
                                                                        exp_setting.TargetAwareness     = target_h;

                                                                        var json_exp_each_setting = JsonConvert.SerializeObject(exp_setting, Formatting.Indented);

                                                                        var output_pass = Properties.Settings.Default.OutputLogPath
                                                                                          + $"/{save_folder}/data/"
                                                                                          + select_graph.ToString()
                                                                                          + $"_{size.ToString()}"
                                                                                          + $"_{algo_extend.ToString()}"
                                                                                          + $"_{op_dim_size}"
                                                                                          + $"_{is_diynamic}"
                                                                                          + $"_{target_h}"
                                                                                          + $"_{env_dist_mode.ToString()}"
                                                                                          + $"_{env_dist_weight}"
                                                                                          + $"_{info_weight_rate}"
                                                                                          + $"_{round}"
                                                                                          + $"_{sensor_size_mode}"
                                                                                          + $"_{mal_sensor_size_rate}"
                                                                                          + $"_{common_weight.ToString()}"
                                                                                          //+ $"_{est_func.ToString()}"
                                                                                          + $"_";

                                                                        //Output.OutputRounds(output_pass, osm.MyRecordRounds, seed.ToString());
                                                                        Output.OutputRounds(output_pass, osm.MyRecordRounds, json_exp_each_setting, seed.ToString());
                                                                        pb.Next();
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
        void Test()
        {
            int      agent_size            = 200;
            int      dim                   = 3;
            int      correct_dim           = 0;
            int      malicious_dim         = 1;
            AlgoEnum algo                  = AlgoEnum.SWTkai;
            double   targeth               = 0.80;
            double   common_weight         = 0.5;
            double   common_curiocity      = 0.5;
            double   sensor_rate           = 0.8;
            double   dist_weight           = 0.5;
            double   malicious_dist_weight = 0.8;
            int      sensor_size           = (int)(0.05 * agent_size);
            //int malicious_sensor_size = (int)(0.04 * agent_size);
            int malicious_sensor_size = 0;
            var op_form_threshold     = 0.8;
            int sample_size           = 10;
            int change_round          = 0;

            var belief_updater = new BeliefUpdater().SetSensorWeightMode(SensorWeightEnum.DependSensorRate);

            GraphGeneratorBase graph_generator;

            //graph_generator = new PC_GraphGenerator().SetNodeSize(500).SetRandomEdges(3).SetAddTriangleP(0.1);
            //graph_generator = new WS_GraphGenerator().SetNodeSize(agent_size).SetNearestNeighbors(6).SetRewireP(0.01);
            //graph_generator = new BA_GraphGenerator().SetNodeSize(agent_size).SetAttachEdges(2);
            graph_generator = new Grid2D_GraphGenerator().SetNodeSize(agent_size);
            //graph_generator = new ER_GraphGenerator().SetNodeSize(agent_size).SetEdgeCreateP(0.01);

            var pb     = new ExtendProgressBar(100);
            var graph  = graph_generator.Generate(0, pb);
            var layout = new KamadaKawai_LayoutGenerator(graph).Generate(pb);
            //var layout = new Circular_LayoutGenerator(graph).Generate();

            var init_belief_gene = new InitBeliefGenerator()
                                   .SetInitBeliefMode(mode: InitBeliefMode.NormalNarrow);
            //.SetInitBeliefMode(mode: InitBeliefMode.NormalWide);

            var subject_test = new OpinionSubject("test", dim);

            var sample_agent_test = new SampleAgent()
                                    .SetInitBeliefGene(init_belief_gene)
                                    .SetThreshold(op_form_threshold)
                                    .SetSubject(subject_test)
                                    .SetInitOpinion(Vector <double> .Build.Dense(dim, 0.0));

            var sensor_gene = new SensorGenerator()
                              //                .SetSensorSize((int)5);
                              .SetSensorSize(sensor_size, malicious_sensor_size);

            int agent_gene_seed = 4;
            var agent_gene_rand = new ExtendRandom(agent_gene_seed);


            var agent_network = new AgentNetwork()
                                .SetRand(agent_gene_rand)
                                .GenerateNetworkFrame(graph)
                                //.ApplySampleAgent(sample_agent_1, mode: SampleAgentSetMode.RandomSetRate, random_set_rate: 0.5)
                                //.ApplySampleAgent(sample_agent_2, mode: SampleAgentSetMode.RemainSet)
                                .ApplySampleAgent(sample_agent_test, mode: SampleAgentSetMode.RemainSet)
                                .GenerateSensor(sensor_gene)
                                .SetLayout(layout);

            int update_step_seed = 0;
            var update_step_rand = new ExtendRandom(update_step_seed);

            OSMBase osm = new OSM_Only();

            switch (algo)
            {
            case AlgoEnum.AAT:
                var osm_aat = new AAT_OSM();
                osm_aat.SetTargetH(targeth);
                osm = osm_aat;
                break;

            case AlgoEnum.AATG:
                var osm_aatg = new AATG_OSM();
                osm_aatg.SetTargetH(targeth);
                osm = osm_aatg;
                break;

            case AlgoEnum.AATfix:
                var osm_aatfix = new AATfix_OSM();
                osm_aatfix.SetTargetH(targeth);
                osm = osm_aatfix;
                break;

            case AlgoEnum.OSMonly:
                var osm_only = new OSM_Only();
                osm_only.SetCommonWeight(common_weight);
                osm = osm_only;
                break;

            case AlgoEnum.IWTori:
                var osm_iwtori = new IWTori_OSM();
                osm_iwtori.SetCommonCuriocity(common_curiocity);
                osm_iwtori.SetTargetH(targeth);
                osm = osm_iwtori;
                break;

            case AlgoEnum.AATparticle:
                var osm_aatpar = new AATparticle_OSM();
                osm_aatpar.SetSampleSize(sample_size);
                osm_aatpar.SetTargetH(targeth);
                osm = osm_aatpar;
                break;

            case AlgoEnum.AATwindow:
                var osm_window = new AATwindow_OSM();
                osm_window.SetTargetH(targeth);
                osm_window.SetAwaRateWindowSize(20);
                osm = osm_window;
                break;

            case AlgoEnum.AATwindowparticle:
                var osm_window_particle = new AATwindow_particle_OSM();
                osm_window_particle.SetTargetH(targeth);
                osm_window_particle.SetAwaRateWindowSize(20);
                osm_window_particle.SetSampleSize(10);
                osm = osm_window_particle;
                break;

            case AlgoEnum.AATfunction:
                var osm_function = new AATfunction_OSM();
                osm_function.SetTargetH(targeth);
                osm_function.SetAwaRateWindowSize(100);
                osm = osm_function;
                break;

            case AlgoEnum.AATfunctioniwt:
                var osm_function_iwt = new AATfunction_iwt_OSM();
                osm_function_iwt.SetCommonCuriocity(common_curiocity);
                osm_function_iwt.SetTargetH(targeth);
                osm_function_iwt.SetAwaRateWindowSize(100);
                osm = osm_function_iwt;
                break;

            case AlgoEnum.SWT:
                var osm_aat_info = new SWT_OSM();
                osm_aat_info.SetTargetH(targeth);
                osm_aat_info.SetAwaRateWindowSize(50);
                osm_aat_info.SetLinkInfoValueWindowSize(50);
                osm_aat_info.SetInfoWeightRate(1.0);
                osm = osm_aat_info;
                break;

            case AlgoEnum.SWTstep:
                var osm_aat_info_step = new SWT_step_OSM();
                osm_aat_info_step.SetTargetH(targeth);
                osm_aat_info_step.SetAwaRateWindowSize(100);
                osm_aat_info_step.SetLinkInfoValueWindowSize(100);
                osm_aat_info_step.SetInfoWeightRate(1.0);
                osm_aat_info_step.SetInfoLearningRate(0.2);
                osm = osm_aat_info_step;
                break;

            case AlgoEnum.SWTkai:
                var osm_swtkai = new SWTkai_OSM();
                osm_swtkai.SetCommonWeight(0.5);
                osm_swtkai.SetLinkInfoValueWindowSize(1000);
                osm = osm_swtkai;
                break;

            case AlgoEnum.GDWTsigW:
                var osm_gdwt_sigw = new GDWT_OSM();
                osm_gdwt_sigw.SetTargetH(targeth);
                osm_gdwt_sigw.SetAwaRateWindowSize(100);
                osm_gdwt_sigw.SetEstimateFunction(new Sigmoid_weight_EstFunc(1.0, 0, 2));
                osm = osm_gdwt_sigw;
                break;

            case AlgoEnum.GDWTsigH:
                var osm_gdwt_sigh = new GDWT_OSM();
                osm_gdwt_sigh.SetTargetH(targeth);
                osm_gdwt_sigh.SetAwaRateWindowSize(100);
                osm_gdwt_sigh.SetEstimateFunction(new Sigmoid_awa_EstFunc(1.0, 0, 3));
                osm = osm_gdwt_sigh;
                break;

            case AlgoEnum.GDWTpowerH:
                var osm_gdwt_powh = new GDWT_OSM();
                osm_gdwt_powh.SetTargetH(targeth);
                osm_gdwt_powh.SetAwaRateWindowSize(100);
                osm_gdwt_powh.SetEstimateFunction(new Power_awa_EstFunc(3.0, 0));
                osm = osm_gdwt_powh;
                break;

            case AlgoEnum.GDWTpowerW:
                var osm_gdwt_poww = new GDWT_OSM();
                osm_gdwt_poww.SetTargetH(targeth);
                osm_gdwt_poww.SetAwaRateWindowSize(100);
                osm_gdwt_poww.SetEstimateFunction(new Power_weight_EstFunc(3.0, 0));
                osm = osm_gdwt_poww;
                break;

            case AlgoEnum.GDWTLinearW:
                var osm_gdwt_linearw = new GDWT_OSM();
                osm_gdwt_linearw.SetTargetH(targeth);
                osm_gdwt_linearw.SetAwaRateWindowSize(100);
                osm_gdwt_linearw.SetEstimateFunction(new Linear_weight_EstFunc(1.0, 1, 0));
                osm = osm_gdwt_linearw;
                break;

            default:
                break;
            }
            osm.SetRand(update_step_rand);
            osm.SetAgentNetwork(agent_network);
            var subject_mgr_dic = new Dictionary <int, SubjectManager>();

            subject_mgr_dic.Add(0, new SubjectManagerGenerator()
                                .Generate(subject_test, dist_weight, correct_dim, sensor_rate, EnvDistributionEnum.Exponential, malicious_dim, malicious_dist_weight));
            //for (int i = 0; i < 1; i++)
            //{
            //  subject_mgr_dic.Add(i * change_round, new SubjectManagerGenerator().Generate(subject_test, dist_weight, i % dim, sensor_rate, EnvDistributionEnum.Turara));
            //}
            osm.SetSubjectManagerDic(subject_mgr_dic);
            osm.SetInitWeightsMode(mode: CalcWeightMode.FavorMyOpinion);
            osm.SetOpinionIntroInterval(10);
            osm.SetOpinionIntroRate(0.1);
            //osm.SetSensorCommonWeight(0.70);
            osm.SetBeliefUpdater(belief_updater);

            this.MyOSM = osm;
            this.MyAnimationForm.RegistOSM(osm);
        }