コード例 #1
0
        static void Main(string[] args)
        {
            var input1 = new Neural(new List <ISinapse <double> >(), (x, y) => 0);
            var input2 = new Neural(new List <ISinapse <double> >(), (x, y) => 1);

            var i1ToH1 = new Sinapse();
            var i2ToH1 = new Sinapse();

            Func <double, double, double> sum = (x, y) => x + y;

            var h1 = new Neural(new List <ISinapse <double> >()
            {
                i1ToH1, i2ToH1
            }, sum);

            i1ToH1.Next     = h1;
            i2ToH1.Next     = h1;
            i1ToH1.Weight   = 0.45;
            i2ToH1.Weight   = -0.12;
            i1ToH1.Previous = input1;
            i2ToH1.Previous = input2;


            var i1ToH2 = new Sinapse();
            var i2ToH2 = new Sinapse();


            var h2 = new Neural(new List <ISinapse <double> >()
            {
                i1ToH2, i2ToH2
            }, sum);

            i1ToH2.Next     = h2;
            i2ToH2.Next     = h2;
            i1ToH2.Weight   = 0.78;
            i2ToH2.Weight   = 0.13;
            i1ToH2.Previous = input1;
            i2ToH2.Previous = input2;


            var h1ToO1 = new Sinapse();
            var h2ToO1 = new Sinapse();


            var o1 = new Neural(new List <ISinapse <double> > {
                h1ToO1, h2ToO1
            }, sum);

            h1ToO1.Previous = h1;
            h1ToO1.Weight   = 1.5;
            h2ToO1.Previous = h2;
            h2ToO1.Weight   = -2.3;
            h1ToO1.Next     = o1;
            h2ToO1.Next     = o1;

            var res   = o1.Calculate();
            var error = GetError(res, 1);

            Console.WriteLine($"result = {res} error = {error}");
        }
