//build model for multi class problems public Problem buildModelMultiClass(ObjectInstanceSelection firefly, Problem prob) { int tNI = firefly.Attribute_Values.Count(); //size of each Instance Mask List <double> y = new List <double>(); List <Node[]> x = new List <Node[]>(); bool pos = false, neg = false; List <double> classes = getClassLabels(prob.Y); //get the class labels int nClass = classes.Count; //count the number of classes int[] classCount = new int[nClass]; //building model for each instance in instance mask in each firefly object for (int j = 0; j < tNI; j++) { if (firefly.__Attribute_Values[j] == 1) //if instance is selected, use for classification { int p = firefly.__Pointers[j]; x.Add(prob.X[p]); y.Add(prob.Y[p]); for (int i = 0; i < nClass; i++) { if (prob.Y[p] == classes[i]) { classCount[i]++; //count the total number of instances in each class } } } else { continue; } } Node[][] X = new Node[x.Count][]; double[] Y = new double[y.Count]; //ensuring that the subproblem consist of both positive and negative instance int k = 0; if (classCount.Sum() == 0) //if the sum is zero, then no instance was selected { return(null); } else //ensure that instance mask contains at least, one of each class instance { for (int a = 0; a < nClass; a++) { if (classCount[a] == 0) { int m = 0; for (int i = 0; i < prob.Count; i++) //if no instance in this class, search the whole subproblem and insert one instance in the kth position of subproblem { if (prob.Y[i] == classes[a]) { x[k] = prob.X[i]; //insert negative instance in the first and second position y[k] = prob.Y[i]; //insert label k++; m++; } if (m == 2) { break; } } } } } x.CopyTo(X); //convert from list to double[] array y.CopyTo(Y); Problem subProb = new Problem(X.Count(), Y, X, X[0].GetLength(0)); return(subProb); }
//build model for binary problems public Problem buildModel(ObjectInstanceSelection firefly, Problem prob) { int tNI = firefly.Attribute_Values.Count(); //size of each Instance Mask List <double> y = new List <double>(); List <Node[]> x = new List <Node[]>(); bool pos = false, neg = false; //building model for each instance in instance mask in each firefly object for (int j = 0; j < tNI; j++) { if (firefly.__Attribute_Values[j] == 1) //if instance is selected, use for classification { int p = firefly.__Pointers[j]; x.Add(prob.X[p]); y.Add(prob.Y[p]); if (prob.Y[p] == 1) { pos = true; } else if (prob.Y[p] == -1) { neg = true; } } else { continue; } } Node[][] X = new Node[x.Count][]; double[] Y = new double[y.Count]; //ensuring that the subproblem consist of both positive and negative instance int k = 0; int countP = y.Count(r => r == 1); //counting the total number of positive instance in the subpeoblem int countN = y.Count(r => r == -1); //counting the total number of negative instance in the subproble if (pos == false && neg == false) //if no instance (positive and negative) was selected, return null. Don't perform any computation { return(null); } else if (pos == false || countP <= 1) //if pos == false, then no positive instance is in the subproblem { for (int i = 0; i < prob.Count; i++) //if no positive instance, search the whole subproblem and insert two postive instance in the first and second position of subproblem { if (prob.Y[i] == 1) { x[k] = prob.X[i]; //insert negative instance in the first and second position y[k] = prob.Y[i]; //insert label k++; } if (k == 2) { break; } } } else if (neg == false || countN <= 1) //if neg == false, then no negative instance is in the subproblem { k = 0; for (int i = 0; i < prob.Count; i++) //if no negative instance, search the whole subproblem and insert two negative instances in the first and second position of subproblem { if (prob.Y[i] == -1) { x[k] = prob.X[i]; //insert negative instance in the first and second position y[k] = prob.Y[i]; //insert label k++; } if (k == 2) { break; } } } x.CopyTo(X); //convert from list to double[] array y.CopyTo(Y); Problem subProb = new Problem(X.Count(), Y, X, X[0].GetLength(0)); return(subProb); }