TomSym
Encyclopedia
The TomSym MATLAB
MATLAB
MATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...

 symbolic modeling engine is a platform for modeling applied optimization and optimal control problems.

Description

TomSym is complete modeling environment in Matlab with support for most built-in mathematical operators
Operation (mathematics)
The general operation as explained on this page should not be confused with the more specific operators on vector spaces. For a notion in elementary mathematics, see arithmetic operation....

 in Matlab. It is a combined modeling
Mathematical model
A mathematical model is a description of a system using mathematical concepts and language. The process of developing a mathematical model is termed mathematical modeling. Mathematical models are used not only in the natural sciences and engineering disciplines A mathematical model is a...

, compilation and interface to the TOMLAB
TOMLAB
The TOMLAB Optimization Environment is a modeling platform for solving applied optimization problems in MATLAB.-Description:TOMLAB is a general purpose development and modeling environment in MATLAB for research, teaching and practical solution of optimization problems...

 solvers. The matrix derivative
Matrix (mathematics)
In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions. The individual items in a matrix are called its elements or entries. An example of a matrix with six elements isMatrices of the same size can be added or subtracted element by element...

 of a matrix function is a fourth rank tensor
Tensor
Tensors are geometric objects that describe linear relations between vectors, scalars, and other tensors. Elementary examples include the dot product, the cross product, and linear maps. Vectors and scalars themselves are also tensors. A tensor can be represented as a multi-dimensional array of...

 - that is, a matrix each of whose entries is a matrix. Rather than using four-dimensional matrices to represent this, TomSym continues to work in two dimensions. This makes it possible to take advantage of the very efficient handling of sparse
Sparse matrix
In the subfield of numerical analysis, a sparse matrix is a matrix populated primarily with zeros . The term itself was coined by Harry M. Markowitz....

 matrices in Matlab, which is not available for higher-dimensional matrices.

TomSym has a variety of functions, among them:
  • Ability to transform expressions and generate analytical first and second order derivative
    Derivative
    In calculus, a branch of mathematics, the derivative is a measure of how a function changes as its input changes. Loosely speaking, a derivative can be thought of as how much one quantity is changing in response to changes in some other quantity; for example, the derivative of the position of a...

    s, including sparsity patterns.
  • Interfaced and compatible with MAD
    Automatic differentiation
    In mathematics and computer algebra, automatic differentiation , sometimes alternatively called algorithmic differentiation, is a set of techniques to numerically evaluate the derivative of a function specified by a computer program...

    , i.e. MAD can be used when symbolic modeling is not suitable.
  • Numerical differentiation can be used to parts of the model.
  • Functionality for plotting and computing a variety of information for the solution to the problem.
  • Support for if, then, else statements.
  • Ability to analyze p-coded
    P-Code machine
    In computer programming, a p-code machine, or portable code machine is a virtual machine designed to execute p-code...

     Matlab files.
  • Automated code simplification for generated models, for example.
    • Multiplication
      Multiplication
      Multiplication is the mathematical operation of scaling one number by another. It is one of the four basic operations in elementary arithmetic ....

       by 1 or the identity matrix
      Identity matrix
      In linear algebra, the identity matrix or unit matrix of size n is the n×n square matrix with ones on the main diagonal and zeros elsewhere. It is denoted by In, or simply by I if the size is immaterial or can be trivially determined by the context...

       is eliminated: 1*A = A
    • Addition
      Addition
      Addition is a mathematical operation that represents combining collections of objects together into a larger collection. It is signified by the plus sign . For example, in the picture on the right, there are 3 + 2 apples—meaning three apples and two other apples—which is the same as five apples....

      /subtraction
      Subtraction
      In arithmetic, subtraction is one of the four basic binary operations; it is the inverse of addition, meaning that if we start with any number and add any number and then subtract the same number we added, we return to the number we started with...

       of 0 is eliminated: 0+A = A
    • All-same matrices are reduced to scalars
      Scalar (mathematics)
      In linear algebra, real numbers are called scalars and relate to vectors in a vector space through the operation of scalar multiplication, in which a vector can be multiplied by a number to produce another vector....

      : [3;3;3]+x = 3+x
    • Scalars are moved to the left in addition/subtraction: A-y = -y+A
    • Inverse operations cancel: sqrt(x)^2 = x

Modeling

