예제 #1
0
        private void Start_Click(object sender, EventArgs e)
        {
            nDim      = int.Parse(n_dim.Text);        //维度
            forDesign = new List <HanselChain>();
            GFunction.getInstance().mapGValue.Clear();
            result = GenerateCube.GenerateNdimCubeAndHanselChain(nDim);
            for (int i = 0; i < result.Count; i++)
            {
                forDesign.Add(result[i].DeepClone());
            }

            InitPaintCube(nDim, forDesign);
            functionInference = new FunctionInference();
            Next.Text         = "执行算法";
            runal             = new AutoResetEvent(false);
            listA.Items.Clear();
            listB.Items.Clear();
            listQuery.Items.Clear();
            listGFunc.Items.Clear();
            GValue.Text = "G:";
            queryList   = null;
            if (runAlgorithm != null)
            {
                runAlgorithm.Abort();
                runAlgorithm = null;
            }
            CubeRepaint();
        }
예제 #2
0
 public static GFunction getInstance()
 {
     if (instance == null)
     {
         instance = new GFunction();
     }
     return(instance);
 }
예제 #3
0
        public static void Test(String input, String output)
        {
            const int          nDim              = 5;
            int                runTimes          = 0;
            int                totalCount        = 0;
            int                incorrectCount    = 0;
            DateTime           startTime         = DateTime.Now;
            List <HanselChain> hcs               = GenerateCube.GenerateNdimCubeAndHanselChain(nDim);
            FunctionInference  functionInference = new FunctionInference();

            foreach (HanselChain hc in hcs)
            {
                foreach (NPoint p in hc.chain)
                {
                    NPoint dp = p.DeepClone();
                    dp.gfuncValue = 0;
                    GFunction.getInstance().mapGValue.Add(dp.toInt(), dp);
                }
            }
            XElement xe = XElement.Load(input);
            IEnumerable <XElement> elements = from ele in xe.Elements("I_f") select ele;

            foreach (XElement e in elements)
            {
                String f  = e.Element("f").Value;
                var    gs = e.Elements("g");
                foreach (var g in gs)
                {
                    totalCount++;
                    String gfunc = g.Value;
                    GFunction.getInstance().g_function = gfunc;
                    functionInference.RunAlgorithm(null, nDim, hcs);
                    functionInference.A.Sort(delegate(NPoint p1, NPoint p2)
                    {
                        BigInteger i1 = p1.toInt();
                        BigInteger i2 = p2.toInt();
                        return(-i1.CompareTo(i2));
                    });
                    String getf = "";
                    for (int i = 0; i < f.Length; i++)
                    {
                        getf += functionInference.A[i].realValue.Value;
                        functionInference.A[i].Destory();
                    }
                    if (!f.Equals(getf) || functionInference.asked.Count > 20)
                    {
                        ++incorrectCount;
                        Console.Out.WriteLine("g:{0},getf:{1},pf:{2},count:{3}", gfunc, getf, f, functionInference.asked.Count);
                    }
                    ++runTimes;
                    if (runTimes % 1000 == 0)
                    {
                        Console.Out.WriteLine("{0} times.  Time usage:{1}.  IncorrectCount:{2}.", runTimes, (DateTime.Now - startTime).ToString(), incorrectCount);
                    }
                }
            }
            Console.Out.WriteLine("Test finished. Total:{0}", totalCount);
        }
예제 #4
0
        private void SavefuncMenuItem_Click(object sender, EventArgs e)
        {
            //保存g函数
            SaveDialog sd = new SaveDialog();

            sd.nDim      = nDim;
            sd.lstPoints = GFunction.getInstance().mapGValue.Values.ToList <NPoint>();
            sd.ShowDialog(this);
        }
예제 #5
0
        private void CreateFunc_Click(object sender, EventArgs e)
        {
            GFunction.getInstance().mapGValue.Clear();

            //所有点的G值设为0,并显示
            foreach (HanselChain hc in forDesign)
            {
                foreach (NPoint p in hc.chain)
                {
                    p.gfuncValue = 0;
                    GFunction.getInstance().mapGValue.Add(p.toInt(), p);
                }
            }

            GFunction.getInstance().g_function = g_function.Text;            //函数g
            CubeRepaint();
        }
