public double FitnessValue(Individual secondInd, KnapsackList knapsackList)
        {
            bool strategy1 = this.strategy;
            bool strategy2 = secondInd.strategy;

            if (strategy1)
            {
                if (strategy2)
                {
                    return(fitnessCooperativeCooperative(knapsackList));
                }
                else
                {
                    return(fitnessCooperativeDefector(knapsackList));
                }
            }
            else
            {
                if (strategy2)
                {
                    return(fitnessDefectorCooperative(knapsackList));
                }
                else
                {
                    return(fitnessDefectorDefector(knapsackList));
                }
            }
        }
        public double FitnessValue(KnapsackList knapsackList)
        {
            double sum = 0.0;

            if (this.isFeasible)
            {
                for (int i = 0; i < this.chromosome.Count; i++)
                {
                    sum += this.chromosome[i] ? knapsackList[0].GetValue(i) : 0.0;
                }

                return(sum / GA_GT.maxFitness);
            }

            else
            {
                double deltaV = GA_GT.cheatingDegree / 100.0;
                for (int i = 0; i < this.chromosome.Count; i++)
                {
                    sum += this.chromosome[i] ? knapsackList[0].GetValue(i) + deltaV : 0.0;
                }

                return((sum - NonFeasibleKnapsacks(knapsackList) * knapsackList[0].maxValue) / GA_GT.maxFitness);
            }
        }
        public int NonFeasibleKnapsacks(KnapsackList knapsackList)
        {
            int counter = 0;

            foreach (Knapsack k in knapsackList.knapsackList)
            {
                if (!this.chromosome.IsFeasible(k))
                {
                    counter++;
                }
            }

            return(counter);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string path = @"..\..\samples\MK250.txt";

            KnapsackList knapsackList = InputOutput.ReadInput(path);

            knapsackList.Show();
            init_static_GA_GT(knapsackList);

            TestPopulation t = new TestPopulation();

            GA_GT ga_gt = new GA_GT();

            ga_gt.RunGA_GT().Show();
            Console.WriteLine("End");
            Console.ReadLine();
        }
        static void init_static_GA_GT(KnapsackList knapsackList)
        {
            GA_GT.gameModel = new PrisonersDilemma();
            GA_GT.knapsackList = knapsackList;
            GA_GT.weightGA = 0.8;
            GA_GT.weightGT = 1.0 - GA_GT.weightGA;
            GA_GT.cheatingDegree = 50;
            GA_GT.cheaterRate = 0.1;
            GA_GT.crossoverRate = 0.75;

            GA_GT.numberOfEpochs = 1000;

            GA_GT.chromosomeLength = InputOutput.objectsNumber;
            GA_GT.populationSize = 40;           // must be even number!!

            GA_GT.mutationRate = 1.0 / GA_GT.chromosomeLength;

            GA_GT.feasibleRate = 0.2;
        }