The TomSym symbolic source transformation makes it possible to define any the set of decision variables (both continuous
Continuous function
In mathematics, a continuous function is a function for which, intuitively, "small" changes in the input result in "small" changes in the output. Otherwise, a function is said to be "discontinuous". A continuous function with a continuous inverse function is called "bicontinuous".Continuity of...

 and integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...

) and any type of constraint as well as scalars and constant
Constant (mathematics)
In mathematics, a constant is a non-varying value, i.e. completely fixed or fixed in the context of use. The term usually occurs in opposition to variable In mathematics, a constant is a non-varying value, i.e. completely fixed or fixed in the context of use. The term usually occurs in opposition...

 parameters.

Linear programming

An example linear programming
Linear programming
Linear programming is a mathematical method for determining a way to achieve the best outcome in a given mathematical model for some list of requirements represented as linear relationships...

 problem would look like this:


c = [-7; -5];
A = [ 1 2
4 1 ];
b_U = [ 6; 12 ];
x_L = [ 0; 0 ];

toms 2x1 x

solution = ezsolve(c'*x, {A*x<=b_U, x_L<=x});

Mixed-integer nonlinear programming

A MINLP problem is defined just like a linear programming problem. This example also shows how to convert the model into a general TOMLAB problem.


Name='minlp1Demo - Kocis/Grossman.';

toms 2x1 x
toms 3x1 integer y

objective = [2 3 1.5 2 -0.5]*[x;y];

constraints = { ...
x(1) >= 0, ...
x(2) >= 1e-8, ...
x <= 1e8, ...
0 <= y <=1, ...
[1 0 1 0 0]*[x;y] <= 1.6, ...
1.333*x(2) + y(2) <= 3, ...
[-1 -1 1]*y <= 0, ...
x(1)^2+y(1)

1.25, ...
sqrt(x(2)^3)+1.5*y(2)

3, ...
};

guess = struct('x',ones(size(x)),'y',ones(size(y)));
options = struct;
options.name = Name;
Prob = sym2prob('minlp',objective,constraints,guess,options);

Prob.DUNDEE.optPar(20) = 1;
Result = tomRun('minlpBB',Prob,2);

Multi-index modeling

tomSym makes it possible to build models with two or more variable indices in MATLAB. The following example creates a variable 'flow' with four indices. The variable is then used to create a constraint over two of the indices and to sum the multiplication with a two-dimensional matrix.



% Create the indices used in model
i = tomArrayIdx('i',1:6);
j = tomArrayIdx('j',1:6);
k = tomArrayIdx('k',1:6);
l = tomArrayIdx('l',1:6);

% Create an integer variable of full length
flow = tom('flow',6^4,1,'int');

% Convert the variable to a matrix with four indices.
flow = tomArray(flow,[6,6,6,6]);

% Create a constraint valid for all i and j
cons = {sum(sum(flow(i,j,k,l),k),l) 1};

% Create a scalar based on multi-index multiplications
distance = tomArray([ 0 945 605 4667 4749 4394;...
945 0 866 3726 3806 3448;...
605 866 0 4471 4541 4152;...
4667 3726 4471 0 109 415;...
4749 3806 4541 109 0 431;...
4394 3448 4152 415 431 0]);

sumtotal = sum(vec((distance(i,k)+distance(l,j)+...
distance(k,l)*.8).*flow(i,j,k,l)));

Automatic and numerical differentiation

For functions that cannot be interpreted by tomSym it is possible to use either automatic differentiation
Automatic differentiation
In mathematics and computer algebra, automatic differentiation , sometimes alternatively called algorithmic differentiation, is a set of techniques to numerically evaluate the derivative of a function specified by a computer program...

or numerical differentiation. In the following example a simple problem is solved using the two methods.


toms x1 x2
alpha = 100;

% USE MAD (AUTOMATIC DIFFERENTIATION) FOR ONE FUNCTION
%
% Create a wrapper function. In this case we use sin, but it could be any
% MAD supported function.

y = wrap(struct('fun','sin','n',1,'sz1',1,'sz2',1,'JFuns','MAD'),x1/x2);
f = alpha*(x2-x1^2)^2 + (1-x1)^2 + y;

% Setup and solve the problem
c = -x1^2 - x2;
con = {-1000 <= c <= 0
-10 <= x1 <= 2
-10 <= x2 <= 2};

x0 = {x1 -1.2
x2 1};

solution1 = ezsolve(f,con,x0);

% USE NUMERICAL DIFFERENTIATION FOR ONE FUNCTIONS
% Create a new wrapper function. In this case we use sin, but it could be
% any function since we use numerical derivatives.

y = wrap(struct('fun','sin','n',1,'sz1',1,'sz2',1,'JFuns','FDJac'),x1/x2);
f = alpha*(x2-x1^2)^2 + (1-x1)^2 + y;

solution2 = ezsolve(f,con,x0);
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK