optimizers - initial revision
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
lib_LTLIBRARIES = libla.la
|
lib_LTLIBRARIES = libla.la
|
||||||
include_HEADERS = LA_version.h tensor.h miscfunc.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h lanczos.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h numbers.h bitvector.h fourindex.h la_traits.h la_random.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h reg.h regsurf.h
|
include_HEADERS = LA_version.h optimize.h tensor.h miscfunc.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h lanczos.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h numbers.h bitvector.h fourindex.h la_traits.h la_random.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h reg.h regsurf.h
|
||||||
libla_la_SOURCES = simple.cc tensor.cc miscfunc.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc numbers.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc la_random.cc reg.cc regsurf.cc
|
libla_la_SOURCES = simple.cc optimize.cc tensor.cc miscfunc.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc numbers.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc la_random.cc reg.cc regsurf.cc
|
||||||
nodist_libla_la_SOURCES = version.cc
|
nodist_libla_la_SOURCES = version.cc
|
||||||
check_PROGRAMS = t test tX test_reg test_regsurf
|
check_PROGRAMS = t test tX test_reg test_regsurf
|
||||||
t_SOURCES = t.cc t2.cc
|
t_SOURCES = t.cc t2.cc
|
||||||
|
|||||||
@@ -47,6 +47,7 @@
|
|||||||
#include "polynomial.h"
|
#include "polynomial.h"
|
||||||
#include "contfrac.h"
|
#include "contfrac.h"
|
||||||
#include "tensor.h"
|
#include "tensor.h"
|
||||||
|
#include "optimize.h"
|
||||||
#include "vecmat3.h"
|
#include "vecmat3.h"
|
||||||
#include "quaternion.h"
|
#include "quaternion.h"
|
||||||
#include "LA_version.h"
|
#include "LA_version.h"
|
||||||
|
|||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
LA: linear algebra C++ interface library
|
||||||
|
Copyright (C) 2026 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "optimize.h"
|
||||||
|
|
||||||
|
namespace LA {
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T testproblem<T>::funcgrad(const NRVec<T> x, NRVec<T> *grad) const
|
||||||
|
{
|
||||||
|
int n=x.size();
|
||||||
|
T r= n*(n-1)/2;
|
||||||
|
for(int i=0; i<n; ++i)
|
||||||
|
{
|
||||||
|
T x2=x[i]*x[i];
|
||||||
|
r += (i+1)*(x2 + .1*x2*x2);
|
||||||
|
for(int j=0; j<i; ++j) r -= cos(x[i]*x[j]);
|
||||||
|
}
|
||||||
|
if(grad)
|
||||||
|
{
|
||||||
|
if(grad->size()!=x.size()) laerror("grad size mismatch");
|
||||||
|
for(int k=0; k<n; ++k)
|
||||||
|
{
|
||||||
|
(*grad)[k] = (k+1)*x[k]*(2+0.4*x[k]*x[k]);
|
||||||
|
for(int i=0; i<k; ++i) (*grad)[k] += sin(x[i]*x[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void BFGS_Optimizer<T>::minimize(const OptProblem<T> *o, NRVec<T> &x, const hyperparameters<T> *par)
|
||||||
|
{
|
||||||
|
//AI: you may suggest improvements to the general design of the class hierarchy for optimizers ( optimize.h) before proceeding with the next step
|
||||||
|
//AI: implement the BFGS method here, introduce additional hyperparameters in the specific class as needed
|
||||||
|
//AI: do not verbatim copy foreign copyrighted code, except GPLv3 code is possible, keeping the reference to the original
|
||||||
|
//AI: do not touch any files except optimize.h optimize.cc and t.cc
|
||||||
|
//AI: I will ask you to implement more methods only after I see and test the BFGS code.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//force instantiation
|
||||||
|
template class testproblem<double>;
|
||||||
|
template class BFGS_Optimizer<double>;
|
||||||
|
|
||||||
|
}//namespace
|
||||||
|
|
||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
LA: linear algebra C++ interface library
|
||||||
|
Copyright (C) 2021 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
//various optimization methods
|
||||||
|
|
||||||
|
#ifndef _OPTIMIZE_H_
|
||||||
|
#define _OPTIMIZE_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <cstdarg>
|
||||||
|
#include "vec.h"
|
||||||
|
#include "mat.h"
|
||||||
|
#include "smat.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace LA {
|
||||||
|
|
||||||
|
//base classes with purely virtual functions for multiple inheritance use
|
||||||
|
|
||||||
|
//optimization problem - provides function and optional gradient, in multiply inherited classess will store auxiliary data for the function
|
||||||
|
template<typename T>
|
||||||
|
class OptProblem {
|
||||||
|
public:
|
||||||
|
virtual T funcgrad(const NRVec<T> x, NRVec<T> *grad) const =0; //evaluate function and optionally also gradient at point x
|
||||||
|
virtual ~OptProblem() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
//test case
|
||||||
|
template<typename T>
|
||||||
|
class testproblem : public OptProblem<T> {
|
||||||
|
public:
|
||||||
|
virtual T funcgrad(const NRVec<T> x, NRVec<T> *grad) const;
|
||||||
|
virtual ~testproblem() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//base classes for optimization methods
|
||||||
|
|
||||||
|
//this is a collection of parameters, which can be augmented by method-specific additions, to be used as a base class
|
||||||
|
template<typename T>
|
||||||
|
class hyperparameters {
|
||||||
|
public:
|
||||||
|
int my_maxstep; //maximum number of iterations
|
||||||
|
T my_f_thr; //threshhold on function change in a single step
|
||||||
|
T my_x_thr; //threshold on x norm change in a single step
|
||||||
|
|
||||||
|
virtual int maxstep() const {return my_maxstep;}
|
||||||
|
virtual T f_thr() const {return my_f_thr;}
|
||||||
|
virtual T x_thr() const {return my_x_thr;}
|
||||||
|
virtual T g_thr() const =0;
|
||||||
|
virtual ~hyperparameters(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//base class for optimization methods, virtual functions to allow a pointer to derived class
|
||||||
|
template<typename T>
|
||||||
|
class Optimizer {
|
||||||
|
public:
|
||||||
|
virtual void minimize(const OptProblem<T> *o, NRVec<T> &x, const hyperparameters<T> *par) = 0;
|
||||||
|
virtual ~Optimizer() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//specific methods
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class BFGS_hyperparameters : public hyperparameters<T> {
|
||||||
|
public:
|
||||||
|
T my_g_thr;
|
||||||
|
virtual T g_thr() const {return my_g_thr;} //gradient magnitude convergence threshold
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class BFGS_Optimizer : public Optimizer<T> {
|
||||||
|
public:
|
||||||
|
virtual void minimize(const OptProblem<T> *o, NRVec<T> &x, const hyperparameters<T> *par);
|
||||||
|
virtual ~BFGS_Optimizer() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}//namespace
|
||||||
|
#endif /* _OPTIMIZE_H_ */
|
||||||
|
|
||||||
Reference in New Issue
Block a user