コード例 #1
0
        public void Hierarchy_AddAlternative_Success()
        {
            //Arrange
            var hierarchy = new Hierarchy();

            hierarchy.GoalNode.AddCriterionNode("Criterion1");
            hierarchy.GoalNode.CriterionNodes[0].AddSubcriterionNode("Criterion11");
            hierarchy.GoalNode.AddCriterionNode("Criterion2");
            hierarchy.GoalNode.AddCriterionNode("Criterion3");

            var alternative1 = new Alternative("Alternative1");
            var alternative2 = new Alternative("Alternative2");
            var alternative3 = new Alternative("Alternative3");

            //Act
            hierarchy.AddAlternative(alternative1);
            hierarchy.AddAlternative(alternative2);
            hierarchy.AddAlternative(alternative3);

            //Assert
            foreach (var criterion in hierarchy.GetLowestCriterionNodes())
            {
                Assert.IsNotNull(criterion.AlternativeNodes[alternative1]);
                Assert.IsNotNull(criterion.AlternativeNodes[alternative2]);
                Assert.IsNotNull(criterion.AlternativeNodes[alternative3]);
            }
        }
コード例 #2
0
        public void Hierarchy_RefreshAlternativeNodes_AppendsAlternativeNodes()
        {
            //Arrange
            var hierarchy    = new Hierarchy();
            var alternative1 = hierarchy.AddAlternative("Alternative1");
            var alternative2 = hierarchy.AddAlternative("Alternative2");
            var alternative3 = hierarchy.AddAlternative("Alternative3");

            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion1"));
            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion2"));
            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion3"));

            var alternative4 = new Alternative("Alternative4");
            var criterion11  = hierarchy.GoalNode.CriterionNodes[0].AddSubcriterionNode("Criterion11");

            //Act
            hierarchy.RefreshAlternativeNodes();

            //Assert
            foreach (var criterion in hierarchy.GetLowestCriterionNodes())
            {
                Assert.IsTrue(criterion.AlternativeNodes.Contains(alternative1));
                Assert.IsTrue(criterion.AlternativeNodes.Contains(alternative2));
                Assert.IsTrue(criterion.AlternativeNodes.Contains(alternative3));
            }
        }
コード例 #3
0
        public void Hierarchy_AddAlternativeTwice_ThrowsException()
        {
            //Arrange
            var       hiearachy    = new Hierarchy();
            var       alternative1 = new Alternative("Alternative1");
            Exception exception    = null;

            //Act
            hiearachy.AddAlternative(alternative1);
            try
            {
                hiearachy.AddAlternative(alternative1);
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            //Assert
            Assert.AreEqual(1, hiearachy.Alternatives.Count());
            Assert.AreEqual(alternative1, hiearachy.Alternatives.ElementAt(0));
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(ArgumentException));
            Assert.AreEqual("Same alternative can not be added twice.", exception.Message);
        }
コード例 #4
0
        public void Hierarchy_RemoveAlternative_Success2()
        {
            //Arrange
            var hierarchy = new Hierarchy();

            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion1"));
            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion2"));
            hierarchy.GoalNode.AddCriterionNode(new CriterionNode("Criterion3"));
            hierarchy.GoalNode.CriterionNodes.ElementAt(0).AddSubcriterionNode(new CriterionNode("Criterion11"));

            var alternative1 = new Alternative("Alternative1");
            var alternative2 = new Alternative("Alternative2");
            var alternative3 = new Alternative("Alternative3");

            //Act
            hierarchy.AddAlternative(alternative1);
            hierarchy.AddAlternative(alternative2);
            hierarchy.AddAlternative(alternative3);
            hierarchy.RemoveAlternative(alternative1);

            //Assert
            foreach (var criterion in hierarchy.GetLowestCriterionNodes())
            {
                Assert.IsFalse(criterion.AlternativeNodes.Contains(alternative1));
            }
        }
コード例 #5
0
        public void CriterionNode_SubcriterionNodes_Add_AddsWithFixUp()
        {
            //Arrange
            var hierarchy = new Hierarchy();

            hierarchy.AddAlternative(new Alternative("Alternative1"));
            hierarchy.AddAlternative(new Alternative("Alternative2"));

            var criterion    = hierarchy.GoalNode.AddCriterionNode("Criterion");
            var subcriterion = new CriterionNode();

            //Act
            criterion.AddSubcriterionNode(subcriterion);

            //Assert
            Assert.AreEqual(criterion, subcriterion.ParentCriterionNode);
            Assert.AreEqual(0, criterion.AlternativeNodes.Count);
        }
