예제 #1
0
        private void Testing(OptimalEnum optimal)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[optimal]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                Optimal optimalAlgorithm = new Optimal(graph);
                optimalAlgorithm.Color();

                stringBuilder.AppendLine(optimal.ToString());
                stringBuilder.AppendLine("Graph colored.");
                stringBuilder.AppendLine(graph.GetColoredGraph().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(optimal.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("OptimalID,DiskNr,MovesO,ConfigurationID")] Optimal optimal)
        {
            if (id != optimal.OptimalID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(optimal);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OptimalExists(optimal.OptimalID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Variations"));
            }
            ViewData["ConfigurationID"] = new SelectList(_context.Configuration, "ConfigurationID", "ConfigurationID", optimal.ConfigurationID);
            return(View(optimal));
        }
예제 #3
0
        public async Task <IActionResult> Demo2(int fullName, int diskNr)
        {
            var viewModel = new ScoreIndexData();


            viewModel.Configurations = await _context.Configuration
                                       .Include(i => i.Optimals)
                                       .AsNoTracking()
                                       .ToListAsync();

            viewModel.Optimals = await _context.Optimal
                                 .Include(i => i.Configuration)
                                 .AsNoTracking()
                                 .ToListAsync();

            ViewData["ConfigurationID"] = fullName;
            Configuration configuration = viewModel.Configurations.Where(
                i => i.ConfigurationID == fullName).Single();

            ViewData["DiskNr"] = diskNr;
            Optimal Optimal = viewModel.Optimals.Where(o => o.ConfigurationID == fullName && o.DiskNr == diskNr).Single();

            var list = new[] { configuration.ConfigurationID, configuration.start, Optimal.MovesO, Optimal.OptimalID }.ToList();;


            var d = DateTime.Now.ToString("HH:mm:ss.ff");

            return(Json(list));
        }
예제 #4
0
        static void Main(string[] args)
        {
            Optimal game = new Optimal(11);

            System.Console.WriteLine("Максимальный выигрыш: " + game.MaxReward);
            System.Console.WriteLine("Пожилая стратегия: " + game.Way);

            System.Console.ReadKey();
        }
예제 #5
0
        public async Task <IActionResult> Create([Bind("OptimalID,DiskNr,MovesO,ConfigurationID")] Optimal optimal)
        {
            if (ModelState.IsValid)
            {
                _context.Add(optimal);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Variations"));
            }
            ViewData["ConfigurationID"] = new SelectList(_context.Configuration, "ConfigurationID", "ConfigurationID", optimal.ConfigurationID);
            return(View(optimal));
        }
예제 #6
0
        public void TestMethod4()
        {
            Optimal opt = new Optimal(new List <int>()
            {
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            });
            int expected_output = 1;

            int output = opt.GetMaxReward();

            Assert.AreEqual(expected_output, output);
        }
예제 #7
0
        public void TestMethod2()
        {
            Optimal opt = new Optimal(new List <int>()
            {
                1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
            });
            int expected_output = 3;

            int output = opt.GetMaxReward();

            Assert.AreEqual(expected_output, output);
        }
예제 #8
0
        public async Task <IActionResult> Demo2(int fullName, int diskNr)
        {
            var viewModel = new ScoreIndexData();


            viewModel.Configurations = await _context.Configuration
                                       .Include(i => i.Optimals)
                                       .AsNoTracking()
                                       .ToListAsync();

            viewModel.Optimals = await _context.Optimal
                                 .Include(i => i.Configuration)
                                 .AsNoTracking()
                                 .ToListAsync();

            var x = "test";


            if (fullName != null)
            {
                ViewData["ConfigurationID"] = fullName;
                Configuration configuration = viewModel.Configurations.Where(
                    i => i.ConfigurationID == fullName).Single();

                ViewData["DiskNr"] = diskNr;
                Optimal Optimal = viewModel.Optimals.Where(o => o.ConfigurationID == fullName && o.DiskNr == diskNr).Single();

                //foreach (d in variation.Optimals){d. }
                //var result = this.Json(variation, JsonRequestBehavior.AllowGet);
                List <Optimal> optimals = configuration.Optimals.ToList();

                //var h = optimals.ToArray();
                Optimal[] array = configuration.Optimals.Cast <Optimal>().ToArray();

                Object[] ArrayOfObjects = new Object[] { Optimal.OptimalID, configuration.ConfigurationID, configuration.start, configuration.end };
                var      list = new[] { configuration.ConfigurationID, configuration.start, Optimal.MovesO, Optimal.OptimalID }.ToList();;

                //foreach (var drat in configuration.Optimals) { if (drat.DiskNr == diskNr ) { list.Add(drat.MovesO);} list.Add(drat.OptimalID); };
                var d = DateTime.Now.ToString("HH:mm:ss.ff");
                return(Json(list));
            }

            return(new JsonResult("test"));
        }
예제 #9
0
        static void Main(string[] args)
        {
            Optimal opt = new Optimal();

            System.Console.WriteLine("Количество побед: " + opt.GetWinsCount());



            System.Console.ReadKey();

            /* var deck = new CardDeck();
             *
             * var card = new Card();
             * card.Number = Card.GetPictureNumber(CardPicture.K);
             * // card.Number = 10;
             * card.Color = CardColor.Spades;
             *
             * PrintCard(card);
             *
             * for (int i = 0; i < 3; i++)
             * {
             *   deck.Shuffle(11 + i);
             *
             *   System.Console.WriteLine("Play deck of " + deck.CardCount() + " cards:");
             *
             *   while (deck.HasCards())
             *   {
             *       card = deck.PickCard();
             *       PrintCard(card);
             *   }
             *
             *   System.Console.WriteLine("--- // ---");
             *   System.Console.WriteLine("Well done!");
             * }
             * System.Console.ReadKey();*/
        }
예제 #10
0
		public Encoder()
		{
			for (int i = 0; i < kNumOpts; i++)
				_optimum[i] = new Optimal();
			for (int i = 0; i < Base.kNumLenToPosStates; i++)
				_posSlotEncoder[i] = new RangeCoder.BitTreeEncoder(Base.kNumPosSlotBits);
		}
예제 #11
0
 public LZMAEncoder()
 {
     for (int i = 0; i < kNumOpts; i++)
         _optimum[i] = new Optimal();
     for (int i = 0; i < LZMABase.kNumLenToPosStates; i++)
         _posSlotEncoder[i] = new LZMARangeBitTreeEncoder(LZMABase.kNumPosSlotBits);
 }
        public async Task <IActionResult> Create([Bind("ConfigurationID,start,end,navodilo,conPic,VariationID")] Configuration configuration, int?o1, int?o2, int?o3, int?o4, int?o5, int?o6, int?o7, int?o8)
        {
            var p1 = 0;
            var p2 = 0;
            var p3 = 0;
            var p4 = 0;
            var p5 = 0;
            var p6 = 0;
            var p7 = 0;
            var p8 = 0;

            if (o1 != null)
            {
                p1 = o1.Value;
            }
            if (o2 != null)
            {
                p2 = o2.Value;
            }
            if (o3 != null)
            {
                p3 = o3.Value;
            }
            if (o4 != null)
            {
                p4 = o4.Value;
            }
            if (o5 != null)
            {
                p5 = o5.Value;
            }
            if (o6 != null)
            {
                p6 = o6.Value;
            }
            if (o7 != null)
            {
                p7 = o7.Value;
            }
            if (o8 != null)
            {
                p8 = o8.Value;
            }

            if (ModelState.IsValid)

            {
                var Optimals = new Optimal[]

                {
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 1, MovesO = p1
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 2, MovesO = p2
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 3, MovesO = p3
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 4, MovesO = p4
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 5, MovesO = p5
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 6, MovesO = p6
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 7, MovesO = p7
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 8, MovesO = p8
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 9, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 10, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 11, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 12, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 13, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 14, MovesO = 0
                    },
                    new Optimal {
                        ConfigurationID = configuration.ConfigurationID, DiskNr = 15, MovesO = 0
                    },
                };
                foreach (Optimal o in Optimals)
                {
                    _context.Optimal.Add(o);
                }



                _context.Add(configuration);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Variations"));
            }
            ViewData["VariationID"] = new SelectList(_context.Variation, "VariationID", "VariationID", configuration.VariationID);
            return(View(configuration));
        }
예제 #13
0
        public static Result MostGuessableMatchSequence(string password, IEnumerable <Match> matches, bool _exclude_additive = false)
        {
            int n = password.Length;

            List <List <Match> > matchesByJ = new List <List <Match> >();

            foreach (int i in EnumerableUtility.Range(0, n))
            {
                matchesByJ.Add(new List <Match>());
            }

            foreach (Match m in matches)
            {
                matchesByJ[m.j].Add(m);
            }

            foreach (List <Match> lst in matchesByJ)
            {
                lst.Sort(delegate(Match m1, Match m2)
                {
                    return(m1.i - m2.i);
                });
            }

            Optimal optimal = new Optimal(n);

            /*
             # helper: considers whether a length-l sequence ending at match m is better (fewer guesses)
             # than previously encountered sequences, updating state if so.
             */
            void update(Match m, int l)
            {
                // helper: considers whether a length-l sequence ending at match m is better (fewer guesses)
                // than previously encountered sequences, updating state if so.
                int    k  = m.j;
                double pi = EstimateGuesses(m, password);

                if (l > 1)
                {
                    /*
                     # we're considering a length-l sequence ending with match m:
                     # obtain the product term in the minimization function by multiplying m's guesses
                     # by the product of the length-(l-1) sequence ending just before m, at m.i - 1.
                     */
                    pi *= optimal.pi[m.i - 1][l - 1];
                }
                // calculate the minimization func
                double g = factorial(l) * pi;

                if (!_exclude_additive)
                {
                    g += Math.Pow(MIN_GUESSES_BEFORE_GROWING_SEQUENCE, l - 1);
                }
                // update state if new best
                // first see if any competing sequences covering this prefix, with l or fewer matches,
                // fare better than this sequence. if so, skip it and return
                foreach (KeyValuePair <int, double> val in optimal.g[k])
                {
                    int    competingL = val.Key;
                    double competingG = val.Value;
                    if (competingL > l)
                    {
                        continue;
                    }
                    if (competingG <= g)
                    {
                        return;
                    }
                }
                optimal.g[k][l]  = g;
                optimal.m[k][l]  = m;
                optimal.pi[k][l] = pi;
            }

            // helper: make bruteforce match objects spanning i to j, inclusive.
            Match makeBruteForceMatch(int i, int j)
            {
                Match m = new Match();

                m.Token   = password.Substring(i, j - i + 1);
                m.Pattern = "bruteforce";
                m.i       = i;
                m.j       = j;
                return(m);
            }

            // helper: evaluate bruteforce matches ending at k.
            void bruteForceUpdate(int k)
            {
                // see eif a single bruteforce match spanning the k-prefix is optimal
                Match m = makeBruteForceMatch(0, k);

                update(m, 1);
                foreach (int i in EnumerableUtility.Range(1, k + 1))
                {
                    // generate k bruteforce matches, spanning from (i=1, j=k) up to (i=k, j=k)
                    // see if adding these new matches to any of the sequences in optimal[i-1]
                    // leads to new bests
                    m = makeBruteForceMatch(i, k);
                    foreach (KeyValuePair <int, Match> val in optimal.m[i - 1])
                    {
                        int l = val.Key;
                        // corner: an optimal sequence will never have two adjacent bruteforce matches
                        // it is strictly better to have a single bruteforce match spanning the same region:
                        // same contribution to the guess product with a lower length
                        // --> safe to skip those cases
                        if (val.Value.Pattern != null && val.Value.Pattern.Equals("bruteforce"))
                        {
                            continue;
                        }
                        // try adding m to this length-l sequence
                        update(m, l + 1);
                    }
                }
            }

            // helper: step backwards through optimal.m starting at the end,
            // constructing the final optimal match sequence
            List <Match> unwind(int nN)
            {
                List <Match> optimalMatchSequenceList = new List <Match>();
                int          k = nN - 1;
                // find the final best seqence length and score
                int    l = 1;
                double g = Double.MaxValue;

                foreach (KeyValuePair <int, double> val in optimal.g[k])
                {
                    int    candidateL = val.Key;
                    double candidateG = val.Value;
                    if (candidateG < g)
                    {
                        l = candidateL;
                        g = candidateG;
                    }
                }

                while (k >= 0)
                {
                    Match m = optimal.m[k][l];
                    optimalMatchSequenceList.Insert(0, m);
                    k = m.i - 1;
                    l--;
                }
                return(optimalMatchSequenceList);
            }

            foreach (int k in EnumerableUtility.Range(0, n))
            {
                foreach (Match m in matchesByJ[k])
                {
                    if (m.i > 0)
                    {
                        foreach (int l in optimal.m[m.i - 1].Keys)
                        {
                            update(m, l + 1);
                        }
                    }
                    else
                    {
                        update(m, 1);
                    }
                }
                bruteForceUpdate(k);
            }
            List <Match> optimalMatchSequence = unwind(n);
            int          optimalL             = optimalMatchSequence.Count;

            double guesses = (password.Length == 0) ? 1 : optimal.g[n - 1][optimalL];

            double seconds = guesses / Math.Pow(10, 4);

            double crackTime = PasswordScoring.GuessesToCrackTime(guesses);
            // UnityEngine.Debug.Log($"Seconds: {seconds}");

            // Debug.Log($"Score: {PasswordScoring.GuessesToScore(guesses)}");

            int score = PasswordScoring.GuessesToScore(guesses);

            return(new Result
            {
                Password = password,
                Guesses = guesses,
                GuessesLog10 = Math.Log10(guesses),
                MatchSequence = optimalMatchSequence,
                CrackTime = Math.Round(crackTime, 3),
                CrackTimeDisplay = Utility.DisplayTime(crackTime),
                Score = score
            });
        }