public Knapsack SolveProblemUsing( KnapsackAlgorithmType algorithmType, List <Supply> possibleSupplies, int maxWeight) { // loop thru all registered implementations foreach (Type impl in RegisteredImplementations) { // get attributes for this type object[] attrlist = impl.GetCustomAttributes(true); // loop thru all attributes for this class foreach (object attr in attrlist) { if (attr is KnapsackAlgorithmAttribute) { if (((KnapsackAlgorithmAttribute)attr).TypeOfAlgorithm.Equals(algorithmType)) { IKnapsackAlgorithm knapsackAlgorithm = (KnapsackAlgorithmBase)Activator.CreateInstance(impl); return(knapsackAlgorithm.Execute(possibleSupplies, maxWeight)); } } } } throw new Exception("Could not find a KnapsackAlgorithmBase implementation for this AlgorithmType"); }
//private void SimpleCase2(IKnapsackAlgorithm algorithm) => // TestCase(algorithm, new int[] { 3, 6, 2, 4, 1, 10, 2, 7, 8, 2, 4, 9 }, new int[] { 1, 3, 5, 7, 9, 11 }); private void TestCase(IKnapsackAlgorithm algorithm, Thing[] input, int maxSize, int[] expected) { var resIndexes = algorithm.Get(input, maxSize).ToArray(); var resThings = resIndexes.Select(x => input[x]).ToArray(); AssertCaseConstraint(resThings, maxSize); AssertAnswer(expected, resIndexes); }
private void SimpleCase1(IKnapsackAlgorithm algorithm) => TestCase(algorithm, new Thing[] { new Thing { Value = 3, Size = 4 }, new Thing { Value = 2, Size = 3 }, new Thing { Value = 4, Size = 2 }, new Thing { Value = 4, Size = 3 } }, 6, new int[] { 2, 3 });
private void TestAlgorithm(IKnapsackAlgorithm algorithm) { SimpleCase1(algorithm); //SimpleCase2(algorithm); }