# TOMNET  
# REGISTER (TOMNET)
# LOGIN  
# myTOMNET
TOMLAB LOGO

« Previous « Start » Next »

7  LLS Problem

The linear least squares (lls) problem is defined as
 
min
x
f(x) =
1
2
|| C xd ||
   
s/t
xL x xU,
bL A x bU
    (9)
where x, xL, xU ∈ Rn, d ∈ RM, C ∈ RM × n, A ∈ Rm1 × n and bL,bU ∈ Rm1.

The following code defines and solves a problem in TOMNET.
using System;
using TOMNET;

namespace TOMNET
{
  /// <summary>
  /// Quick guide class for linear least squares problem.
  /// </summary>
  public class llsQG
  {

    /// <summary>
    /// Testprogram for llsQG.
    /// </summary>
    static void Main()
    {
      //
      // For use in bounds.
      //
      double Inf = double.PositiveInfinity;

      //
      // Number of decision variables, linear constraints
      // and observations
      //
      const int n = 9;
      const int mLin = 3;
      const int k = 10;

      //
      // Lower and upper bounds for the decision variables
      //
      double[] x_L = new double[n] { -2, -2, -Inf, -2, -2, -2, -2, -2,
        -2 };
      double[] x_U = new double[n] { 2, 2, 2, 2, 2, 2, 2, 2, 2 };

      //
      // Matrix defining linear constraints
      //
      double[] A = new double[n * mLin]
      { 1,  1,  1,  1,  1,  1,  1,  1,  4,
        1,  2,  3,  4, -2,  1,  1,  1,  1,
        1, -1,  1, -1,  1,  1,  1,  1,  1};

      //
      // Lower and upper bounds on the linear constraints
      //
      double[] b_L = new double[mLin] {   2, -Inf, -4 };
      double[] b_U = new double[mLin] { Inf,   -2, -2 };

      //
      // Vector k x 1 with observations in objective ||Cx - d||
      //
      double[] d = new double[k] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

      //
      // Matrix m x n in objective ||Cx - d||
      //
      double[] C = new double[90]
      { 1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  2,  1,  1,  1,  1,  2,  0,  0,
        1,  1,  3,  1,  1,  1, -1, -1, -3,
        1,  1,  1,  4,  1,  1,  1,  1,  1,
        1,  1,  1,  3,  1,  1,  1,  1,  1,
        1,  1,  2,  1,  1,  0,  0,  0, -1,
        1,  1,  1,  1,  0,  1,  1,  1,  1,
        1,  1,  1,  0,  1,  1,  1,  1,  1,
        1,  1,  0,  1,  1,  1,  2,  2,  3,
        1,  0,  1,  1,  1,  1,  0,  2,  2};

      //
      // Starting point.
      //
      double[] x_0 = new double[n];
      for (int i = 0; i < n; i++)
      {
        x_0[i] = 1.0 / i;
      }

      //
      // Creating the problem, solver and result
      //
      LinearLeastSquaresProblem Prob = new LinearLeastSquaresProblem(
        n, mLin, k, C, d, A, b_L, b_U, x_0, x_L, x_U);
      SNOPT solver = new SNOPT();
      Result result;

      //
      // Setting a print file for SNOPT
      //
      string printfilename = "SnoptllsQG.txt";
      solver.Options.PrintFile = printfilename;
      Prob.Name = "llsQG";

      //
      // Calling Solve to get a Result object
      //
      solver.Solve(Prob, out result);

      //
      // Extract solution and objective from result
      //
      double[] solution = result.x_k;
      double objective = result.f_k;

      //
      // Display the solution on the console.
      //
      Console.WriteLine(" * * * * * * * *");
      Console.WriteLine(" * Solved " + Prob.Name);
      Console.WriteLine(" * Printfile: " + printfilename);
      Console.WriteLine(" * Objective: {0,10:g8}", objective);
      Console.WriteLine(" * Solution");
      Console.WriteLine("        x_L[i] <=     x_k[i] <=     x_U[i]");
      for (int i = 0; i < solution.Length; i++)
      {
        Console.WriteLine("   {0,10:g3} <= {1,10:g3} <= {2,10:g3} ",
          Prob.x_L[i], solution[i], Prob.x_U[i]);
      }

      //
      // Print linear constraints
      //
      Console.WriteLine(" * Linear constraints");
      Console.WriteLine("        b_L[i] <=   (A*x)[i] <=     b_U[i]");
      double[] Ax = Prob.A.Evaluate(result.x_k);
      for (int i = 0; i < Prob.mLin; i++)
      {
        Console.WriteLine("   {0,10:g3} <= {1,10:g3} <= {2,10:g3}",
          Prob.A.GetLowerBound(i), Ax[i], Prob.A.GetUpperBound(i));
      }
    }
  }
}
The following picture shows the correct output results:

« Previous « Start » Next »