Beispiel #1
0
        private double GetKnapsackCost(KnapsackConfig sack)
        {
            double count = 0;

            for (int i = 0; i < itemsAmount; i++)
            {
                if (sack.isValueActive(i))
                {
                    count += itemsCosts[i];
                }
            }

            return(count);
        }
Beispiel #2
0
 private static bool IsValid(KnapsackConfig config)
 {
     double[] summ = new double[dimensions];
     for (var i = 0; i < itemsAmount; i++)
     {
         if (config.isValueActive(i))
         {
             for (var j = 0; j < dimensions; j++)
             {
                 summ[j] += itemsSet[i, j];
                 if (summ[j] > restrictions[j])
                 {
                     return(false);
                 }
             }
         }
         //Amount of items is much bigger than number of dimensions, so we can do checks on every turn.
     }
     return(true);
 }
Beispiel #3
0
        private KnapsackConfig FirstApproachGenerate()
        {
            KnapsackConfig result = new KnapsackConfig(itemsAmount);

            for (var i = 0; i < itemsAmount; i++)
            {
                result.setValueToActive(i);
            }
            Random rand = new Random();

            while (!IsValid(result))
            {
                int positionNumber = rand.Next(itemsAmount);
                while (!result.isValueActive(positionNumber))
                {
                    positionNumber = rand.Next(itemsAmount);
                }
                result.setValueToPassive(positionNumber);
            }
            return(result);
        }