예제 #6
0
        private void ContextMenuLoadgf_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            String        name  = e.ClickedItem.Text;
            int           order = (int)e.ClickedItem.Tag;
            GFunctionNode node  = gLoadNodes[order];

            GFunction.getInstance().g_function = node.gFunction;
            //List<NPoint> ps = GFunction.getInstance().mapGValue.Values.ToList<NPoint>();
            //ps.Sort(
            //	delegate (NPoint p1, NPoint p2)
            //	{
            //		BigInteger i1 = p1.toInt();
            //		BigInteger i2 = p2.toInt();
            //		return i1.CompareTo(i2);
            //	}
            //);
            //for(int i = 0; i < ps.Count; ++i)
            //{
            //	ps[i].gfuncValue = node.points[i].gfuncValue;
            //}
            CubeRepaint();
        }
예제 #7
0
        //return asked count
        private int Algorithm()
        {
            while (B.Count != 0)
            {
                if (runal != null)
                {
                    runal.WaitOne();
                }
                //1.选点
                NPoint alpha_i = B[0];
                if (asked.Count >= 1)
                {
                    NPoint alpha_i_1 = asked[asked.Count - 1];
                    //如果αi的绝对值等于αi-1的绝对值,那么就选αi
                    alpha_i = PickFromBShortest(alpha_i_1);
                }
                int val = GFunction.getInstance().Calculate(alpha_i);
                alpha_i.gfuncValue = val;
                //AddPointToA(alpha_i);
                AddPointsToA(new List <NPoint>()
                {
                    alpha_i
                }, null, null);
                asked.Add(alpha_i);

                //2.查看是否出现逆序对
                //先判断是否有之前未处理的逆序对
                isFromList = true;
                foreach (var reverseOrder in unhandledReverseOrders)
                {
                    HandleReverseOrder(reverseOrder.alpha, reverseOrder.beta);
                }
                isFromList = false;
                //
                bool   bReverseOrder = false;
                NPoint getVal        = null;
                if (val == 1)
                {
                    List <NPoint> upoints = getDirectUpPoints(alpha_i);
                    foreach (NPoint p in upoints)
                    {
                        if (A.Contains(p))
                        {
                            if (p.gfuncValue == 0)
                            {
                                //一次只会出现一个逆序对
                                Debug.Assert(bReverseOrder != true);
                                getVal        = p;
                                bReverseOrder = true;
                                //break;
                            }
                        }
                    }
                }
                else
                {
                    List <NPoint> dpoints = getDirectDownPoints(alpha_i);
                    foreach (NPoint p in dpoints)
                    {
                        if (A.Contains(p))
                        {
                            if (p.gfuncValue == 1)
                            {
                                Debug.Assert(bReverseOrder != true);
                                getVal        = p;
                                bReverseOrder = true;
                                //break;
                            }
                        }
                    }
                }
                if (bReverseOrder)
                {
                    Debug.Assert(getVal != null);
                    //出现逆序对
                    if (alpha_i.gfuncValue == 1)
                    {
                        HandleReverseOrder(alpha_i, getVal);
                    }
                    else
                    {
                        HandleReverseOrder(getVal, alpha_i);
                    }
                }

                //3.共同确定
                //未出现逆序对 或 已经处理完逆序对
                for (int i = 0; i < A.Count; i++)
                {
                    for (int j = i + 1; j < A.Count; j++)
                    {
                        NPoint beta1 = A[i];
                        NPoint beta2 = A[j];
                        if (!beta1.Equals(beta2))
                        {
                            if (beta1.HasNOne() == beta2.HasNOne() && beta1.gfuncValue == beta2.gfuncValue)
                            {
                                HandleBeta1AndBeta2(beta1, beta2);
                            }
                        }
                    }
                }

                //4.遍历A寻找非G
                for (int i = 0; i < A.Count; ++i)
                {
                    NPoint beta = A[i];
                    //找到全部的β∈A,g(β)==0?
                    if (beta.gfuncValue == 0)
                    {
                        //找到β的所有直接下级γ,如果使g(γ)==1的γ的数量为0且g(γ)==0的γ的数量为|β|-1
                        List <NPoint> dbeta   = getDirectDownPoints(beta);
                        int           oneCnt  = 0;
                        int           zeroCnt = 0;
                        foreach (NPoint gamma in dbeta)
                        {
                            if (gamma.realValue == 1)
                            {
                                oneCnt++;
                            }
                            else if (gamma.realValue == 0)
                            {
                                zeroCnt++;
                            }
                        }
                        if (oneCnt == 0 && (zeroCnt == (beta.HasNOne() - 1)))
                        {
                            //令β的所有下级δ,f(δ)=0,所有δ归于A
                            AddPointsToA(dbeta, 0, null);
                            //foreach (NPoint delta in dbeta)
                            //{
                            //	delta.ffuncValue = 0;
                            //	AddPointToA(delta);
                            //}
                        }
                    }
                    else
                    {
                        //找到β的所有直接上级γ,如果使g(γ)==0的γ的数量为0且g(γ)==1的γ的数量为n-|β|-1
                        List <NPoint> ubeta   = getDirectUpPoints(beta);
                        int           oneCnt  = 0;
                        int           zeroCnt = 0;
                        foreach (NPoint gamma in ubeta)
                        {
                            if (gamma.realValue == 1)
                            {
                                oneCnt++;
                            }
                            else if (gamma.realValue == 0)
                            {
                                zeroCnt++;
                            }
                        }
                        if (zeroCnt == 0 && (oneCnt == (nDim - beta.HasNOne() - 1)))
                        {
                            //令β的所有上级δ,f(δ)=1,所有δ归于A
                            AddPointsToA(ubeta, 1, null);
                            //foreach (NPoint delta in ubeta)
                            //{
                            //	delta.ffuncValue = 1;
                            //	AddPointToA(delta);
                            //}
                        }
                    }
                }
            }
            return(asked.Count);
        }