コード例 #6
0
        public void CriterionNode_AlternativeNodesCount_WithNodes_Returns1()
        {
            //Arrange => Act
            var hierarchy = new Hierarchy();

            hierarchy.AddAlternative(new Alternative("Alternative"));
            var criterion = hierarchy.GoalNode.AddCriterionNode("Criterion");

            //Assert
            Assert.AreEqual(1, criterion.AlternativeNodes.Count);
        }
コード例 #7
0
        public void AlternativeNode_GlobalPriority_ReturnsCorrectValue()
        {
            //Arrange => Act
            var alternative1 = new Alternative();
            var alternative2 = new Alternative();
            var criterion    = new CriterionNode("Criterion", 0.374M);

            var hierarchy = new Hierarchy();

            hierarchy.AddAlternative(alternative1);
            hierarchy.AddAlternative(alternative2);
            hierarchy.GoalNode.AddCriterionNode(criterion);

            criterion.AlternativeNodes[alternative1].LocalPriority = 0.791M;
            criterion.AlternativeNodes[alternative2].LocalPriority = 0.673M;

            //Assert
            Assert.AreEqual(0.791M * 0.374M, criterion.AlternativeNodes[alternative1].GlobalPriority);
            Assert.AreEqual(0.673M * 0.374M, criterion.AlternativeNodes[alternative2].GlobalPriority);
        }
コード例 #8
0
        public void Hierarchy_RemoveAlternative_Success()
        {
            //Arrange
            var hiearachy   = new Hierarchy();
            var alternative = new Alternative("Alternative1");

            //Act
            hiearachy.AddAlternative(alternative);
            hiearachy.RemoveAlternative(alternative);

            //Assert
            Assert.IsFalse(hiearachy.Alternatives.Contains(alternative));
        }
コード例 #9
0
        public void Hierarchy_AddAlternative_IdAndName_Success()
        {
            //Arrange
            var hiearachy = new Hierarchy();

            //Act
            var alternative = hiearachy.AddAlternative("ID123", "Alternative");

            //Assert
            Assert.AreEqual(1, hiearachy.Alternatives.Count());
            Assert.AreEqual(alternative, hiearachy.Alternatives.ElementAt(0));
            Assert.AreEqual("ID123", alternative.ID);
            Assert.AreEqual("Alternative", alternative.Name);
        }
