« Previous « Start » Next »
A TOMNETProblem - the problem description
The class
TOMNETProblem, is one
of the most central aspects of working with TOMNET. It contains
numerous members and methods.
A.1 Important Members and Methods
This section contains details about the base class
TOMNETProblem. This class is inherited to
the more specific classes:
-
LinearProblem (see 7.1).
- QuadraticProblem (see 7.2).
- NonlinearProblem (see 7.3).
- ConProblem (see 7.4).
- ConLinProblem (see 7.5).
- LinearLeastSquaresProblem (see 7.6).
- NonlinearLeastSquaresProblem (see 7.7).
The table
9 contains a list of the most important
members of
TOMNETProblem. To get familiar with how to use
these parameters consult the quickguide examples in
tomnet/quickguide .
A list of the most important methods are found in table
10.
Table 9: List of the most important members of
TOMNETProblem.
|
| Member |
Description |
|
| |
| A |
LinearConstraints object for linear constrains. |
| |
| b_L |
Lower bounds on the linear constraints. |
| b_U |
Upper bounds on the linear constraints. |
| |
| c_L |
Lower bounds on the nonlinear constraints. |
| c_U |
Upper bounds on the nonlinear constraints. |
| |
| ConsDiff |
Numerical approximation of the constraint
derivatives.
Constructors to Problems where ConsDiff
is applicable automatically sets
ConsDiff when appropriate.
See Section 8 for instructions
on how to force numerical differentiation
of constraints. |
| |
| N |
Problem dimension (number of variables). |
| |
| mLin |
Number of linear constraints. |
| |
| mNonlin |
Number of nonlinear constraints. |
| |
| Name |
Problem name. |
| |
| NumDiff |
Numerical approximation of the derivatives of the
objective function.
Constructors to Problems where NumDiff
is applicable automatically sets
NumDiff when appropriate.
See Section 8 for instructions
on how to force numerical differentiation
of the objective function. |
| |
| x_0 |
Starting point. |
| |
| x_L |
Lower bounds on the decision variables x. |
| x_U |
Upper bounds on the decision variables x. |
| |
| x_opt |
Stationary points x*, one per row (if known). It is possible
to define an extra column, in which a zero (0) indicates a minimum
point, a one (1) a saddle point, and a two (2) a maximum.
As default, minimum points are assumed.
The corresponding function values for each row in x_opt
should be given in Prob.f_opt. |
| |
|
Table 10: List of the most important methods of
TOMNETProblem.
|
| Method |
Description |
|
| |
| Constructor |
All classes that inherit TOMNETProblem have appropriate
Constructors that can be used to create instances of the
specific class. |
| |
| f |
Method for evaluating the objective function. |
| |
| g |
Method for evaluating the gradient of the objective function.
If numerical differentiation is used g is never called. |
| |
| c |
Method for evaluating the nonlinear constraints. |
| |
| dc |
Method for evaluating the Jacobian (gradients of the nonlinear constraints.)
If numerical differentiation is used dc is never called. |
| |
A.2 Creating a Problem.
In order to implement a custom
TOMNETProblem there are a few
items to consider:
-
Find the TOMNETProblem class with the best problem fit.
- Implement the objective function (using an IFunction or an IDFunction).
- Implement the nonlinear constraints (using an IConstraints or an IDConstraints).
- Create objects needed to call the constructor.
- Solve using an appropriate TOMNETSolver.
In this section we will create a problem with a custom nonlinear objective function,
and two nonlinear constraints. This example is included in the TOMNET distribution, in
(usersguide/CreateProblem/CreateProblem.cs).
A.2.1 Find the TOMNETProblem class that matched the problem.
Suppose we want to model the below problem:
|
|
|
f(x) = x[0] + x[1] x[2] − x[3]2.37 |
| |
|
| s/t |
| 0.1 |
≤ |
x[i] |
≤ |
100, for i = 0, 1, 2, 3 |
| −10 |
≤ |
x[0] + x[1] |
|
|
| −10 |
≤ |
x[1] + x[2] |
|
|
| −10 |
≤ |
x[2] + x[3] |
|
|
| −10 |
≤ |
x[3] + x[0] |
|
|
| −100 |
≤ |
x[0] x[1] x[2] x[3] − x[0]4 |
≤ |
1000 |
| 0.0 |
≤ |
x[0]2+x[1]2+x[2]2+x[3]2 |
≤ |
1000 |
|
|
(10) |
where
x,
xL,
xU
Rn,
f(
x)
R,
A
Rm1 × n,
bL,
bU
Rm1 and
cL,
c(
x),
cU
Rm2.
It is clear that this problem has linear and nonlinear constraints and
a nonlinear objective function. The
ConLinProblem
(see
7.5) is the most suitable class.
A.2.2 Implement the objective function
We must create the objective function and the gradient. In the
following example an
IDFunction (see
7.9) is
implemented. (Note that the parameter
fx is an array - this is
for compliance with problems like
LinearConstraintsProblem,
For regular problems this is an array of length 1.)
class myFunc : IDFunction
{
public void Evaluate(double[] fx, double[] x)
{
fx[0] = x[0] + x[1] * x[2] - Math.Pow(x[3], 2.37);
}
public void Grad(double[] gx, double[] x)
{
gx[0] = 1;
gx[1] = x[2];
gx[2] = x[1];
gx[3] = -2.37 * Math.Pow(x[3], 1.37);
}
}
A.2.3 Implement the nonlinear constraints
It is always best to separate the linear and nonlinear constraints
since this improves the performance of most solvers. The linear
constraints will be dealt with in the constructor and two nonlinear
constraints remains to be implemented using an
IDConstraints
(see
7.11) or an
IConstraints (see
7.10).
class myCons : IDConstraints
{
public void Evaluate(double[] cx, double[] x)
{
cx[0] = x[0] * x[1] * x[2] * x[3] - Math.Pow(x[0], 4.0);
cx[1] = Math.Pow(x[0], 2.0) +
Math.Pow(x[1], 2.0) +
Math.Pow(x[2], 2.0) +
Math.Pow(x[3], 2.0);
}
public double[] Evaluate(double[] x)
{
double[] cx = new double[2];
this.Evaluate(cx, x);
return cx;
}
public void dc(Jacobian dcx, double[] x)
{
dcx[0, 0] = x[1] * x[2] * x[3] - 4 * Math.Pow(x[0], 3.0);
dcx[0, 1] = x[0] * x[2] * x[3];
dcx[0, 2] = x[0] * x[1] * x[3];
dcx[0, 3] = x[0] * x[1] * x[2];
dcx[1, 0] = 2 * x[0];
dcx[1, 1] = 2 * x[1];
dcx[1, 2] = 2 * x[2];
dcx[1, 3] = 2 * x[3];
}
}
A.2.4 Create objects needed to call the constructor.
In this small example it is suitable to create all arrays, the
linear constraints and set a name to use in the constructor call.
static void Main(string[] args)
{
//
// Name of the problem
//
string Name = "my problem";
//
// Bounds on x and initial point.
//
double[] x_L = new double[4] { 0.1, 0.1, 0.1, 0.1 };
double[] x_U = new double[4] { 100.0, 100.0, 100.0, 100.0 };
double[] x_0 = new double[4] { 10.0, 10.0, 10.0, 10.0 };
//
// Linear constraints
//
int mLin = 4;
int nnz = 8;
int[] ColIdx = new int[8] { 1, 2, 2, 3, 3, 4, 4, 1 };
int[] RowIdx = new int[8] { 1, 1, 2, 2, 3, 3, 4, 4 };
double[] Values = new double[8] { 1, 1, 1, 1, 1, 1, 1, 1 };
Sparse A = new Sparse(mLin, 4, nnz, RowIdx, ColIdx, Values, 0);
//
// Bounds on linear constraints
//
double Inf = double.PositiveInfinity;
double[] b_L = new double[4] { -10.0, -10.0, -10.0, -10.0 };
double[] b_U = new double[4] { Inf, Inf, Inf, Inf };
//
// Create instances of the
// objective function and the nonlinear constraints
//
myFunc F = new myFunc();
myCons C = new myCons();
//
// Bounds on nonlinear constraints
//
double[] c_L = new double[2] { -100, 0.0 };
double[] c_U = new double[2] { 1000, 1000 };
//
// Call constructor
//
ConLinProblem Prob = new ConLinProblem(F, A, b_L, b_U,
C, c_L, c_U, x_L, x_U, x_0, Name, double.NegativeInfinity,
null, null);
//
// The problem is created
//
}
A.2.5 Solve using a TOMNETSolver.
In order to solve a
TOMNETProblem we always need three
objects: the problem, a solver and a result. Above we created a
TOMNETProblem, now we need to add a solver, a result and solve the
problem. By adding the following three lines of code we achieve
this:
NPSOL solver = new NPSOL();
Result result;
solver.Solve(Prob, out result);
To display the solution on the console - we also add the following:
Console.WriteLine(" * Solved '{0}'", Prob.Name);
Console.WriteLine(" * Objective: {0}", result.f_k);
Console.WriteLine(" * Solution");
Console.WriteLine(" x_L[i] <= x_k[i] <= x_U[i]");
for (int i = 0; i < Prob.N; i++)
{
Console.WriteLine(" {0,10:g3} <= {1,10:g3} <= {2,10:g3} ",
Prob.x_L[i], result.x_k[i], Prob.x_U[i]);
}
The output from this problem should be as follows.
Creating my problem.
* Solved my problem
* Objective: -3588.98175192509
* Solution
x_L[i] <= x_k[i] <= x_U[i]
0.1 <= 0.1 <= 100
0.1 <= 0.1 <= 100
0.1 <= 0.1 <= 100
0.1 <= 31.6 <= 100
« Previous « Start » Next »