Exemplo n.º 1
0
        /// <summary>
        /// KKT行列を作って解く。
        /// </summary>
        private void SolveKKT()
        {
            var pList  = mDGEditor.PointList();
            var eList  = mDGEditor.EdgeList();
            var earthP = mDGEditor.EarthedPoint();

            if (pList.Count == 0 || eList.Count == 0)
            {
                return;
            }

            var A = CalcA();

            AddLog(string.Format("A: {0}", A.ToString()));

            var C = CalcC();

            // A^T
            var AT = A.Transpose();

            // K = A^T C Aを作る。
            var K = new WWMatrix(pList.Count - 1, pList.Count - 1);

            {
                var CA = WWMatrix.Mul(C, A);
                K = WWMatrix.Mul(AT, CA);
            }
            AddLog(string.Format("K: {0}", K.ToString()));

            var b = new WWMatrix(eList.Count, 1);

            {   // 電圧源b
                for (int i = 0; i < eList.Count; ++i)
                {
                    var e = eList[i];
                    b.Set(i, 0, e.B);
                }
            }
            AddLog(string.Format("b: {0}", b.ToString()));

            var f = new WWMatrix(pList.Count - 1, 1);

            {   // fを作る。
                int nP = 0;
                for (int i = 0; i < pList.Count; ++i)
                {
                    var point = pList[i];

                    if (point == earthP)
                    {
                        // アースされている点は除外。
                        continue;
                    }
                    f.Set(nP, 0, point.F);
                    ++nP;
                }
            }
            AddLog(string.Format("f: {0}", f.ToString()));

            WWMatrix KKT;

            {   // KKT行列
                var Cinv    = CalcCinv();
                var Cinv_A  = WWMatrix.JoinH(Cinv, A);
                var AT_zero = WWMatrix.JoinH(AT, new WWMatrix(A.Column, A.Column));
                KKT = WWMatrix.JoinV(Cinv_A, AT_zero);
            }
            AddLog(string.Format("KKT: {0}", KKT.ToString()));

            WWMatrix bf;

            {   // bとfを縦に連結した縦ベクトル。
                bf = WWMatrix.JoinV(b, f);
            }
            AddLog(string.Format("bf: {0}", bf.ToString()));

            var bfA = bf.ToArray();

            // KKT u = bfを解いて uを求める。
            var uA = WWLinearEquation.SolveKu_eq_f(KKT, bfA);
            var u  = new WWMatrix(uA.Length, 1, uA);

            AddLog(string.Format("u: {0}", u.ToString()));
        }
Exemplo n.º 2
0
        private static void TestGeneral()
        {
            TextWriter tout = new NullWriter();


            LPProblem p = new LPProblem();

            tout.WriteLine(p.AddRows(4));
            tout.WriteLine(p.AddCols(5));


            p.Name = "yoyoyo";
            tout.WriteLine(p.Name);
            p.ObjectiveName = "monobj";
            tout.WriteLine(p.ObjectiveName);
            p.ObjectiveDirection = OptimisationDirection.MINIMISE;
            tout.WriteLine(p.ObjectiveDirection);
            p.ObjectiveDirection = OptimisationDirection.MAXIMISE;
            tout.WriteLine(p.ObjectiveDirection);


            p.SetRowName(1, "ert");
            tout.WriteLine(p.GetRowName(1));
            tout.WriteLine(p.GetRowName(2));
            p.SetRowName(1, null);
            tout.WriteLine(p.GetRowName(1));

            p.SetColName(1, "colk");
            tout.WriteLine(p.GetColName(1));
            p.SetColName(1, null);
            tout.WriteLine(p.GetColName(1));
            tout.WriteLine(p.GetColName(2));

            p.SetRowBounds(1, BOUNDSTYPE.Upper, 4, 6);
            p.SetColBounds(2, BOUNDSTYPE.Upper, 3, 7);
            p.SetMatRow(1, new int[] { 0, 1, 2, 4 }, new double[] { 0, 2.2, 3.3, 4.4 });
            int[]    ind;
            double[] val;
            p.GetMatRow(1, out ind, out val);
            tout.WriteLine(ind.Length + " " + ind[1]);
            tout.WriteLine(val.Length + " " + val[1]);

            p.SetObjCoef(2, 3.6);
            tout.WriteLine(p.GetObjCoef(2));

            tout.WriteLine(p.GetNonZeroCount());
            tout.WriteLine(p.GetColBoundType(2));
            tout.WriteLine(p.GetColLb(2));
            tout.WriteLine(p.GetColUb(2));
            p.SetColKind(1, COLKIND.Integer);

            tout.WriteLine(p.GetColsCount());
            tout.WriteLine(p.GetRowsCount());
            Console.Out.WriteLine(p.GetVersion());
            p.TermHook(new TermHookDelegate(Hook));
            p.ModelClass = MODELCLASS.MIP;
            BnCCallback.SetCallback(p,
                                    new BranchAndCutDelegate(
                                        delegate(BranchTree t, Object o)
            {
                Console.WriteLine(t.GetReason().ToString());
            }
                                        ));
            p.SolveSimplex();
            p.SolveInteger();
            p.WriteSol("c:\\sol.txt");
            KKT    kkt = p.CheckKKT(0);
            int    count;
            int    cpeak;
            double total;
            double tpeak;

            p.GetMemoryUsage(out count, out cpeak, out total, out tpeak);
            Console.Out.WriteLine(count);
            Console.Out.WriteLine(cpeak);
            Console.Out.WriteLine(total);
            Console.Out.WriteLine(tpeak);

            LPProblem copy = p.Clone(true);

            p.Clear();

            Console.Out.WriteLine("tapez une touche.");
            Console.In.ReadLine();
            //Console.In.Read();
        }