// 判断一组配置是否合法 public bool IsLegal(Genome genome) { //Firstly, we think about whether it's a legal tree-like model; for (int i = 0; i < genome.aGene.Length; i++) { SPL.FeaturePoint t = ((SPL.FeaturePoint)spl.aFeaturePoint[i]); if (genome.aGene[i] == 1) { t.isChosen = true; } else { t.isChosen = false; } } //只有全员通过考验,才算通过 foreach (SPL.FeaturePoint featurePoint in spl.aFeaturePoint) { if (featurePoint.CheckValidity() == false) { return(false); } } //Secondly, we consider about the constraints; foreach (SPL.Constraint constraint in spl.aConstraint) { //如果A被选中,那么针对关系的不同,判断B在不在 if (genome.aGene[constraint.indexA] == 1) { switch (constraint.constraintRealtion) { case SPL.Constraint.relation.implied: { if (genome.aGene[constraint.indexB] != 1) { return(false); } break; } case SPL.Constraint.relation.excluded: { if (genome.aGene[constraint.indexB] == 1) { return(false); } break; } } } } return(true); }
// Optimization function 1 --- For SPL // !!! Need to be rewritten when apply to another problem; private double optFunc2(Genome genome) { int sumPerformance = 0; for (int i = 0; i < genome.GetLength(); i++) { if (genome.aGene[i] == 1) { SPL.FeaturePoint tempPoint = (SPL.FeaturePoint)spl.aFeaturePoint[i]; sumPerformance += tempPoint.performance; } } return(sumPerformance); }
// TEST EVENT public void test() { //Genome a = new Genome(); //a.aGene[0] = 1; //a.aGene[1] = 1; //a.aGene[4] = 1; //MessageBox.Show(IsLegal(a).ToString()); int c = 0; int total_price; int total_performance; Genome[] allPossibleSet = new Genome[(int)Math.Pow(2, spl.aFeaturePoint.Count)]; for (int i = 0; i < allPossibleSet.Length; i++) { //将每个基因基于编号编码, 最终实现全部的0,1取值 allPossibleSet[i] = new Genome(); Code(ref allPossibleSet[i], i); if (IsLegal(allPossibleSet[i]) == true) { total_price = 0; total_performance = 0; c++; for (int j = 0; j < allPossibleSet[i].aGene.Length; j++) { Console.Write(allPossibleSet[i].aGene[j]); if (allPossibleSet[i].aGene[j] == 1) { SPL.FeaturePoint t = (SPL.FeaturePoint)spl.aFeaturePoint[j]; total_price += t.price; total_performance += t.performance; } } Console.Write("total_price:" + total_price + " total_performance" + total_performance); Console.WriteLine(); } } MessageBox.Show(c.ToString()); }
// 将一组不合法的配置变成合法配置 public void TransToLegal(ref Genome genome) { //Console.Write("Before:"); //for (int i = 0; i < genome.aGene.Length; i++) //{ // Console.Write(genome.aGene[i]); //} //Console.WriteLine(); for (int i = 0; i < genome.aGene.Length; i++) { SPL.FeaturePoint t = ((SPL.FeaturePoint)spl.aFeaturePoint[i]); if (genome.aGene[i] == 1) { t.isChosen = true; } else { t.isChosen = false; } } //纠错策略为,从第一个特征点开始,让每个点都没有问题 foreach (SPL.FeaturePoint featurePoint in spl.aFeaturePoint) { int error = featurePoint.GetErrorNumber(); while (error != 0) { switch (error) { //序号1: 强制节点没有选择 -- 那就选 case 1: featurePoint.isChosen = true; break; //序号2:自己选择,父节点却没有选 -- 那自己也别选了,不然父亲还会出错 case 2: featurePoint.isChosen = false; break; //序号3: Alternative关系出错 -- 那就把孩子全部放弃,挑一个选 case 3: foreach (SPL.FeaturePoint children in featurePoint.children) { children.isChosen = false; } int rd = random.Next(0, featurePoint.children.Length); featurePoint.children[rd].isChosen = true; break; //序号4:Or关系出错(也就是说一个都没选) -- 那就让孩子每个都有概率选中 case 4: foreach (SPL.FeaturePoint children in featurePoint.children) { if (random.NextDouble() < 0.5) { children.isChosen = true; } } break; } error = featurePoint.GetErrorNumber(); } } for (int i = 0; i < spl.aFeaturePoint.Count; i++) { SPL.FeaturePoint t = (SPL.FeaturePoint)spl.aFeaturePoint[i]; if (t.isChosen == true) { genome.aGene[i] = 1; } else { genome.aGene[i] = 0; } } //Console.Write("After:"); //for (int i = 0; i < genome.aGene.Length; i++) //{ // Console.Write(genome.aGene[i]); //} //Console.WriteLine(); }