示例#1
0
        public void SetCost(int c1, int s1, int c2, int s2)
        {
            if (s1 == 0 || s2 == 0)
            {
                return;
            }
            Shoot[] shoots = new Shoot[2];
            shoots[0] = kmeans.clusters[c1].members[s1 - 1];
            shoots[1] = kmeans.clusters[c2].members[s2 - 1];

            //DBA.CostPath DPtable = DBA.DpMatching(inputshoots[shoot1].acc, inputshoots[shoot2].acc);
            // DBA.CalcCost(a,b) aからみたbのコスト 長さはa
            double[][] costs  = new double[2][];
            Chart[]    charts = { chart1, chart2 };

            for (int c = 0; c < 2; ++c)
            {
                //shoot[0] and shoot[1] compare
                //cost[0]=(0,1) cost[1]=(1,0)
                costs[c] = DBA.CalcCost(shoots[c].acc, shoots[(c + 1) % 2].acc).cost;
                string legend4 = "cost";
                charts[c].Series.Add(legend4);
                charts[c].Series[legend4].ChartType = SeriesChartType.Line; // 折れ線グラフ

                for (int i = 0; i < shoots[c].acc.Length; i++)
                {
                    //shoot1における、shoot2からみたコスト
                    //shoot2における、shoot1からみたコスト
                    charts[c].Series[legend4].YAxisType = AxisType.Secondary;
                    charts[c].Series[legend4].Points.AddXY(shoots[c].time[i], costs[c][i]); //cost
                    Console.WriteLine("cost i :" + i + "  time:" + shoots[c].time[i]);
                }
            }
        }
        public void SetData(int cidA, int cidB)
        {
            //クラスタ同士のコスト 格納
            double[] cost = DBA.CalcCost(kmeans.clusters[cidA].centroid.DBAacc, kmeans.clusters[cidB].centroid.DBAacc).cost;
            double   dist = DBA.CalcCost(kmeans.clusters[cidA].centroid.DBAacc, kmeans.clusters[cidB].centroid.DBAacc).dist;

            chart1.Series.Clear();
            chart1.ChartAreas.Clear();

            // ChartにSeriesを追加します
            string legend1 = "X";
            string legend2 = "Y";
            string legend3 = "Z";
            string legend4 = "cost";

            chart1.Series.Add(legend1);
            chart1.Series.Add(legend2);
            chart1.Series.Add(legend3);
            chart1.Series.Add(legend4);

            chart1.Series[legend1].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend2].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend3].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend4].ChartType = SeriesChartType.Line; // 折れ線グラフ

            chart1.ChartAreas.Add(new ChartArea("Area1"));
            chart1.ChartAreas["Area1"].AxisX.Title = "time";
            chart1.ChartAreas["Area1"].AxisY.Title = "degree";


            {
                //クラスタ1(基準)
                Cluster ca = kmeans.clusters[cidA];
                //クラスタ2(比較したいもの)
                Cluster cb = kmeans.clusters[cidB];

                //クラスタ間の距離(総コスト)
                distNum.Text = dist.ToString();
                //double dt = 0.1;
                //double[] dtCost = new double[s.acc.Length];
                for (int i = 0; i < ca.centroid.DBAacc.Length - 1; i++)
                {
                    chart1.Series[legend1].Points.AddXY(ca.centroid.DBAtime[i], ca.centroid.DBAacc[i].x); //X
                    chart1.Series[legend2].Points.AddXY(ca.centroid.DBAtime[i], ca.centroid.DBAacc[i].y); //Y
                    chart1.Series[legend3].Points.AddXY(ca.centroid.DBAtime[i], ca.centroid.DBAacc[i].z); //Z
                }

                for (int i = 0; i < ca.centroid.DBAtime.Length; i++)
                {
                    //costの軸は2軸目(右側のY軸目盛り)で表示
                    chart1.Series[legend4].YAxisType = AxisType.Secondary;
                    //caを基準にグラフを表示
                    chart1.Series[legend4].Points.AddXY(ca.centroid.DBAtime[i], cost[i]); //cost
                }
            }
        }
 void CalcFinalDistance()
 {
     foreach (Cluster c in clusters)
     {
         foreach (Shoot s in c.members)
         {
             //コスト計算
             s.cost = DBA.CalcCost(s.acc, c.centroid.DBAacc).cost;
         }
     }
 }
        public void MakeCentroid()
        {
            Shoots members = new Shoots();

            members.AddRange(allShoots);
            int clsLen = members.Count / clusters.Length;

            //エルボー法評価のため
            double distSumAllShoots       = 0;
            string distSumAllShootsOutput = "distSumAllShoots.csv";

            foreach (Cluster c in clusters)
            {
                c.centroid.DBAacc = (Vec3[])members[0].acc.Clone();
                c.members.Add(members[0]);
                for (int i = 1; i < clsLen; ++i)
                {
                    double minDist = double.MaxValue;
                    Shoot  minS    = null;


                    foreach (Shoot s in members)
                    {
                        DBA.CostPath cp;
                        cp = DBA.DpMatching(s.acc, c.centroid.DBAacc);

                        //to callback in DBA-DBA distance
                        //myDBShoots.Add(new DBAShoot());

                        //最終コストが距離
                        double dist = cp.cost[cp.cost.Length - 1][cp.cost[cp.cost.Length - 1].Length - 1];
                        s.distFromCentroid = dist;

                        //エルボー法評価のため全ての距離の和を求める
                        distSumAllShoots += dist;
                        if (dist < minDist)
                        {
                            minDist = dist;
                            minS    = s;
                        }
                    }


                    members.Remove(minS);
                    c.members.Add(minS);
                }
            }
            File.WriteAllText(distSumAllShootsOutput, distSumAllShoots.ToString());
        }
        //public DBAShoots getShoot()
        //{
        //   return myDBShoots;
        //}

        public void UpdateCentroid()
        {
            //クラスタごとにDBAグラフを出力
            int clustersNumber = 1;

            foreach (Cluster c in clusters)
            {
                string centroidoutput = "centroidoutput" + clustersNumber + ".csv";
                c.centroid = DBA.CalcAverage(c.members, numberOfDbaUpdates);
                //centroid保存用
                string centroid = "";
                for (int i = 0; i < c.centroid.DBAacc.Length; ++i)
                {
                    //グラフの横軸は時刻ではなく何番目のデータか であることに注意
                    centroid += i + "," + c.centroid.DBAacc[i].x + "," + c.centroid.DBAacc[i].y + "," + c.centroid.DBAacc[i].z + "\r\n";
                }
                File.WriteAllText(centroidoutput, centroid);
                clustersNumber++;
            }
        }
        public void UpdateMember()
        {
            //クラスタの更新
            foreach (Cluster c in clusters)
            {
                c.prevMembers.Clear();
                c.prevMembers.AddRange(c.members);
                c.members.Clear();
            }
            foreach (Shoot s in allShoots)
            {
                double  minDist    = double.MaxValue;
                Cluster minCluster = null;
                foreach (Cluster c in clusters)
                {
                    double[][] cost = DBA.DpMatching(s.acc, c.centroid.DBAacc).cost;
                    double     dist = cost[cost.Length - 1][cost[cost.Length - 1].Length - 1];
                    if (dist < minDist)
                    {
                        minDist    = dist;
                        minCluster = c;
                    }
                }
                minCluster.members.Add(s);
            }

            //クラスタが更新されない場合
            clusteringFinished = true;
            foreach (Cluster c in clusters)
            {
                //積集合が一致しないとき => クラスタ更新は続ける
                IEnumerable <Shoot> intersect = c.members.Intersect(c.prevMembers);
                if (intersect.Count() != c.members.Count())
                {
                    clusteringFinished = false;
                }
            }
        }
        public void SetData(int cid, int sid)
        {
            chart1.Series.Clear();
            chart1.ChartAreas.Clear();

            // ChartにSeriesを追加します
            string legend1 = "X";
            string legend2 = "Y";
            string legend3 = "Z";
            string legend4 = "cost";

            chart1.Series.Add(legend1);
            chart1.Series.Add(legend2);
            chart1.Series.Add(legend3);
            chart1.Series.Add(legend4);

            chart1.Series[legend1].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend2].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend3].ChartType = SeriesChartType.Line; // 折れ線グラフ
            chart1.Series[legend4].ChartType = SeriesChartType.Line; // 折れ線グラフ

            chart1.ChartAreas.Add(new ChartArea("Area1"));
            chart1.ChartAreas["Area1"].AxisX.Title = "time";
            chart1.ChartAreas["Area1"].AxisY.Title = "degree";

            //クラスタ型から拾ってくる
            //Centroid のグラフ出力 sid == 0
            if (sid == 0)
            {
                Cluster c = kmeans.clusters[cid];
                for (int i = 0; i < c.centroid.DBAacc.Length; i++)
                {
                    chart1.Series[legend1].Points.AddY(c.centroid.DBAacc[i].x); //X
                    chart1.Series[legend2].Points.AddY(c.centroid.DBAacc[i].y); //Y
                    chart1.Series[legend3].Points.AddY(c.centroid.DBAacc[i].z); //Z
                }
                laShoot.Text    = "Centroid";
                laShootNum.Text = "";
                distNum.Text    = " ";
            }
            //各クラスタに属する射の入力データのグラフ出力 Centroidに距離が近い順(Sort済み)
            else
            {
                //クラスタ番号cidのクラスタ
                Cluster c = kmeans.clusters[cid];
                //クラスタ番号cidのメンバー 
                Shoot    s   = c.members[sid - 1];
                DBAShoot dba = c.centroid;
                laShoot.Text    = "Shoot";
                laShootNum.Text = s.shootnumber.ToString();
                distNum.Text    = DBA.CalcCost(s.acc, dba.DBAacc).dist.ToString();
                //double dt = 0.1;
                //double[] dtCost = new double[s.acc.Length];
                for (int i = 0; i < s.acc.Length - 1; i++)
                {
                    chart1.Series[legend1].Points.AddXY(s.time[i], s.acc[i].x); //X
                    chart1.Series[legend2].Points.AddXY(s.time[i], s.acc[i].y); //Y
                    chart1.Series[legend3].Points.AddXY(s.time[i], s.acc[i].z); //Z
                }

                //costのグラフも表示
                for (int i = 0; i < s.cost.Length; i++)
                {
                    ////chart1.Series[legend4].Points.AddY(s.path[i]); //cost
                    //dtCost[i] = (s.cost[i] - s.cost[i - 1]) / dt;
                    //dtCost[i] = s.cost[i];
                    //costの軸は2軸目(右側のY軸目盛り)で表示
                    double[] DBAcost = DBA.CalcCost(s.acc, dba.DBAacc).cost;

                    chart1.Series[legend4].YAxisType = AxisType.Secondary;
                    //chart1.Series[legend4].Points.AddXY(dba.DBAtime[i], s.cost[i]); //cost
                    chart1.Series[legend4].Points.AddXY(s.time[i], DBAcost[i]); //cost
                }
            }
        }