Beispiel #1
0
 public void Fix(Mutator mutator)
 {
     foreach (Configuration config in _configurations)
     {
         if (mutator.Rand(20) == 0)
         {
             config.Fix(mutator);
         }
     }
 }
Beispiel #2
0
 public Generation(Mutator mutator, int size, Knapsack knapsack)
 {
     _knapsack       = knapsack;
     _configurations = new List <Configuration>(size);
     for (int i = 0; i < size; i++)
     {
         Configuration configuration = new Configuration(knapsack);
         configuration.Randomize(mutator);
         _configurations.Add(configuration);
     }
 }
Beispiel #3
0
        public Configuration Cross(Mutator mutator, Configuration other, int count)
        {
            Configuration configuration = new Configuration(_knapsack);

            mutator.Mutate(configuration._presence, count);

            for (int i = 0; i < _presence.Length; i++)
            {
                configuration._presence[i] = configuration._presence[i] ? other._presence[i] : _presence[i];
            }

            return(configuration);
        }
Beispiel #4
0
        public void Mutate(Mutator mutator, int maxSize, float percent, float countPercent)
        {
            int flipCount = (int)((_knapsack.ItemValues.Length * percent) * 0.5f);
            int count     = (int)((maxSize * percent) * 0.5f);

            Mutator.Shuffle(_configurations);
            List <Configuration> newConfigs = new List <Configuration>(count);

            for (int i = 0; i < count && i < _configurations.Count; i++)
            {
                Configuration config = new Configuration(_configurations[i]);
                config.Mutate(mutator, flipCount);
                newConfigs.Add(config);
            }
            _configurations.AddRange(newConfigs);
        }
Beispiel #5
0
        public Generation(Mutator mutator, List <Configuration> bestConfigurations, int fullSize, Knapsack knapsack)
        {
            _knapsack       = knapsack;
            _configurations = new List <Configuration>(fullSize);
            Debug.Assert(bestConfigurations.Count <= fullSize);
            int i = 0;

            for (i = 0; i < bestConfigurations.Count; i++)
            {
                _configurations.Add(new Configuration(bestConfigurations[i]));
            }
            for (; i < fullSize; i++)
            {
                Configuration configuration = new Configuration(knapsack);
                configuration.Randomize(mutator);
                _configurations.Add(configuration);
            }
        }
Beispiel #6
0
        public void Cross(Mutator mutator, int maxSize, Generation other, float percent, float countPercent)
        {
            int crossCount = (int)((_knapsack.ItemValues.Length * percent) * 0.5f);
            int count      = (int)((maxSize * percent) * 0.5f);

            Configuration[] prevConfigs = new Configuration[other._configurations.Count];
            other._configurations.CopyTo(prevConfigs);
            Mutator.Shuffle(_configurations);
            List <Configuration> newConfigs = new List <Configuration>(count);

            for (int i = 0; i < count && i < other._configurations.Count; i++)
            {
                Configuration config = new Configuration(_configurations[i]);
                config.Cross(mutator, prevConfigs[i], crossCount);
                newConfigs.Add(config);
            }
            _configurations.AddRange(newConfigs);
        }
Beispiel #7
0
        public override int Solve()
        {
            int bits = _knapsack.ItemValues.Length / 2;

            _mutator = new Mutator(bits);

            _generation = new Generation(_mutator, _entityCount, _knapsack);

            for (int i = 0; i < _iterationCount; i++)
            {
                _generation = Cycle(_generation, i);
                string text = $"{i},{_knapsack.Solution},{BestSolution()}";
                //string text = $"{i},{BestSolution()}";
                //Console.WriteLine(text);
                //Out(text);
                //Console.WriteLine($"{i}:" + BestSolution());
            }

            return(BestSolution());
        }
Beispiel #8
0
        public Generation SelectBest(Mutator mutator, int count, int maxSize)
        {
            SortedSet <ConfigurationSortedItem> set = new SortedSet <ConfigurationSortedItem>();

            for (int i = 0; i < _configurations.Count; i++)
            {
                set.Add(new ConfigurationSortedItem(i, _configurations[i].Evaluate()));
            }
            List <Configuration> configurations = new List <Configuration>(count);

            foreach (ConfigurationSortedItem configurationSortedItem in set.Reverse())
            {
                if (configurations.Count >= count)
                {
                    break;
                }

                configurations.Add(_configurations[configurationSortedItem.Idx]);
            }
            return(new Generation(mutator, configurations, maxSize, _knapsack));
        }
Beispiel #9
0
        public void Fix(Mutator mutator)
        {
            int weight = SumWeight();

            while (weight > _knapsack.Capacity)
            {
                int idx   = mutator.Rand(_presence.Length);
                int dir   = mutator.Rand(2);
                int found = -1;
                if (dir == 0)
                {
                    for (int i = idx; i < _presence.Length; i++)
                    {
                        if (_presence[idx])
                        {
                            found = idx;
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = idx; i >= 0; i--)
                    {
                        if (_presence[idx])
                        {
                            found = idx;
                            break;
                        }
                    }
                }
                if (found >= 0)
                {
                    _presence[found] = false;
                    weight          -= _knapsack.ItemValues[found * 2];
                }
            }
        }
Beispiel #10
0
        public void MutateDuplicates(Mutator mutator, float percent)
        {
            int flipCount = (int)((_knapsack.ItemValues.Length * percent) * 0.5f);
            List <Configuration> newConfigs = new List <Configuration>();

            if (_configurations.Count > 0)
            {
                newConfigs.Add(_configurations[0]);
            }
            for (int i = 1; i < _configurations.Count; i++)
            {
                if (_configurations[i].Same(_configurations[i - 1]))
                {
                    Configuration config = _configurations[i];
                    config.Mutate(mutator, flipCount);
                    newConfigs.Add(config);
                }
                else
                {
                    newConfigs.Add(_configurations[i]);
                }
            }
            _configurations = newConfigs;
        }
Beispiel #11
0
 public void Mutate(Mutator mutator, int count)
 {
     Debug.Assert(count <= _presence.Length);
     mutator.Mutate(_presence, count);
 }
Beispiel #12
0
 public void Randomize(Mutator mutator)
 {
     Mutate(mutator, _presence.Length);
 }