/// <summary> /// Defines attributes on the element using a data file. /// </summary> /// <param name="element">The element to which attributes are added.</param> /// <param name="dataFile">A <see cref="DelimitedFilePath"/> with headers and double values.</param> /// <param name="index">The row index of the data (where the header row == -1) for this element.</param> /// <returns>A reference to the existing <see cref="XElement"/>. This is returned for use with fluent syntax calls.</returns> public static XElement DefineAttributeData(this XElement element, DelimitedFilePath dataFile, int index) { IDictionary <string, double[]> data = dataFile.ReadData(); foreach (string header in data.Keys) { element.SetAttributeValue(header, data[header][index]); } return(element); }
/// <summary> /// Defines attributes on the root and descendants using a data file. /// </summary> /// <param name="root">The root element.</param> /// <param name="dataFile">A <see cref="DelimitedFilePath"/> with headers and double values.</param> /// <returns>A reference to the existing <see cref="XElement"/>. This is returned for use with fluent syntax calls.</returns> public static XElement DefineAttributeData(this XElement root, DelimitedFilePath dataFile) { IDictionary <string, double[]> data = dataFile.ReadData(); XElement[] elements = root.DescendantsAndSelf().ToArray(); foreach (string header in data.Keys) { for (int i = 0; i < elements.Length; i++) { elements[i].SetAttributeValue(header, data[header][i]); } } return(root); }
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); }