public Chromosome(int size, Knapsack knapsack) { this.genes = new bool[size]; List <int> zeros = new List <int>(); for (int i = 0; i < size; i++) { zeros.Add(i); } while (zeros.Count > 0) { int index = zeros[GA_GT.random.Next() % zeros.Count]; this.genes[index] = true; if (!IsFeasible(knapsack)) { this.genes[index] = false; return; } zeros.Remove(index); } }
public Chromosome(int size, Knapsack knapsack) { this.genes = new bool[size]; List<int> zeros = new List<int>(); for (int i = 0; i < size; i++) { zeros.Add(i); } while (zeros.Count > 0) { int index = zeros[GA_GT.random.Next() % zeros.Count]; this.genes[index] = true; if (!IsFeasible(knapsack)) { this.genes[index] = false; return; } zeros.Remove(index); } }
public bool IsFeasible(Knapsack knapsack) { int sum = 0; for (int i = 0; i < genes.Length; i++) { sum += genes[i] ? knapsack.GetWeight(i) : 0; } if (sum > knapsack.constraint) { return(false); } return(true); }
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 bool IsFeasible(Knapsack knapsack) { int sum = 0; for (int i = 0; i < genes.Length; i++) { sum += genes[i] ? knapsack.GetWeight(i) : 0; } if (sum > knapsack.constraint) { return false; } return true; }