예제 #1
0
        private static SetInformation[] BuildAllSets([NotNull] HeaderArrayFile arrayFile)
        {
            int[]    sizes         = arrayFile["SSZ "].As <int>().Values.ToArray();
            char[]   intertemporal = arrayFile["STTP"].As <char>().Values.ToArray();
            string[] names         = arrayFile["STNM"].As <string>().Values.ToArray();
            string[] descriptions  = arrayFile["STLB"].As <string>().Values.ToArray();
            string[] elements      = arrayFile["STEL"].As <string>().Values.ToArray();

            SetInformation[] result = new SetInformation[names.Length];

            int counter = 0;

            for (int i = 0; i < names.Length; i++)
            {
                result[i] =
                    new SetInformation(
                        names[i],
                        descriptions[i],
                        intertemporal[i] == 'i',
                        sizes[i],
                        new ArraySegment <string>(elements, counter, sizes[i]));

                counter += sizes[i];
            }

            return(result);
        }
예제 #2
0
        private static ParallelQuery <SolutionArray> BuildSolutionArrays([NotNull] HeaderArrayFile arrayFile)
        {
            int[]    numberOfSets  = arrayFile["VCNI"].As <int>().Values.ToArray();
            string[] names         = arrayFile["VCNM"].As <string>().Values.ToArray();
            string[] descriptions  = arrayFile["VCL0"].As <string>().Values.ToArray();
            string[] unitTypes     = arrayFile["VCLE"].As <string>().Values.ToArray();
            char[]   changeTypes   = arrayFile["VCT0"].As <char>().Values.ToArray();
            char[]   variableTypes = arrayFile["VCS0"].As <char>().Values.ToArray();

            KeyValuePair <string, IImmutableList <string> >[][] sets = VariableIndexedCollectionsOfSets(arrayFile);

            return
                (ParallelEnumerable.Range(0, names.Length)
                 .Select(
                     x =>
                     new SolutionArray(
                         x,
                         numberOfSets[x],
                         names[x],
                         descriptions[x],
                         unitTypes[x],
                         (ModelChangeType)changeTypes[x],
                         (ModelVariableType)variableTypes[x],
                         sets[x])));
        }
예제 #3
0
        /// <summary>
        /// Executes the selected tests.
        /// </summary>
        /// <param name="input">
        /// The input file.
        /// </param>
        /// <param name="binaryOutput">
        /// The binary output file.
        /// </param>
        /// <param name="jsonOutput">
        /// The json output file.
        /// </param>
        /// <param name="option">
        /// The bit flags indicating which tests to perform.
        /// </param>
        private static void Test(string input, string binaryOutput, string jsonOutput, TestOptions option)
        {
            Console.WriteLine($"Reading {nameof(input)} with {nameof(HeaderArrayFile.Read)} at {DateTime.Now}.");

            HeaderArrayFile arrays = HeaderArrayFile.Read(input);

            if (option.HasFlag(TestOptions.WriteBinary))
            {
                Console.WriteLine($"Writing {nameof(arrays)} to {nameof(binaryOutput)} with {nameof(HeaderArrayFile.BinaryWriter)} at {DateTime.Now}.");
                HeaderArrayFile.BinaryWriter.Write(binaryOutput, arrays);
            }

            if (option.HasFlag(TestOptions.WriteJson))
            {
                Console.WriteLine($"Writing {nameof(arrays)} to {nameof(jsonOutput)} with {nameof(HeaderArrayFile.JsonWriter)} at {DateTime.Now}.");
                HeaderArrayFile.JsonWriter.Write(jsonOutput, arrays);
            }

            if (option.HasFlag(TestOptions.ReadJson))
            {
                Console.WriteLine($"Reading {nameof(jsonOutput)} with {nameof(HeaderArrayFile.JsonReader)} at {DateTime.Now}.");
                HeaderArrayFile.JsonReader.Read(jsonOutput);
            }

            if (option.HasFlag(TestOptions.ValidateSets))
            {
                Console.WriteLine($"Running {nameof(HeaderArray.ValidateSets)} on {nameof(arrays)} at {DateTime.Now}.");
                arrays.ValidateSets(Console.Out);
            }

            Console.WriteLine($"Completed test at {DateTime.Now}");
        }
예제 #4
0
        /// <summary>
        /// Program entry point.
        /// </summary>
        public static void Main()
        {
            Console.WriteLine($"Reading at {DateTime.Now}.");

            HeaderArrayFile arrays = HeaderArrayFile.Read($"{Directory.GetCurrentDirectory()}\\basedata.har");

            Console.WriteLine($"Writing {nameof(arrays)} with {nameof(HeaderArrayFile.JsonWriter)} at {DateTime.Now}.");

            HeaderArrayFile.JsonWriter.Write($"{Directory.GetCurrentDirectory()}\\basedata.harx", arrays);

//            Test(Input[1], Output[0], Output[1], TestOptions.All);
//
//            Console.WriteLine(HeaderArray<float>.JsonSchema);
//
//            Console.ReadLine();
        }
