# commons-user mailing list archives

##### Site index · List index
Message view
Top
From Thomas Neidhart <thomas.neidh...@gmail.com>
Subject Re: org.apache.commons.math3.optim
Date Wed, 31 Jul 2013 08:00:36 GMT
```Dear Esther,

there is no good starting example in the user guide yet (we should add
that!) but you can find some examples in the unit tests.
In general, the OptimizationData interface is just a marker interface for
all kinds of parameters an optimizer may need or support.
It is a bit tricky to figure out what kind of data a specific optimizer, in
this case a linear one, needs, but we are working on improving this API for
the next release.

In the case of a simplex solver, you want to provide the following
parameters:

* the objective function (mandatory)
* the constraints (mandatory)
* the goal type (maximize or minimize, optional)
* the maximum number of iterations (optional)
* whether to limit the results to be non-negative (optional)

A simple example would look like this:

LinearObjectiveFunction f = new LinearObjectiveFunction(new
double[] { 2, 2, 1 }, 0);
Collection<LinearConstraint> constraints = new
ArrayList<LinearConstraint>();
constraints.add(new LinearConstraint(new double[] { 1, 1, 0 },
Relationship.GEQ,  1));
constraints.add(new LinearConstraint(new double[] { 1, 0, 1 },
Relationship.GEQ,  1));
constraints.add(new LinearConstraint(new double[] { 0, 1, 0 },
Relationship.GEQ,  1));

SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(new MaxIter(100), f, new
LinearConstraintSet(constraints),
GoalType.MINIMIZE, new
NonNegativeConstraint(true));

This would minimize the following linear problem:

f = 2*x2 + 2*x1 + x0

with

x2 + x1 >= 1
x2 + x0 >= 1
x1 >= 1

limiting the values of x0 .. x2 to be non-negative and stop the
optimization after 100 iterations if no optimal solution has been found so
far.

Best regards,

Thomas

On Wed, Jul 31, 2013 at 2:29 AM, Esther Camilo <esthercamilo@gmail.com>wrote:

> Dear users,
>
> I am trying to solve a optimization problem. I found a "Hello World"
> example for Commons Math 2. But, I would like to use Commons Math 3.2
> and I couldn't find any example on how to use this part of the code:
>
> PointValuePair solution = null;
> SimplexSolver solver = new SimplexSolver();
> solution = solver.optimize(optData);
>
> Specificaly, I don't know what is optData and where I put the
> constraints. I would appreciate if someone indicate me one "Hello
> World" example of how to use the org.apache.commons.math3.optim
> library
>
> Best wishes!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

```
Mime
• Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message