public override IOperation Apply()
        {
            BinaryVector   binaryVector = BinaryVectorParameter.ActualValue;
            OneBitflipMove move         = OneBitflipMoveParameter.ActualValue;

            BinaryVector newSolution = new BinaryVector(binaryVector);

            newSolution[move.Index] = !newSolution[move.Index];

            DoubleValue quality = KnapsackEvaluator.Apply(newSolution,
                                                          KnapsackCapacityParameter.ActualValue,
                                                          PenaltyParameter.ActualValue,
                                                          WeightsParameter.ActualValue,
                                                          ValuesParameter.ActualValue).Quality;

            double moveQuality = quality.Value;

            if (MoveQualityParameter.ActualValue == null)
            {
                MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
            }
            else
            {
                MoveQualityParameter.ActualValue.Value = moveQuality;
            }
            return(base.Apply());
        }
예제 #2
0
 private void ParameterizeEvaluator()
 {
     if (Evaluator is KnapsackEvaluator)
     {
         KnapsackEvaluator knapsackEvaluator =
             (KnapsackEvaluator)Evaluator;
         knapsackEvaluator.BinaryVectorParameter.ActualName     = SolutionCreator.BinaryVectorParameter.ActualName;
         knapsackEvaluator.BinaryVectorParameter.Hidden         = true;
         knapsackEvaluator.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
         knapsackEvaluator.KnapsackCapacityParameter.Hidden     = true;
         knapsackEvaluator.WeightsParameter.ActualName          = WeightsParameter.Name;
         knapsackEvaluator.WeightsParameter.Hidden     = true;
         knapsackEvaluator.ValuesParameter.ActualName  = ValuesParameter.Name;
         knapsackEvaluator.ValuesParameter.Hidden      = true;
         knapsackEvaluator.PenaltyParameter.ActualName = PenaltyParameter.Name;
         knapsackEvaluator.PenaltyParameter.Hidden     = true;
     }
 }
예제 #3
0
        public override IOperation Apply()
        {
            BinaryVector sol = CurrentScope.Variables[SolutionParameter.ActualName].Value as BinaryVector;

            if (sol == null)
            {
                throw new ArgumentException("Cannot improve solution because it has the wrong type.");
            }

            // calculate value-to-weight ratio
            double[] ratio = new double[Values.Length];
            for (int i = 0; i < ratio.Length; i++)
            {
                ratio[i] = (double)Values[i] / (double)Weights[i];
            }


            // calculate order for ratio
            int[] order = new int[ratio.Length];
            foreach (var x in ratio.Select((x, index) => new { Value = x, ValueIndex = index })
                     .OrderByDescending(x => x.Value)
                     .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index }))
            {
                order[x.ItemIndex] = x.ValueIndex;
            }

            int evaluatedSolutions = 0;
            int j = sol.Length - 1;

            while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values)
                   .SumWeights.Value > KnapsackCapacity.Value && j >= 0)
            {
                sol[order[j--]] = false;
                evaluatedSolutions++;
            }

            // calculate weight
            int weight = 0;

            for (int i = 0; i < sol.Length; i++)
            {
                if (sol[i])
                {
                    weight += Weights[i];
                }
            }

            // improve solution
            bool feasible = true; j = 0;

            while (feasible && j < sol.Length)
            {
                while (sol[order[j]])
                {
                    j++;
                }
                if (weight + Weights[order[j]] <= KnapsackCapacity.Value)
                {
                    sol[order[j]] = true;
                    weight       += Weights[order[j]];
                }
                else
                {
                    feasible = false;
                }
                evaluatedSolutions++;
            }

            CurrentScope.Variables[SolutionParameter.ActualName].Value = sol;
            CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(evaluatedSolutions)));

            return(base.Apply());
        }
 protected KnapsackEvaluator(KnapsackEvaluator original, Cloner cloner) : base(original, cloner)
 {
 }
예제 #5
0
 protected KnapsackEvaluator(KnapsackEvaluator original, Cloner cloner) : base(original, cloner) { }