Example #1
0
        public IHarmonySearcher <StopTimeInfo> GetHarmonySearcher(Graph <int, StopTimeInfo> graph, Location source, Location destination)
        {
            var objectiveFunction = ObjectiveFunctionFactory.GetInstance(ObjectiveFunctionType, destination);

            var harmonyGenerator = HarmonyGeneratorFactory.GetInstance(HarmonyGeneratorType, objectiveFunction, graph, source, destination);

            var antColonyOptimizer = new StopTimeAntColonyOptimizer(objectiveFunction, graph, source, destination);

            var harmonySearcher = HarmonySearcherFactory.GetInstance(HarmonySearcherType, harmonyGenerator,
                                                                     HarmonyMemorySize, MaxImprovisationCount, HarmonyMemoryConsiderationRatio, PitchAdjustingRatio, MinPitchAdjustingRatio, MaxPitchAdjustingRatio, antColonyOptimizer);

            return(harmonySearcher);
        }
        public IObjectiveFunctionFactory CreateObjectiveFunctionFactory()
        {
            IObjectiveFunctionFactory factory = null;

            try
            {
                factory = new ObjectiveFunctionFactory();
            }
            catch (Exception exception)
            {
                this.Log.Error("Exception message: " + exception.Message + " and stacktrace " + exception.StackTrace);
            }

            return(factory);
        }
        public IObjectiveFunctionFactory CreateObjectiveFunctionFactory()
        {
            IObjectiveFunctionFactory factory = null;

            try
            {
                factory = new ObjectiveFunctionFactory();
            }
            catch (Exception exception)
            {
                this.Log.Error(
                    exception.Message,
                    exception);
            }

            return(factory);
        }
        public static void Example1()
        {
            TestModels.IModel modelFactory = TestModels.ModelFactory.Model2B();

            XmlFilePath       structureFile = modelFactory.Model();
            DelimitedFilePath dataFile      = modelFactory.Data();

            // Read in the model and the data.
            XElement model =
                XElement.Load(structureFile)
                .DefineAttributeData(dataFile);

            // Create the objective function.
            double ObjectiveFunction(double[] x)
            {
                XElement localModel = new XElement(model);

                localModel.SetConsumerPrices(x)
                .ShockProducerPrices()
                .CalculateMarketShares()
                .CalculateMarketEquilibrium();

                return(ObjectiveFunctionFactory.SumOfSquares(localModel));
            }

            // Set up the simplex solver.
            Simplex simplex =
                new Simplex(
                    objectiveFunction: ObjectiveFunction,
                    lowerBound: 0,
                    upperBound: 10,
                    //dimensions: model.DescendantsAndSelf().Count(),
                    dimensions: model.DescendantsAndSelf().Count(x => !x.HasElements),
                    iterations: 50000,
                    seed: 0,
                    textWriter: Console.Out
                    );

            // Find the minimum solution.
            Solution solution = simplex.Minimize();

            // Apply the final solution
            model.SetConsumerPrices(solution.Vector)
            .ShockProducerPrices()
            .CalculateMarketShares()
            .CalculateMarketEquilibrium();

            // Print the results
            PrintResults(model, solution);

            //// Set up the swarm solver.
            //PSO.Swarm swarm =
            //    new PSO.Swarm(
            //        objectiveFunction: x => objectiveFunction(x),
            //        lowerBound: 0,
            //        upperBound: 5,
            //        dimensions: model.DescendantsAndSelf().Count(x => !x.HasElements),
            //        iterations: 25000,
            //        particles: model.DescendantsAndSelf().Count(x => !x.HasElements) * 2,
            //        seed: 0,
            //        textWriter: Console.Out
            //    );

            //// Find the minimum solution.
            //Solution solution = PSO.MinimizeSwarmExtensions.Minimize(swarm);

            //// Apply the final solution
            //model.SetConsumerPrices(solution.Vector)
            //     .ShockProducerPrices()
            //     .CalculateMarketShares()
            //     .CalculateMarketEquilibrium();

            //// Print the results
            //PrintResults(model, solution);
        }