|
Java example source code file (optimization.xml)
The optimization.xml Java example source code<?xml version="1.0"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?> <document url="optimization.html"> <properties> <title>The Commons Math User Guide - Optimization </properties> <body> <section name="12 Optimization"> <p>The contents of this section currently describes deprecated classes. Please refer to the new <a href="../apidocs/org/apache/commons/math3/optim/package-summary.html">API description</a>. </p> <p>Least squares optimizers are not in this package anymore, they have been moved in a dedicated least-squares sub-package described in the <a href="./leastsquares.html">least squares section. </p> <subsection name="12.1 Overview" href="overview"> <p> The optimization package provides algorithms to optimize (i.e. either minimize or maximize) some objective or cost function. The package is split in several sub-packages dedicated to different kind of functions or algorithms. <ul> <li>the univariate package handles univariate scalar functions, <li>the linear package handles multivariate vector linear functions with linear constraints,</li> <li>the direct package handles multivariate scalar functions using direct search methods (i.e. not using derivatives),</li> <li>the general package handles multivariate scalar or vector functions using derivatives.</li> <li>the fitting package handles curve fitting by univariate real functions </ul> </p> <p> The top level optimization package provides common interfaces for the optimization algorithms provided in sub-packages. The main interfaces defines defines optimizers and convergence checkers. The functions that are optimized by the algorithms provided by this package and its sub-packages are a subset of the one defined in the <code>analysis package, namely the real and vector valued functions. These functions are called objective function here. When the goal is to minimize, the functions are often called cost function, this name is not used in this package. </p> <p> The type of goal, i.e. minimization or maximization, is defined by the enumerated <a href="../apidocs/org/apache/commons/math3/optimization/GoalType.html"> GoalType</a> which has only two values: |
<td valign="bottom" align="center" style=" font-size:10pt;">Y
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">1
<td valign="bottom" align="center" style=" font-size:10pt;">34.234064369
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">2
<td valign="bottom" align="center" style=" font-size:10pt;">68.2681162306108
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">3
<td valign="bottom" align="center" style=" font-size:10pt;">118.615899084602
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">4
<td valign="bottom" align="center" style=" font-size:10pt;">184.138197238557
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">5
<td valign="bottom" align="center" style=" font-size:10pt;">266.599877916276
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">6
<td valign="bottom" align="center" style=" font-size:10pt;">364.147735251579
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">7
<td valign="bottom" align="center" style=" font-size:10pt;">478.019226091914
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">8
<td valign="bottom" align="center" style=" font-size:10pt;">608.140949270688
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">9
<td valign="bottom" align="center" style=" font-size:10pt;">754.598868667148
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">10
<td valign="bottom" align="center" style=" font-size:10pt;">916.128818085883
</tr>
</table>
<p>
First we need to implement the interface <a href="../apidocs/org/apache/commons/math3/analysis/DifferentiableMultivariateVectorFunction.html">DifferentiableMultivariateVectorFunction.
This requires the implementation of the method signatures:
</p>
<ul>
<li>MultivariateMatrixFunction jacobian()
<li>double[] value(double[] point)
</ul>
<p>
We'll tackle the implementation of the <code>MultivariateMatrixFunction jacobian() method first. You may wish to familiarize yourself with what a Jacobian Matrix is.
In this case the Jacobian is the partial derivative of the function with respect
to the parameters a, b and c. These derivatives are computed as follows:
<ul>
<li>d(ax2 + bx + c)/da = x2
<li>d(ax2 + bx + c)/db = x
<li>d(ax2 + bx + c)/dc = 1
</ul>
</p>
<p>
For a quadratic which has three variables the Jacobian Matrix will have three columns, one for each variable, and the number
of rows will equal the number of rows in our data set, which in this case is ten. So for example for <tt>[a = 1, b = 1, c = 1], the Jacobian Matrix is (excluding the first column which shows the value of x):
</p>
<table cellspacing="0" cellpadding="3">
<tr>
<td valign="bottom" align="left" style=" font-size:10pt;">x
<td valign="bottom" align="left" style=" font-size:10pt;">d(ax2 + bx + c)/da
<td valign="bottom" align="left" style=" font-size:10pt;">d(ax2 + bx + c)/db
<td valign="bottom" align="left" style=" font-size:10pt;">d(ax2 + bx + c)/dc
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">1
<td valign="bottom" align="center" style=" font-size:10pt;">1
<td valign="bottom" align="center" style=" font-size:10pt;">1
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">2
<td valign="bottom" align="center" style=" font-size:10pt;">4
<td valign="bottom" align="center" style=" font-size:10pt;">2
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">3
<td valign="bottom" align="center" style=" font-size:10pt;">9
<td valign="bottom" align="center" style=" font-size:10pt;">3
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">4
<td valign="bottom" align="center" style=" font-size:10pt;">16
<td valign="bottom" align="center" style=" font-size:10pt;">4
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">5
<td valign="bottom" align="center" style=" font-size:10pt;">25
<td valign="bottom" align="center" style=" font-size:10pt;">5
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">6
<td valign="bottom" align="center" style=" font-size:10pt;">36
<td valign="bottom" align="center" style=" font-size:10pt;">6
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">7
<td valign="bottom" align="center" style=" font-size:10pt;">49
<td valign="bottom" align="center" style=" font-size:10pt;">7
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">8
<td valign="bottom" align="center" style=" font-size:10pt;">64
<td valign="bottom" align="center" style=" font-size:10pt;">8
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">9
<td valign="bottom" align="center" style=" font-size:10pt;">81
<td valign="bottom" align="center" style=" font-size:10pt;">9
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
<tr>
<td valign="bottom" align="center" style=" font-size:10pt;">10
<td valign="bottom" align="center" style=" font-size:10pt;">100
<td valign="bottom" align="center" style=" font-size:10pt;">10
<td valign="bottom" align="center" style=" font-size:10pt;">1
</tr>
</table>
<p>
The implementation of the <code>MultivariateMatrixFunction jacobian() for this problem looks like this (The
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.