/* Check a way so that the proportion: * Compares the data with all the data in the same gesture with the same name. * Find the alpha between the first found * add alpha to alphas * Then it has to do the same for all the other gestures */ private static PrimitiveData getPrimitiveWithSameName(PrimitiveData data, SetOfPrimitives gesture) { foreach (PrimitiveData result in gesture) { if ((data.Name == result.Name) && (data.IDcompare(result.ID))) return result; } return null; }
private static PrimitiveData checkValueComplexPrimitives(PrimitiveData reference) { //calculate the difference between the alphas // if the difference is than TOLERANCE -- that means that all the alpha have similar values // if alpha is about 1 then use a fixed value (that means that all the examples have a similar value) // if create a mutiplicity rule using the average of SUM(alphas) if (reference.ID.Count() > 1) return null; IEnumerable<PrimitiveData> similarValues = from set in samples from primitive in set where primitive.Name == reference.Name && reference.Value != 0 && reference.IDcompare(primitive.ID) && !(from s in solution select s.Name) .Contains(reference.Name) select primitive; PrimitiveData newPrimitive = null; if ((similarValues != null) && (similarValues.Count() == samples.Count)) { List<double> values = new List<double>(); foreach (PrimitiveData p in similarValues) { values.Add(p.Value); } double value = Math.Round(values.Average(), 2); double maxDifference = values.Max() - values.Min(); double tolerance = Math.Round(value * matchingAccuracy, 2); if (maxDifference > tolerance) return newPrimitive; else if (maxDifference == 0) { newPrimitive = new PrimitiveData(); newPrimitive.ID = reference.ID; newPrimitive.Name = reference.Name; newPrimitive.Value = value; } else if (maxDifference <= tolerance) { newPrimitive = new PrimitiveData(); newPrimitive.ID = reference.ID; newPrimitive.Name = reference.Name; newPrimitive.Value = 0; newPrimitive.ListValue.Add((value - tolerance) + ".." + (value + tolerance)); } } return newPrimitive; }
private static void checkProportionforPrimitive(PrimitiveData reference) { /* * On the same sample, compare to see the relation between the basic primitives, if there is any proportion, if find any, create a new complex primitive that will * reference the basic similar ones */ if (reference.Value == 0) { return; } /* IEnumerable<PrimitiveData> similarInSameStep = from set in samples from primitive in set where primitive.Name == data.Name && primitive.ID.Count == 1 && data.IDcompare(primitive.ID) select primitive; */ PrimitiveData originalData = reference; SetOfPrimitives gesture; List<SetOfPrimitives> proportions = new List<SetOfPrimitives>(); PrimitiveData newPrimitive = null; List<double> values = new List<double>(); values.Add(reference.Value); int i = 0; gesture = samples.ElementAt(i); while (i < samples.Count) { PrimitiveData compare; for (int j = 0; j < gesture.Count; j++) { compare = gesture[j]; if ((reference.Name == compare.Name) && (reference.IDcompare(compare.ID))) { values.Add(compare.Value); } if (reference.Equals(compare)) { continue; } if ((reference.Name == compare.Name) && (compare.ID.Count == 1) && (reference.ID[0] < compare.ID[0])) { newPrimitive = new PrimitiveData(); newPrimitive.Name = reference.Name; newPrimitive.IDadd(reference.ID[0]); newPrimitive.IDadd(compare.ID[0]); newPrimitive.Value = compare.Value / reference.Value; while (proportions.Count <= i) { proportions.Add(new SetOfPrimitives()); } proportions[i].Add(newPrimitive); } } i++; if (i < samples.Count) { gesture = samples.ElementAt(i); reference = getPrimitiveWithSameName(reference, gesture); if (reference == null) return; values.Add(reference.Value); } } // After the loops, the proportions should have the same amount of items as samples // and each setofprimitive of proportion should have the same amount of primitives // If not, then nothing will be add to solution // if it is, the same comparison should be done in proportions if (proportions.Count < samples.Count) { // No proportion was found, try to see if the primitive is a constant among the samples checkConstantValuePrimitives(originalData); return; } List<double> alphas; int setCount = proportions[0].Count; for (i = 0; i < proportions[0].Count; i++) { alphas = new List<double>(); foreach (SetOfPrimitives set in proportions) { if (setCount != set.Count) return; alphas.Add(set[i].Value); } double value = Math.Round(values.Average(), 2); double maxDifference = alphas.Max() - alphas.Min(); double tolerance = Math.Round(value * matchingAccuracy, 2); if (maxDifference > tolerance) { newPrimitive = new PrimitiveData(); newPrimitive.ID = originalData.ID; newPrimitive.Name = originalData.Name; newPrimitive.ListValue.Add((value - tolerance) + ".." + (value + tolerance)); solution.checkAndAdd(newPrimitive); } else if ((maxDifference != 0)) { string variable = VariableName.getAvailableLetter(); // Adds the x primitive newPrimitive = new PrimitiveData(); newPrimitive.IDadd(proportions[0][i].ID[0]); newPrimitive.Name = proportions[0][i].Name; newPrimitive.Value = 0; newPrimitive.ListValue.Add(variable); solution.checkAndAdd(newPrimitive); // adds the alpha*x primitive newPrimitive = new PrimitiveData(); newPrimitive.IDadd(proportions[0][i].ID[1]); newPrimitive.Name = proportions[0][i].Name; newPrimitive.Value = 0; double alpha = Math.Round(alphas.Average(), 2); //newPrimitive.strValue = alpha + "x"; tolerance = Math.Round(alpha * matchingAccuracy, 2); newPrimitive.ListValue.Add((alpha - tolerance) + variable + ".." + (alpha + tolerance) + variable); solution.checkAndAdd(newPrimitive); } else { newPrimitive = new PrimitiveData(); newPrimitive.IDadd(proportions[0][i].ID[0]); newPrimitive.Name = proportions[0][i].Name; newPrimitive.Value = proportions[0][i].Value; solution.checkAndAdd(newPrimitive); newPrimitive = new PrimitiveData(); newPrimitive.IDadd(proportions[0][i].ID[1]); newPrimitive.Name = proportions[0][i].Name; newPrimitive.Value = proportions[0][i].Value; solution.checkAndAdd(newPrimitive); } } }
private static void checkStringComplexPrimitives(PrimitiveData data) { //calculate the difference between the alphas // if the difference is than TOLERANCE -- that means that all the alpha have similar values // if alpha is about 1 then use a fixed value (that means that all the examples have a similar value) // if create a mutiplicity rule using the average of SUM(alphas) if (solution.Contains(data)) return; IEnumerable<PrimitiveData> similarStrings = from set in samples from primitive in set where primitive.Name == data.Name && data.Value == 0 && data.IDcompare(primitive.ID) && data.StrListcompare(primitive.ListValue) select primitive; if ((similarStrings != null) && (similarStrings.Count() == samples.Count)) { PrimitiveData newPrimitive = new PrimitiveData(); newPrimitive.ID = data.ID; newPrimitive.Name = data.Name; newPrimitive.ListValue = data.ListValue; solution.Add(newPrimitive); } }
private static void checkConstantValuePrimitives(PrimitiveData data) { /* * On the same sample, compare to see the relation between the basic primitives, if there is any proportion, create a new complex primitive that will * reference the basic similar ones */ SetOfPrimitives gesture; List<double> values = new List<double>(); values.Add(data.Value); for (int i = 0; i < samples.Count; i++) { gesture = samples.ElementAt(i); foreach (PrimitiveData compare in gesture) { if (!(data.Equals(compare)) && (data.Name == compare.Name) && (data.IDcompare(compare.ID))) { values.Add(compare.Value); } } } double value = Math.Round(values.Average(), 2); double maxDifference = values.Max() - values.Min(); double tolerance = Math.Round(value * matchingAccuracy, 2); if (maxDifference > tolerance) return; PrimitiveData newPrimitive = new PrimitiveData(); if (maxDifference == 0) { newPrimitive.ID = data.ID; newPrimitive.Name = data.Name; newPrimitive.Value = data.Value; } else { newPrimitive.ID = data.ID; newPrimitive.Name = data.Name; newPrimitive.ListValue.Add((value - tolerance) + ".." + (value + tolerance)); } solution.checkAndAdd(newPrimitive); }