Beispiel #6
0
        static void init_static_GA_GT(KnapsackList knapsackList)
        {
            GA_GT.gameModel      = new PrisonersDilemma();
            GA_GT.knapsackList   = knapsackList;
            GA_GT.weightGA       = 0.8;
            GA_GT.weightGT       = 1.0 - GA_GT.weightGA;
            GA_GT.cheatingDegree = 50;
            GA_GT.cheaterRate    = 0.1;
            GA_GT.crossoverRate  = 0.75;

            GA_GT.numberOfEpochs = 1000;

            GA_GT.chromosomeLength = InputOutput.objectsNumber;
            GA_GT.populationSize   = 40;         // must be even number!!

            GA_GT.mutationRate = 1.0 / GA_GT.chromosomeLength;

            GA_GT.feasibleRate = 0.2;
        }
        private double fitnessCooperativeDefector(KnapsackList knapsackList)
        {
            Knapsack knapsack = knapsackList[0];
            double   sum      = 0.0;

            for (int i = 0; i < this.chromosome.Count; i++)
            {
                sum += this.chromosome[i] ? knapsack.GetValue(i) : 0.0;
            }

            if (this.isFeasible)
            {
                return(GA_GT.weightGA * sum / GA_GT.maxFitness
                       + GA_GT.weightGT * GA_GT.gameModel.cooperatorDefectorPayoff / GA_GT.maxPayoff);
            }

            else
            {
                return(GA_GT.weightGA * (sum - NonFeasibleKnapsacks(knapsackList) * knapsack.maxValue) / GA_GT.maxFitness
                       + GA_GT.weightGT * GA_GT.gameModel.cooperatorDefectorPayoff / GA_GT.maxPayoff);
            }
        }
        private double fitnessDefectorDefector(KnapsackList knapsackList)
        {
            Knapsack knapsack = knapsackList[0];
            double   deltaV   = GA_GT.cheatingDegree / 100.0;
            //double deltaW = GA_GT.cheatingDegree / 100.0;
            double sum = 0.0;

            for (int i = 0; i < this.chromosome.Count; i++)
            {
                sum += this.chromosome[i] ? knapsack.GetValue(i) + deltaV : 0.0;
            }

            if (this.isFeasible)
            {
                return(GA_GT.weightGA * sum / GA_GT.maxFitness
                       + GA_GT.weightGT * GA_GT.gameModel.defectorDefectorPayoff / GA_GT.maxPayoff);
            }

            else
            {
                return(GA_GT.weightGA * (sum - NonFeasibleKnapsacks(knapsackList) * knapsack.maxValue) / GA_GT.maxFitness
                       + GA_GT.weightGT * GA_GT.gameModel.defectorDefectorPayoff / GA_GT.maxPayoff);
            }
        }
 public void Update(KnapsackList knapsackList)
 {
     isFeasible = chromosome.IsFeasible();
     fitness    = FitnessValue(knapsackList);
 }
        public double FitnessValue(Individual secondInd, KnapsackList knapsackList)
        {
            bool strategy1 = this.strategy;
            bool strategy2 = secondInd.strategy;

            if (strategy1)
            {
                if (strategy2)
                {
                    return fitnessCooperativeCooperative(knapsackList);
                }
                else
                {
                    return fitnessCooperativeDefector(knapsackList);
                }
            }
            else
            {
                if (strategy2)
                {
                    return fitnessDefectorCooperative(knapsackList);
                }
                else
                {
                    return fitnessDefectorDefector(knapsackList);
                }
            }
        }
        private double fitnessDefectorDefector(KnapsackList knapsackList)
        {
            Knapsack knapsack = knapsackList[0];
            double deltaV = GA_GT.cheatingDegree / 100.0;
            //double deltaW = GA_GT.cheatingDegree / 100.0;
            double sum = 0.0;

            for (int i = 0; i < this.chromosome.Count; i++)
            {
                sum += this.chromosome[i] ? knapsack.GetValue(i) + deltaV : 0.0;
            }

            if (this.isFeasible)
            {
                return GA_GT.weightGA * sum / GA_GT.maxFitness
                     + GA_GT.weightGT * GA_GT.gameModel.defectorDefectorPayoff / GA_GT.maxPayoff;
            }

            else
            {
                return GA_GT.weightGA * (sum - NonFeasibleKnapsacks(knapsackList) * knapsack.maxValue) / GA_GT.maxFitness
                     + GA_GT.weightGT * GA_GT.gameModel.defectorDefectorPayoff / GA_GT.maxPayoff;
            }
        }
        private double fitnessCooperativeDefector(KnapsackList knapsackList)
        {
            Knapsack knapsack = knapsackList[0];
            double sum = 0.0;

            for (int i = 0; i < this.chromosome.Count; i++)
            {
                sum += this.chromosome[i] ? knapsack.GetValue(i) : 0.0;
            }

            if (this.isFeasible)
            {
                return GA_GT.weightGA * sum / GA_GT.maxFitness
                     + GA_GT.weightGT * GA_GT.gameModel.cooperatorDefectorPayoff / GA_GT.maxPayoff;
            }

            else
            {
                return GA_GT.weightGA * (sum - NonFeasibleKnapsacks(knapsackList) * knapsack.maxValue) / GA_GT.maxFitness
                     + GA_GT.weightGT * GA_GT.gameModel.cooperatorDefectorPayoff / GA_GT.maxPayoff;
            }
        }
 public void Update(KnapsackList knapsackList)
 {
     isFeasible = chromosome.IsFeasible();
     fitness = FitnessValue(knapsackList);
 }
        public int NonFeasibleKnapsacks(KnapsackList knapsackList)
        {
            int counter = 0;

            foreach (Knapsack k in knapsackList.knapsackList)
            {
                if (!this.chromosome.IsFeasible(k))
                    counter++;
            }

            return counter;
        }
        public double FitnessValue(KnapsackList knapsackList)
        {
            double sum = 0.0;

            if (this.isFeasible)
            {
                for (int i = 0; i < this.chromosome.Count; i++)
                {
                    sum += this.chromosome[i] ? knapsackList[0].GetValue(i) : 0.0;
                }

                return sum / GA_GT.maxFitness;
            }

            else
            {
                double deltaV = GA_GT.cheatingDegree / 100.0;
                for (int i = 0; i < this.chromosome.Count; i++)
                {
                    sum += this.chromosome[i] ? knapsackList[0].GetValue(i) + deltaV : 0.0;
                }

                return (sum - NonFeasibleKnapsacks(knapsackList) * knapsackList[0].maxValue) / GA_GT.maxFitness;
            }
        }