예제 #8
0
        void UpdateListView()
        {
            List <NPoint> B = functionInference.B;
            List <NPoint> A = functionInference.A;

            queryList = functionInference.asked;
            //diffList = functionInference.Diff;
            paintCube.askedList = queryList;
            NPoint G = functionInference.G;

            //更新B控件
            listB.BeginUpdate();
            listB.Items.Clear();
            //if (listB.Items.Count < B.Count)
            //{
            for (int i = 0; i < B.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (i + 1).ToString();
                lvi.SubItems.Add(B[i].ToString());
                listB.Items.Add(lvi);
            }
            //}
            //else
            //{
            //	for (int i = B.Count; i < listB.Items.Count; i++)
            //	{
            //		listB.Items.RemoveAt(i);
            //	}
            //}
            listB.EndUpdate();

            listA.BeginUpdate();
            //更新A控件
            listA.Items.Clear();
            for (int i = 0; i < A.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (listA.Items.Count + 1).ToString();
                //lvi.ForeColor = Color.Red;
                lvi.SubItems.Add(A[i].ToString());
                lvi.SubItems.Add(A[i].realValue.ToString());
                lvi.SubItems.Add(A[i].gfuncValue.ToString());
                listA.Items.Add(lvi);
            }
            listA.EndUpdate();

            listQuery.BeginUpdate();
            //更新Query控件
            //listQuery.Items.Clear();
            for (int i = listQuery.Items.Count; i < queryList.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (listQuery.Items.Count + 1).ToString();
                //lvi.ForeColor = Color.Red;
                lvi.SubItems.Add(queryList[i].ToString());
                listQuery.Items.Add(lvi);
            }
            listQuery.EndUpdate();

            //更新G
            if (G != null)
            {
                GValue.Text = String.Format("G:{0}", G.ToString());
            }

            //更新函数g
            listGFunc.BeginUpdate();
            //更新Query控件
            //listGFunc.Items.Clear();
            List <NPoint> gps = GFunction.getInstance().mapGValue.Values.ToList <NPoint>();

            for (int i = listGFunc.Items.Count; i < gps.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (listGFunc.Items.Count + 1).ToString();
                //lvi.ForeColor = Color.Red;
                lvi.SubItems.Add(gps[i].ToString());
                lvi.SubItems.Add(gps[i].gfuncValue.ToString());
                listGFunc.Items.Add(lvi);
            }
            listGFunc.EndUpdate();

            CubeRepaint();
        }