コード例 #10
0
        public void HierarchyProcessor_Analyse_Calculates()
        {
            //This is sample hierarchy from Wikipedia article about AHP
            //http://en.wikipedia.org/wiki/Analytic_Hierarchy_Process

            //Arrange
            var hierarchy = new Hierarchy("Choose optimal car");

            var costCriterion   = hierarchy.GoalNode.AddCriterionNode("Cost", 0.504M);
            var safetyCriterion = hierarchy.GoalNode.AddCriterionNode("Safety", 0.237M);
            var styleCriterion  = hierarchy.GoalNode.AddCriterionNode("Style", 0.042M);
            var capacityNode    = hierarchy.GoalNode.AddCriterionNode("Capacity", 0.217M);

            var purchasePriceCriterion    = costCriterion.AddSubcriterionNode("Purchase price", 0.488M);
            var fuelCostsCriterion        = costCriterion.AddSubcriterionNode("Fuel costs", 0.251M);
            var maintenanceCostsCriterion = costCriterion.AddSubcriterionNode("Maintenance costs", 0.100M);
            var resaleValueCriterion      = costCriterion.AddSubcriterionNode("Resale value", 0.161M);

            var cargoCapacityCriterion     = capacityNode.AddSubcriterionNode("Cargo capacity", 0.167M);
            var passengerCapacityCriterion = capacityNode.AddSubcriterionNode("Passenger capacity", 0.833M);

            var alternative1 = hierarchy.AddAlternative("Accord sedan");
            var alternative2 = hierarchy.AddAlternative("Accord Hybrid");
            var alternative3 = hierarchy.AddAlternative("Pilot CUV");
            var alternative4 = hierarchy.AddAlternative("CR-V SUV");
            var alternative5 = hierarchy.AddAlternative("Element SUV");
            var alternative6 = hierarchy.AddAlternative("Odyssey Minivan");

            #region Alternative node local priorities

            purchasePriceCriterion.AlternativeNodes[alternative1].LocalPriority = 0.242M;
            purchasePriceCriterion.AlternativeNodes[alternative2].LocalPriority = 0.027M;
            purchasePriceCriterion.AlternativeNodes[alternative3].LocalPriority = 0.027M;
            purchasePriceCriterion.AlternativeNodes[alternative4].LocalPriority = 0.242M;
            purchasePriceCriterion.AlternativeNodes[alternative5].LocalPriority = 0.362M;
            purchasePriceCriterion.AlternativeNodes[alternative6].LocalPriority = 0.100M;

            fuelCostsCriterion.AlternativeNodes[alternative1].LocalPriority = 0.188M;
            fuelCostsCriterion.AlternativeNodes[alternative2].LocalPriority = 0.212M;
            fuelCostsCriterion.AlternativeNodes[alternative3].LocalPriority = 0.133M;
            fuelCostsCriterion.AlternativeNodes[alternative4].LocalPriority = 0.160M;
            fuelCostsCriterion.AlternativeNodes[alternative5].LocalPriority = 0.151M;
            fuelCostsCriterion.AlternativeNodes[alternative6].LocalPriority = 0.156M;

            maintenanceCostsCriterion.AlternativeNodes[alternative1].LocalPriority = 0.357M;
            maintenanceCostsCriterion.AlternativeNodes[alternative2].LocalPriority = 0.312M;
            maintenanceCostsCriterion.AlternativeNodes[alternative3].LocalPriority = 0.084M;
            maintenanceCostsCriterion.AlternativeNodes[alternative4].LocalPriority = 0.100M;
            maintenanceCostsCriterion.AlternativeNodes[alternative5].LocalPriority = 0.089M;
            maintenanceCostsCriterion.AlternativeNodes[alternative6].LocalPriority = 0.058M;

            resaleValueCriterion.AlternativeNodes[alternative1].LocalPriority = 0.225M;
            resaleValueCriterion.AlternativeNodes[alternative2].LocalPriority = 0.095M;
            resaleValueCriterion.AlternativeNodes[alternative3].LocalPriority = 0.055M;
            resaleValueCriterion.AlternativeNodes[alternative4].LocalPriority = 0.415M;
            resaleValueCriterion.AlternativeNodes[alternative5].LocalPriority = 0.105M;
            resaleValueCriterion.AlternativeNodes[alternative6].LocalPriority = 0.105M;

            safetyCriterion.AlternativeNodes[alternative1].LocalPriority = 0.215M;
            safetyCriterion.AlternativeNodes[alternative2].LocalPriority = 0.215M;
            safetyCriterion.AlternativeNodes[alternative3].LocalPriority = 0.083M;
            safetyCriterion.AlternativeNodes[alternative4].LocalPriority = 0.038M;
            safetyCriterion.AlternativeNodes[alternative5].LocalPriority = 0.025M;
            safetyCriterion.AlternativeNodes[alternative6].LocalPriority = 0.424M;

            styleCriterion.AlternativeNodes[alternative1].LocalPriority = 0.346M;
            styleCriterion.AlternativeNodes[alternative2].LocalPriority = 0.346M;
            styleCriterion.AlternativeNodes[alternative3].LocalPriority = 0.045M;
            styleCriterion.AlternativeNodes[alternative4].LocalPriority = 0.160M;
            styleCriterion.AlternativeNodes[alternative5].LocalPriority = 0.025M;
            styleCriterion.AlternativeNodes[alternative6].LocalPriority = 0.078M;

            cargoCapacityCriterion.AlternativeNodes[alternative1].LocalPriority = 0.090M;
            cargoCapacityCriterion.AlternativeNodes[alternative2].LocalPriority = 0.090M;
            cargoCapacityCriterion.AlternativeNodes[alternative3].LocalPriority = 0.170M;
            cargoCapacityCriterion.AlternativeNodes[alternative4].LocalPriority = 0.170M;
            cargoCapacityCriterion.AlternativeNodes[alternative5].LocalPriority = 0.170M;
            cargoCapacityCriterion.AlternativeNodes[alternative6].LocalPriority = 0.310M;

            passengerCapacityCriterion.AlternativeNodes[alternative1].LocalPriority = 0.136M;
            passengerCapacityCriterion.AlternativeNodes[alternative2].LocalPriority = 0.136M;
            passengerCapacityCriterion.AlternativeNodes[alternative3].LocalPriority = 0.273M;
            passengerCapacityCriterion.AlternativeNodes[alternative4].LocalPriority = 0.136M;
            passengerCapacityCriterion.AlternativeNodes[alternative5].LocalPriority = 0.046M;
            passengerCapacityCriterion.AlternativeNodes[alternative6].LocalPriority = 0.273M;

            #endregion

            //Act
            var processor = new HierarchyProcessor();
            var result    = processor.Analyse(hierarchy);

            //Assert
            Assert.AreEqual(0.212885342M, result[alternative1]);
            Assert.AreEqual(0.150225038M, result[alternative2]);
            Assert.AreEqual(0.109231639M, result[alternative3]);
            Assert.AreEqual(0.164945910M, result[alternative4]);
            Assert.AreEqual(0.142593084M, result[alternative5]);
            Assert.AreEqual(0.220118987M, result[alternative6]);
        }