Exemplo n.º 1
0
        public static PizzaPreferences operator +(PizzaPreferences a, PizzaPreferences b)
        {
            var pref = new PizzaPreferences();

            pref.likes = a.likes.Select(x => x).ToList();
            pref.likes.AddRange(b.likes);

            pref.hates = a.hates.Select(x => x).ToList();
            pref.hates.AddRange(b.hates);
            return(pref);
        }
Exemplo n.º 2
0
        public override void Run(IEnumerable <string> args)
        {
            int nlikes      = 2;
            int nhates      = 1;
            var maxToppings = int.Parse(args.First());

            PizzaPreferences[] prefs;
            if (args.Skip(1).First().ToLower() == "random")
            {
                if (args.Count() >= 4)
                {
                    prefs = PizzaPreferences.Random(int.Parse(args.Skip(2).First()),
                                                    nlikes,
                                                    nhates,
                                                    int.Parse(args.Skip(3).First()));
                }
                else
                {
                    prefs = PizzaPreferences.Random(int.Parse(args.Skip(2).First()),
                                                    nlikes,
                                                    nhates);
                }
            }
            else
            {
                prefs = JsonConvert.DeserializeObject <PizzaPreferences[]>(args.Skip(1).Aggregate((a, b) => $"{a}{b}"), new StringEnumConverter());
            }

            Console.WriteLine($"Testing FewestPizzas algorithms with max toppings {maxToppings} and preferences {JsonConvert.SerializeObject(prefs, Formatting.Indented, new StringEnumConverter())}");

            Compose();
            var sw = new Stopwatch();

            foreach (var q in pizzaAlgos)
            {
                string answer = "";
                try
                {
                    sw.Restart();
                    var result = q.Run(maxToppings, prefs);
                    answer = $"{result}";
                }
                catch (Exception ex)
                {
                    answer = $" !!! Threw exception with message: {ex.Message}";
                }
                Console.WriteLine($"{q.GetType().Name} (in {sw.ElapsedMilliseconds} ms) << {answer}");
            }
        }
Exemplo n.º 3
0
        public static PizzaPreferences [] Random(int n, int nlikes = 2, int nhates = 1, int randomSeed = int.MaxValue)
        {
            if (randomSeed == int.MaxValue)
            {
                randomSeed = Environment.TickCount;
            }
            var rand = new Random(randomSeed);

            Console.WriteLine($"Using random seed {randomSeed}");

            var prefs    = new PizzaPreferences[n];
            var toppings = Enum.GetValues(typeof(PizzaTopping));

            foreach (var i in Enumerable.Range(0, n))
            {
                prefs[i]       = new PizzaPreferences();
                prefs[i].likes = Enumerable.Repeat <Func <int> >(() => rand.Next(toppings.Length), nlikes).Select(v => (PizzaTopping)toppings.GetValue(v())).ToList();
                prefs[i].hates = Enumerable.Repeat <Func <int> >(() => rand.Next(toppings.Length), nhates).Select(v => (PizzaTopping)toppings.GetValue(v())).Where(t => !prefs[i].likes.Contains(t)).ToList();
            }
            return(prefs);
        }