# CompactQuasiNewtonSolver.Solve Method

Solve a model using the specified parameters.

Namespace:  Microsoft.SolverFoundation.Solvers
Assembly:  Microsoft.Solver.Foundation (in Microsoft.Solver.Foundation.dll)

## Syntax

``````'Declaration
Public Function Solve ( _
solverParams As CompactQuasiNewtonSolverParams _
) As CompactQuasiNewtonSolutionQuality
``````
``````public CompactQuasiNewtonSolutionQuality Solve(
CompactQuasiNewtonSolverParams solverParams
)
``````
``````public:
CompactQuasiNewtonSolutionQuality Solve(
CompactQuasiNewtonSolverParams^ solverParams
)
``````
``````member Solve :
solverParams:CompactQuasiNewtonSolverParams -> CompactQuasiNewtonSolutionQuality
``````
``````public function Solve(
solverParams : CompactQuasiNewtonSolverParams
) : CompactQuasiNewtonSolutionQuality
``````

#### Return Value

Type: Microsoft.SolverFoundation.Solvers.CompactQuasiNewtonSolutionQuality
The solution quality.

## Examples

The following code example demonstrates how to use the compact quasi-Newton solver to solve a multi-dimensional variant of Rosenbrock's function. For more examples, see Samples for Solver Foundation.

``````Private Shared Sub SolveSecondMultidimensionalVariant(ByVal dimentions As Integer)
Dim solverParams = New CompactQuasiNewtonSolverParams()
Dim solver = New CompactQuasiNewtonSolver(dimentions)
Dim solutionQuality As CompactQuasiNewtonSolutionQuality = solver.Solve(solverParams)
Console.WriteLine("=========Second, more complicated multidimensional variant of Rosenbrock==========")
Console.WriteLine(solver.ToString())

solver.SetStartingPointValue(0, -1)
For i As Integer = 1 To dimentions - 1
solver.SetStartingPointValue(i, 1)
Next
solutionQuality = solver.Solve(solverParams)
Console.WriteLine("=========Second, more complicated multidimensional variant of Rosenbrock, trapped in local Minima==========")
Console.WriteLine(solver.ToString())
End Sub

Private Shared Function SecondRosenbrockVariantCallback(ByVal point As Double(), ByVal gradient As Double()) As Double
Dim dimentions As Integer = gradient.Length
If dimentions < 2 Then
Throw New ArgumentException("Multidimensional variant require at least two dimensions")
End If
' common alpha is 100
Dim alpha As Integer = 100
' first dimention special case
Dim value As Double = alpha * (Math.Pow((point(1) - point(0) * point(0)), 2)) + (Math.Pow(1 - point(0), 2))
gradient(0) = 4 * alpha * point(0) * (point(0) * point(0) - point(1)) + 2 * (point(0) - 1)
For i As Integer = 1 To gradient.Length - 2
value += alpha * (Math.Pow((point(i + 1) - point(i) * point(i)), 2)) + (Math.Pow(1 - point(i), 2))
gradient(i) = -2 * alpha * (point(i - 1) * point(i - 1) - point(i)) + 4 * alpha * point(i) * (point(i) * point(i) - point(i + 1)) + 2 * (point(i) - 1)
Next
' last dimention special case
gradient(dimentions - 1) = -2 * alpha * (point(dimentions - 2) * point(dimentions - 2) - point(dimentions - 1))

Return value
End Function
``````
``````static void SolveSecondMultidimensionalVariant(int dimentions) {
var solverParams =
new CompactQuasiNewtonSolverParams();
var solver =
new CompactQuasiNewtonSolver(dimentions);
SecondRosenbrockVariantCallback;
CompactQuasiNewtonSolutionQuality solutionQuality =
solver.Solve(solverParams);
Console.WriteLine("=========Second, more complicated multidimensional variant of Rosenbrock==========");
Console.WriteLine(solver.ToString());

solver.SetStartingPointValue(0, -1);
for (int i = 1; i < dimentions; i++)
solver.SetStartingPointValue(i, 1);
solutionQuality = solver.Solve(solverParams);
Console.WriteLine("=========Second, more complicated multidimensional variant of Rosenbrock, trapped in local Minima==========");
Console.WriteLine(solver.ToString());
}

static double SecondRosenbrockVariantCallback(
if (dimentions < 2)
throw new ArgumentException("Multidimensional variant require at least two dimensions");
// common alpha is 100
int alpha = 100;
// first dimention special case
double value =
alpha * (Math.Pow((point[1] - point[0] * point[0]), 2)) +
(Math.Pow(1 - point[0], 2));
4 * alpha * point[0] * (point[0] * point[0] - point[1]) +
2 * (point[0] - 1);
for (int i = 1; i < gradient.Length - 1; i++) {
value +=
alpha * (Math.Pow((point[i + 1] - point[i] * point[i]), 2)) +
(Math.Pow(1 - point[i], 2));
-2 * alpha * (point[i - 1] * point[i - 1] - point[i]) +
4 * alpha * point[i] * (point[i] * point[i] - point[i + 1]) +
2 * (point[i] - 1);
}
// last dimention special case
-2 * alpha * (point[dimentions - 2] * point[dimentions - 2] -
point[dimentions - 1]);

return value;
}
``````