Esempio n. 1
0
        /// <summary>
        /// Reads a problem from a stream.
        /// </summary>
        /// <param name="stream">Stream to read from</param>
        /// <returns>The problem</returns>
        public static Problem Read(Stream stream)
        {
            TemporaryCulture.Start();

            StreamReader  input     = new StreamReader(stream);
            List <double> vy        = new List <double>();
            List <Node[]> vx        = new List <Node[]>();
            int           max_index = 0;

            while (input.Peek() > -1)
            {
                string[] parts = input.ReadLine().Trim().Split();

                vy.Add(double.Parse(parts[0]));
                int    m = parts.Length - 1;
                Node[] x = new Node[m];
                for (int j = 0; j < m; j++)
                {
                    x[j] = new Node();
                    string[] nodeParts = parts[j + 1].Split(':');
                    x[j].Dim   = int.Parse(nodeParts[0]);
                    x[j].Value = double.Parse(nodeParts[1]);
                }
                if (m > 0)
                {
                    max_index = Math.Max(max_index, x[m - 1].Dim);
                }
                vx.Add(x);
            }

            TemporaryCulture.Stop();

            return(new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index));
        }
Esempio n. 2
0
        /// <summary>
        /// Writes a problem to a stream.
        /// </summary>
        /// <param name="stream">The stream to write the problem to.</param>
        /// <param name="problem">The problem to write.</param>
        public static void Write(Stream stream, Problem problem)
        {
            TemporaryCulture.Start();

            StreamWriter output = new StreamWriter(stream);

            for (int i = 0; i < problem.N; i++)
            {
                output.Write(problem.Y[i]);
                for (int j = 0; j < problem.X[i].Length; j++)
                {
                    output.Write(" {0}:{1}", problem.X[i][j].Dim, problem.X[i][j].Value);
                }
                output.WriteLine();
            }
            output.Flush();

            TemporaryCulture.Stop();
        }