예제 #5
0
        private static KeyValuePair <string, IImmutableList <string> >[][] VariableIndexedCollectionsOfSets([NotNull] HeaderArrayFile arrayFile)
        {
            SetInformation[] setInformation  = BuildAllSets(arrayFile).ToArray();
            int[]            pointerIntoVcsn = arrayFile["VCSP"].As <int>().Values.ToArray();
            int[]            setsPerVariable = arrayFile["VCNI"].As <int>().Values.ToArray();
            int[]            setPositions    = arrayFile["VCSN"].As <int>().Values.ToArray();

            KeyValuePair <string, IImmutableList <string> >[][] arraySetInfo = new KeyValuePair <string, IImmutableList <string> > [pointerIntoVcsn.Length][];

            for (int i = 0; i < pointerIntoVcsn.Length; i++)
            {
                arraySetInfo[i] = new KeyValuePair <string, IImmutableList <string> > [setsPerVariable[i]];

                int pointer = pointerIntoVcsn[i] - 1;

                for (int j = 0; j < arraySetInfo[i].Length; j++)
                {
                    SetInformation set = setInformation[setPositions[pointer + j] - 1];

                    arraySetInfo[i][j] = new KeyValuePair <string, IImmutableList <string> >(set.Name, set.Elements);
                }
            }

            return(arraySetInfo);
        }
예제 #6
0
        private IEnumerable <IHeaderArray> BuildHeaderArrays([NotNull] FilePath file)
        {
            HeaderArrayFile arrayFile = HeaderArrayFile.BinaryReader.Read(file);

            // Variable information.
            int[] countOfComponentsInVariable = arrayFile["VNCP"].As <int>().Values.ToArray();

            // Endogenous variable components and information.
            int[]   pointersToCumulative = arrayFile["PCUM"].As <int>().Values.ToArray();
            int[]   countInCumulative    = arrayFile["CMND"].As <int>().Values.ToArray();
            float[] cumulativeResults    = arrayFile["CUMS"].As <float>().Values.ToArray();

            // Exogenous variable components and list of positions (where OREX != array.Length).
            int[] countOfExogenous     = arrayFile["OREX"].As <int>().Values.ToArray();
            int[] positionsOfExogenous = arrayFile["OREL"].As <int>().Values.ToArray();

            // Shocked variable information
            int[]   numberOfShockedComponents = arrayFile["SHCK"].As <int>().Values.ToArray();
            int[]   pointersToShockValues     = arrayFile["PSHK"].As <int>().Values.ToArray();
            int[]   positionsOfShockValues    = arrayFile["SHCL"].As <int>().Values.ToArray();
            float[] shockValues = arrayFile["SHOC"].As <float>().Values.ToArray();

            return
                (BuildSolutionArrays(arrayFile).Where(x => x.IsBacksolvedOrCondensed)
                 .OrderBy(x => x.VariableIndex)
                 .Select(BuildNextArray));

            // Local method here to limit passing arrays as parameters.
            IHeaderArray BuildNextArray(SolutionArray array, int index)
            {
                float[] values = new float[array.Count];

                // When the array is condensed/backsolved and the pointer is empty, its exogenous.
                int pointer = pointersToCumulative[index] - 1;

                if (pointer != -1)
                {
                    Array.Copy(cumulativeResults, pointer, values, 0, countInCumulative[index]);
                }

                ShiftExogenous(values);

                Shocks(values);

                return
                    (HeaderArray <float> .Create(
                         array.Name,
                         array.Name,
                         array.Description,
                         HeaderArrayType.RE,
                         array.Sets.Select(x => x.Value.Count).Concat(Enumerable.Repeat(1, 7)).Take(7),
                         values,
                         array.Sets));

                // Shifts existing entries to their appropriate positions to create gaps for exogenous values.
                void ShiftExogenous(float[] inputArray)
                {
                    if (array.Count == countOfExogenous[index])
                    {
                        Array.Clear(inputArray, 0, inputArray.Length);
                        return;
                    }

                    float[] withGaps = inputArray;

                    int nextValidPosition = 0;

                    for (int i = 0; i < index; i++)
                    {
                        if (countOfExogenous[i] != countOfComponentsInVariable[i])
                        {
                            nextValidPosition += countOfExogenous[i];
                        }
                    }

                    for (int i = 0; i < countOfExogenous[index]; i++)
                    {
                        int position = positionsOfExogenous[nextValidPosition + i] - 1;
                        Array.Copy(withGaps, position, withGaps, position + 1, withGaps.Length - position - 1);
                        withGaps[position] = default(float);
                    }
                }

                // Adds shocks to open positions to a copy of the input array.
                void Shocks(float[] inputArray)
                {
                    float[] withShocks = inputArray;

                    int shockedCount = numberOfShockedComponents[index];

                    if (shockedCount == 0)
                    {
                        return;
                    }

                    int shockPointer = pointersToShockValues[index] - 1;

                    int nextValidPosition = 0;

                    for (int i = 0; i < index - 1; i++)
                    {
                        if (numberOfShockedComponents[i] != countOfComponentsInVariable[i])
                        {
                            nextValidPosition += numberOfShockedComponents[i];
                        }
                    }

                    for (int i = 0; i < shockedCount; i++)
                    {
                        int   position = array.Count == shockedCount ? i : positionsOfShockValues[nextValidPosition + i] - 1;
                        float value    = shockValues[shockPointer + i];
                        withShocks[position] = value;
                    }
                }
            }
        }