private KeyValuePair<Fruit, float> CalculateFitness(IEnumerable<Fruit> fruits, Fruit match)
        {
            var bestFitness = 0.0f;
            var mostFitFruit = new Fruit();

            foreach (var fruit in fruits) {

                var fitness = 0.0f;
                if (fruit.Type == match.Type) {
                    fitness += TypeFit;
                }
                if (fruit.Color == match.Color) {
                    fitness += ColorFit;
                }
                else {
                    fitness += WeightFit;
                }

                var c = PercentDeviation(match.Weight, fruit.Weight);
                fitness += (1 - c) * WeightFit;

                if (!(fitness > bestFitness)) continue;

                bestFitness = fitness;
                mostFitFruit = fruit;
            }

            RemoveFromSupply(mostFitFruit);

            return new KeyValuePair<Fruit, float>(mostFitFruit, bestFitness * 100);
        }
        public KeyValuePair<Fruit, float> PickFruit(IEnumerable<Fruit> fruits, Fruit match)
        {
            var picked =
                fruits.Where(x => x.Type == match.Type).Where(y => y.Color == match.Color).FirstOrDefault(z => z.Weight == match.Weight);

            if (picked == null) return new KeyValuePair<Fruit, float>(null, 0);

            RemoveFromSupply(picked);
            return new KeyValuePair<Fruit, float>(picked, 100);
        }
        public IEnumerable<Fruit> Provide(int howMany)
        {
            var random = new Random();
            var fruitArray = Enum.GetValues(typeof(FruitType));
            var colorArray = Enum.GetValues(typeof(FruitColor));

            for (var i = 0; i < howMany; i++) {
                var fruit = new Fruit(
                    (FruitType)fruitArray.GetValue(random.Next(1, fruitArray.Length)),
                    (FruitColor)fruitArray.GetValue(random.Next(1, colorArray.Length)),
                    random.Next(50, 500));

                yield return fruit;
            }
        }
        public void RemoveFromSupply(Fruit match)
        {
            using (var dbContext = _dbHandler as FruitMachineDbContext)
            {
                if (dbContext != null)
                {
                    var result = from f in dbContext.Fruits where f.FruitId == match.FruitId select f;

                    if (result.Any())
                    {
                        var f = result.First();
                        dbContext.Fruits.Remove(f);
                        dbContext.SaveChanges();
                    }
                }
            }
        }
 /// <summary>
 /// Matches existing fruit objects against list of criteria
 /// Match priority: Type 70%, Color 20%, Weight 10% 
 /// </summary>
 /// <param name="fruits">Collection of Fruit objects to test against</param>
 /// <param name="match">Fruit object with the exact criteria to match</param>
 /// <returns></returns>
 public KeyValuePair<Fruit, float> PickFruit(IEnumerable<Fruit> fruits, Fruit match)
 {
     return CalculateFitness(fruits, match);
 }
 public void PrintResult(Fruit fruit, float fitness)
 {
     Console.WriteLine("This came close: ");
     Console.WriteLine(fruit);
     Console.WriteLine("With a fitness of " + fitness);
 }