コード例 #2
0
        private void getSamples()
        {
            try
            {
                Result = string.Empty;
                List <Neural> firstMGroup  = neurals.Where(ne => ne.W == T).ToList();
                List <Neural> secondMGroup = neurals.Where(ne => ne.W > (double)T / 2 && ne.W < T).ToList();
                List <Neural> thirdMGroup  = neurals.Where(ne => ne.W == (double)T / 2).ToList();
                List <Neural> fourthMGroup = neurals.Where(ne => ne.W < (double)T / 2).ToList();

                List <Neural> firstMGroupStar  = neurals.Where(ne => ne.W == T - 1).ToList();
                List <Neural> secondMGroupStar = neurals.Where(ne => ne.W > (double)(T - 1) / 2 && ne.W < T - 1).ToList();
                List <Neural> thirdMGroupStar  = neurals.Where(ne => ne.W == (double)(T - 1) / 2).ToList();
                List <Neural> fourthMGroupStar = neurals.Where(ne => ne.W < (double)(T - 1) / 2).ToList();

                List <List <Neural> > result = new List <List <Neural> >();
                //bool usedAllFourth = false;

                //1 group
                firstMGroup.ForEach(m => result.Add(new List <Neural> {
                    m
                }));

                //2 group
                //int fourthCounter = 0;
                if (fourthMGroup.Count == 0)
                {
                    secondMGroup.ForEach(m => result.Add(new List <Neural> {
                        m
                    }));
                }
                else
                {
                    foreach (var m in secondMGroup)
                    {
                        result.Add(new List <Neural> {
                            m, fourthMGroup.FirstOrDefault(f => f.W + m.W == T)
                        });
                    }
                }

                //3 group
                if (thirdMGroup.Count % 2 == 0)
                {
                    for (int i = 0; i < thirdMGroup.Count; i += 2)
                    {
                        result.Add(new List <Neural> {
                            thirdMGroup[i], thirdMGroup[i + 1]
                        });
                    }
                }
                else
                {
                    for (int i = 0; i < thirdMGroup.Count - 1; i += 2)
                    {
                        result.Add(new List <Neural> {
                            thirdMGroup[i], thirdMGroup[i + 1]
                        });
                    }
                    var temp = new List <Neural> {
                        thirdMGroup[thirdMGroup.Count - 1]
                    };
                    if (fourthMGroup.Any())
                    {
                        Neural t = fourthMGroup.FirstOrDefault(f => f.W + thirdMGroup[thirdMGroup.Count - 1].W == T);
                        if (t != null)
                        {
                            temp.Add(t);
                        }
                        else
                        {
                            int i = 0;
                            while (temp.Sum(te => te.W) != T)
                            {
                                temp.Add(fourthMGroup[i++]);
                            }
                        }
                    }

                    result.Add(temp);
                }

                //4 group

                List <Neural> neuralsForCheck = new List <Neural>();
                foreach (var res in result)
                {
                    neuralsForCheck.AddRange(res);
                }
                var fourthMGroupCopy = fourthMGroup.Where(g => !neuralsForCheck.Select(s => s.Number).Contains(g.Number)).ToList();
                for (int i = 0; i < fourthMGroupCopy.Count; i++)
                {
                    int           res  = fourthMGroupCopy[i].W;
                    List <Neural> temp = new List <Neural> {
                        fourthMGroupCopy[i]
                    };
                    if (res == T)
                    {
                        result.Add(temp);
                        break;
                    }

                    Neural t = fourthMGroup.FirstOrDefault(g => (g.W + res) == T && g.Number != fourthMGroupCopy[i].Number);
                    if (t != null)
                    {
                        temp.Add(t);
                    }

                    temp.Add(fourthMGroup.FirstOrDefault(g => g.Number != fourthMGroupCopy[i].Number));
                    res += fourthMGroup.FirstOrDefault(g => g.Number != fourthMGroupCopy[i].Number).W;
                    if (res == T)
                    {
                        result.Add(temp);
                        break;
                    }
                    List <Neural> tempNeurals = new List <Neural>();
                    tempNeurals.AddRange(firstMGroup);
                    tempNeurals.AddRange(secondMGroup);
                    tempNeurals.AddRange(thirdMGroup);
                    tempNeurals.AddRange(fourthMGroup);

                    foreach (var tempNeural in tempNeurals)
                    {
                        int a = res + tempNeural.W;
                        if (a == T)
                        {
                            temp.Add(tempNeural);
                            result.Add(temp);
                            break;
                        }
                    }
                }



                //STAR
                List <List <Neural> > result2 = new List <List <Neural> >();
                //1 group
                firstMGroupStar.ForEach(m => result2.Add(new List <Neural> {
                    m
                }));

                //2 group
                //int fourthCounter = 0;
                if (fourthMGroupStar.Count == 0)
                {
                    secondMGroupStar.ForEach(m => result2.Add(new List <Neural> {
                        m
                    }));
                }
                else
                {
                    foreach (var m in secondMGroupStar)
                    {
                        result2.Add(new List <Neural> {
                            m, fourthMGroupStar.FirstOrDefault(f => f.W + m.W == T - 1)
                        });
                    }
                }

                //3 group
                if (thirdMGroupStar.Count % 2 == 0)
                {
                    for (int i = 0; i < thirdMGroupStar.Count; i += 2)
                    {
                        result2.Add(new List <Neural> {
                            thirdMGroupStar[i], thirdMGroupStar[i + 1]
                        });
                    }
                }
                else
                {
                    for (int i = 0; i < thirdMGroupStar.Count - 1; i += 2)
                    {
                        result2.Add(new List <Neural> {
                            thirdMGroupStar[i], thirdMGroupStar[i + 1]
                        });
                    }
                    var temp = new List <Neural> {
                        thirdMGroupStar[thirdMGroupStar.Count - 1]
                    };
                    if (fourthMGroupStar.Any())
                    {
                        Neural t = fourthMGroupStar.FirstOrDefault(f => f.W + thirdMGroupStar[thirdMGroupStar.Count - 1].W == T - 1);
                        if (t != null)
                        {
                            temp.Add(t);
                        }
                        else
                        {
                            int i = 0;
                            while (temp.Sum(te => te.W) != T - 1)
                            {
                                temp.Add(fourthMGroup[i++]);
                            }
                        }
                    }

                    result2.Add(temp);
                }

                //4 group

                neuralsForCheck = new List <Neural>();
                foreach (var res in result2)
                {
                    neuralsForCheck.AddRange(res);
                }
                fourthMGroupCopy = fourthMGroupStar.Where(g => !neuralsForCheck.Select(s => s.Number).Contains(g.Number)).ToList();
                for (int i = 0; i < fourthMGroupCopy.Count; i++)
                {
                    int           res  = fourthMGroupCopy[i].W;
                    List <Neural> temp = new List <Neural> {
                        fourthMGroupCopy[i]
                    };
                    if (res == T - 1)
                    {
                        result2.Add(temp);
                        break;
                    }

                    Neural t = fourthMGroupStar.FirstOrDefault(g => (g.W + res) == T - 1 && g.Number != fourthMGroupCopy[i].Number);
                    if (t != null)
                    {
                        temp.Add(t);
                    }

                    temp.Add(fourthMGroupStar.FirstOrDefault(g => g.Number != fourthMGroupCopy[i].Number));
                    res += fourthMGroupStar.FirstOrDefault(g => g.Number != fourthMGroupCopy[i].Number).W;
                    if (res == T - 1)
                    {
                        result2.Add(temp);
                        break;
                    }
                    List <Neural> tempNeurals = new List <Neural>();
                    tempNeurals.AddRange(firstMGroupStar);
                    tempNeurals.AddRange(secondMGroupStar);
                    tempNeurals.AddRange(thirdMGroupStar);
                    tempNeurals.AddRange(fourthMGroupStar);

                    foreach (var tempNeural in tempNeurals)
                    {
                        int a = res + tempNeural.W;
                        if (a == T - 1)
                        {
                            temp.Add(tempNeural);
                            result2.Add(temp);
                            break;
                        }
                    }
                }
                //checkForTable(result, result2);

                //checkForCapacity(secondMGroup, fourthMGroup, firstMGroup.Count, thirdMGroup.Count, result);
                //checkForCapacity2(secondMGroupStar, fourthMGroupStar, firstMGroupStar.Count, thirdMGroupStar.Count, result2);

                //string s = string.Empty;
                //foreach (var res in result)
                //{
                //    int temp = res.Sum(r => r.W * Convert.ToInt32(r.X));
                //    s += (temp == T) + Environment.NewLine;
                //}
                ////MessageBox.Show(s);
                //Result += s + Environment.NewLine;

                //s = string.Empty;
                //foreach (var res in result2)
                //{
                //    int temp = res.Sum(r => r.W * Convert.ToInt32(r.X));
                //    s += (temp == T - 1) + Environment.NewLine;
                //}
                ////MessageBox.Show(s);
                //Result += s + Environment.NewLine;

                foreach (var res in result)
                {
                    for (int i = 1; i <= N; i++)
                    {
                        if (!res.Select(r => r.Number).Contains(i))
                        {
                            Result += string.Format("¬X{0} ", i);
                        }
                        else
                        {
                            Result += string.Format("X{0} ", i);
                        }
                    }
                    Result += Environment.NewLine;
                }
                Result += Environment.NewLine;
                foreach (var res in result2)
                {
                    for (int i = 1; i <= N; i++)
                    {
                        if (!res.Select(r => r.Number).Contains(i))
                        {
                            Result += string.Format("¬X{0} ", i);
                        }
                        else
                        {
                            Result += string.Format("X{0} ", i);
                        }
                    }
                    Result += Environment.NewLine;
                }

                OnPropertyChanged("Result");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
        }