tmp
This commit is contained in:
parent
4d29b9c8fa
commit
1022227780
@ -20,4 +20,4 @@ add_example(ex7 OFF)
|
|||||||
add_example(ex8 OFF)
|
add_example(ex8 OFF)
|
||||||
add_example(ex9 OFF)
|
add_example(ex9 OFF)
|
||||||
add_example(ex10 OFF)
|
add_example(ex10 OFF)
|
||||||
add_example(cfg_ex OFF)
|
add_example(cfg_ex ON)
|
@ -1,43 +1,143 @@
|
|||||||
/********************************************************
|
/********************************************************
|
||||||
* ██████╗ ███████╗████████╗██╗
|
* ██████╗ ██████╗████████╗██╗
|
||||||
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
||||||
* ██║ ███╗███████╗ ██║ ██║
|
* ██║ ███╗██║ ██║ ██║
|
||||||
* ██║ ██║╚════██║ ██║ ██║
|
* ██║ ██║██║ ██║ ██║
|
||||||
* ╚██████╔╝███████║ ██║ ███████╗
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
||||||
* ╚═════╝ ╚══════╝ ╚═╝ ╚══════╝
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
||||||
* Generic Scientific Template Library
|
* Geophysical Computational Tools & Library (GCTL)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
||||||
*
|
*
|
||||||
* The GSTL is distributed under a dual licensing scheme. You can redistribute
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
||||||
* it and/or modify it under the terms of the GNU Lesser General Public
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation, either version 2
|
* License as published by the Free Software Foundation, either version 2
|
||||||
* of the License, or (at your option) any later version. You should have
|
* of the License, or (at your option) any later version. You should have
|
||||||
* received a copy of the GNU Lesser General Public License (LGPL) along with
|
* received a copy of the GNU Lesser General Public License along with this
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
||||||
* the GSTL, please consider the option to obtain a commercial license for a
|
* the GCTL, please consider the option to obtain a commercial license for a
|
||||||
* fee. These licenses are offered by the GSTL's original author. As a rule,
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
||||||
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
||||||
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
||||||
* to include some description of your company and the realm of its activities.
|
* to include some description of your company and the realm of its activities.
|
||||||
* Also add information on how to contact you by electronic and paper mail.
|
* Also add information on how to contact you by electronic and paper mail.
|
||||||
******************************************************/
|
******************************************************/
|
||||||
|
|
||||||
#include "../lib/optimization/cmn_grad.h"
|
#include "../lib/optimization.h"
|
||||||
|
|
||||||
using namespace gctl;
|
using namespace gctl;
|
||||||
|
|
||||||
|
class cfg : public lgd_solver
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::vector<gaussian_para2d> gs1_, gs2_;
|
||||||
|
array<double> g1_, g2_;
|
||||||
|
|
||||||
|
common_gradient cmg_;
|
||||||
|
|
||||||
|
array<double> x_;
|
||||||
|
array<double> xm_;
|
||||||
|
array<double> xs_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
cfg(/* args */){}
|
||||||
|
~cfg(){}
|
||||||
|
|
||||||
|
virtual double LGD_Evaluate(const array<double> &x, array<double> &g);
|
||||||
|
void read_gaussians(std::string file, std::vector<gaussian_para2d> &gs);
|
||||||
|
void routine(int argc, char *argv[]);
|
||||||
|
};
|
||||||
|
|
||||||
|
double cfg::LGD_Evaluate(const array<double> &x, array<double> &g)
|
||||||
|
{
|
||||||
|
double f, fx = 0.0;
|
||||||
|
g1_.assign(0.0);
|
||||||
|
g2_.assign(0.0);
|
||||||
|
|
||||||
|
f = 0.0;
|
||||||
|
for (int i = 0; i < gs1_.size(); i++)
|
||||||
|
{
|
||||||
|
f += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs1_[i]);
|
||||||
|
g1_[0] += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs1_[i], gctl::Dx);
|
||||||
|
g1_[1] += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs1_[i], gctl::Dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
cmg_.fill_model_gradient(0, f, g1_);
|
||||||
|
fx += f;
|
||||||
|
|
||||||
|
f = 0.0;
|
||||||
|
for (int i = 0; i < gs2_.size(); i++)
|
||||||
|
{
|
||||||
|
f += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs2_[i]);
|
||||||
|
g2_[0] += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs2_[i], gctl::Dx);
|
||||||
|
g2_[1] += -1e+3*gctl::gaussian_dist2d(x[0], x[1], gs2_[i], gctl::Dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
cmg_.fill_model_gradient(1, f, g2_);
|
||||||
|
fx += f;
|
||||||
|
fx += 0.666027; // 目标值
|
||||||
|
|
||||||
|
g = cmg_.get_common_gradient();
|
||||||
|
return fx;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cfg::read_gaussians(std::string file, std::vector<gaussian_para2d> &gs)
|
||||||
|
{
|
||||||
|
gaussian_para2d tmp_p;
|
||||||
|
std::string tmp_str;
|
||||||
|
std::stringstream tmp_ss;
|
||||||
|
|
||||||
|
std::ifstream infile;
|
||||||
|
gctl::open_infile(infile, file, ".txt");
|
||||||
|
|
||||||
|
while(getline(infile, tmp_str))
|
||||||
|
{
|
||||||
|
if (tmp_str[0] == '#') continue;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gctl::str2ss(tmp_str, tmp_ss);
|
||||||
|
tmp_ss >> tmp_p.mu_x >> tmp_p.mu_y >> tmp_p.sigma_x >> tmp_p.sigma_y >> tmp_p.rho;
|
||||||
|
gs.push_back(tmp_p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
infile.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cfg::routine(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
cmg_.init(2, 2);
|
||||||
|
g1_.resize(2);
|
||||||
|
g2_.resize(2);
|
||||||
|
|
||||||
|
read_gaussians("example/data/gauss_model", gs1_);
|
||||||
|
read_gaussians("example/data/gauss_model2", gs2_);
|
||||||
|
|
||||||
|
x_.resize(2, 20.0);
|
||||||
|
xm_.resize(2, 0.0);
|
||||||
|
xs_.resize(2, 0.0);
|
||||||
|
array<double> low(2, 0.0);
|
||||||
|
array<double> high(2, 100.0);
|
||||||
|
|
||||||
|
lgd_para para = default_lgd_para();
|
||||||
|
para.alpha = 0.01;
|
||||||
|
para.flight_times = 1000;
|
||||||
|
set_lgd_para(para);
|
||||||
|
|
||||||
|
set_lgd_record_trace();
|
||||||
|
LGD_Minimize(x_, xm_, xs_, low, high);
|
||||||
|
|
||||||
|
save_lgd_trace("trace.txt");
|
||||||
|
std::cout << "x = (" << x_[0] << ", " << x_[1] << ")\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) try
|
int main(int argc, char *argv[]) try
|
||||||
{
|
{
|
||||||
common_gradient cmg;
|
cfg c;
|
||||||
cmg.init(2, 2);
|
c.routine(argc, argv);
|
||||||
cmg.fill_model_gradient(0, _1d_array({1.0, 0.5}));
|
|
||||||
cmg.fill_model_gradient(1, _1d_array({-0.6, 1.0}));
|
|
||||||
|
|
||||||
_1d_array x = cmg.get_common_gradient(false);
|
|
||||||
std::cout << "x = (" << x[0] << ", " << x[1] << ")\n";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
|
7
example/data/forward.sh
Executable file
7
example/data/forward.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 系统里有一个隐藏的错误设置 不然不需要在这里手动设置动态库的地址
|
||||||
|
export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH
|
||||||
|
|
||||||
|
#g++ gaussian2d.cpp -o gaussian2d -lgctl -lnetcdfcxx_legacy -std=c++17 -I/opt/homebrew/include
|
||||||
|
./gaussian2d gauss_model.txt
|
BIN
example/data/gauss_dist.nc
Normal file
BIN
example/data/gauss_dist.nc
Normal file
Binary file not shown.
BIN
example/data/gauss_dist2.nc
Normal file
BIN
example/data/gauss_dist2.nc
Normal file
Binary file not shown.
2
example/data/gauss_model.txt
Normal file
2
example/data/gauss_model.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
32.0 66.0 24.0 22.0 -0.1
|
||||||
|
32.0 10.0 26.0 24.0 0.6
|
2
example/data/gauss_model2.txt
Normal file
2
example/data/gauss_model2.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
75.0 25.0 22.0 28.0 0.5
|
||||||
|
32.0 66.0 24.0 22.0 -0.1
|
70
example/data/gaussian2d.cpp
Normal file
70
example/data/gaussian2d.cpp
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* @defgroup MAIN main
|
||||||
|
*
|
||||||
|
* @brief 测试非线形最优化中的局部极小值问题
|
||||||
|
*
|
||||||
|
* @author Zhangyi
|
||||||
|
* @date 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "gctl/core.h"
|
||||||
|
#include "gctl/io.h"
|
||||||
|
|
||||||
|
// 声明我们所使用的二维高斯分布的参数
|
||||||
|
static std::vector<gctl::gaussian_para2d> p;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
std::ifstream infile;
|
||||||
|
gctl::open_infile(infile, argv[1], "");
|
||||||
|
|
||||||
|
gctl::gaussian_para2d tmp_p;
|
||||||
|
std::string tmp_str;
|
||||||
|
std::stringstream tmp_ss;
|
||||||
|
while(getline(infile, tmp_str))
|
||||||
|
{
|
||||||
|
if (tmp_str[0] == '#') continue;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gctl::str2ss(tmp_str, tmp_ss);
|
||||||
|
tmp_ss >> tmp_p.mu_x >> tmp_p.mu_y >> tmp_p.sigma_x
|
||||||
|
>> tmp_p.sigma_y >> tmp_p.rho;
|
||||||
|
p.push_back(tmp_p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infile.close();
|
||||||
|
|
||||||
|
// 我们先来确认高斯分布的参数 生成解空间
|
||||||
|
int m = 101, n = 101;
|
||||||
|
gctl::array<double> gauss_dist(m*n, 0.0);
|
||||||
|
for (int t = 0; t < p.size(); t++)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < m; j++)
|
||||||
|
{
|
||||||
|
gauss_dist[j + i*m] += -1e+3*gctl::gaussian_dist2d(1.0*j, 1.0*i, p[t]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出最小值及对应的解
|
||||||
|
double f_min = 1e+30, x_min, y_min;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < m; j++)
|
||||||
|
{
|
||||||
|
if (gauss_dist[j + i*m] < f_min)
|
||||||
|
{
|
||||||
|
f_min = gauss_dist[j + i*m];
|
||||||
|
x_min = j; y_min = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "f_min(" << x_min << ", " << y_min << ") = " << std::setprecision(10) << f_min << std::endl;
|
||||||
|
|
||||||
|
// 保存计算结果
|
||||||
|
gctl::save_netcdf_grid("gauss_dist", gauss_dist, m, n, 0, 1, 0, 1);
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
example/data/lgd_anim.mp4
Normal file
BIN
example/data/lgd_anim.mp4
Normal file
Binary file not shown.
BIN
example/data/lgd_anim2.mp4
Normal file
BIN
example/data/lgd_anim2.mp4
Normal file
Binary file not shown.
27
example/data/plot_movie.sh
Executable file
27
example/data/plot_movie.sh
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# 1. Create files needed in the loop
|
||||||
|
cat << 'EOF' > pre.sh
|
||||||
|
gmt begin pre
|
||||||
|
gmt set FONT_ANNOT_PRIMARY=15p,Times-Roman,black
|
||||||
|
gmt set FONT_LABEL=15p,Times-Roman,black
|
||||||
|
gmt set MAP_GRID_CROSS_SIZE_PRIMARY=5p
|
||||||
|
gmt set MAP_FRAME_PEN=thinnest,black
|
||||||
|
gmt set MAP_TICK_LENGTH_PRIMARY=4p/2p
|
||||||
|
gmt grd2cpt gauss_dist.nc -Clapaz -R0/100/0/100 -Z -D
|
||||||
|
gmt grdimage gauss_dist.nc -R0/100/0/100 -Bxag+l"x (m)" -Byag+l"y (m)" -JX15c/15c -X4.5c -Y1.5c
|
||||||
|
gmt end
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 2. Set up the main frame script
|
||||||
|
cat << 'EOF' > main.sh
|
||||||
|
gmt begin
|
||||||
|
# Plot smooth blue curve and dark red dots at all steps so far
|
||||||
|
gmt convert trace.txt -qi0:${MOVIE_FRAME} > data.txt
|
||||||
|
gmt plot data.txt -W0.05p,white -R0/100/0/100 -JX15c/15c -X4.5c -Y1.5c
|
||||||
|
gmt plot data.txt -Sc0.05i -Gred
|
||||||
|
gmt end
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 3. Run the movie
|
||||||
|
gmt movie main.sh -Sbpre.sh -Cxga -Ttrace.txt -Vi -D24 -Zs -Nlgd_anim -Fmp4
|
@ -1,24 +1,24 @@
|
|||||||
/********************************************************
|
/********************************************************
|
||||||
* ██████╗ ███████╗████████╗██╗
|
* ██████╗ ██████╗████████╗██╗
|
||||||
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
||||||
* ██║ ███╗███████╗ ██║ ██║
|
* ██║ ███╗██║ ██║ ██║
|
||||||
* ██║ ██║╚════██║ ██║ ██║
|
* ██║ ██║██║ ██║ ██║
|
||||||
* ╚██████╔╝███████║ ██║ ███████╗
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
||||||
* ╚═════╝ ╚══════╝ ╚═╝ ╚══════╝
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
||||||
* Generic Scientific Template Library
|
* Geophysical Computational Tools & Library (GCTL)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
||||||
*
|
*
|
||||||
* The GSTL is distributed under a dual licensing scheme. You can redistribute
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
||||||
* it and/or modify it under the terms of the GNU Lesser General Public
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation, either version 2
|
* License as published by the Free Software Foundation, either version 2
|
||||||
* of the License, or (at your option) any later version. You should have
|
* of the License, or (at your option) any later version. You should have
|
||||||
* received a copy of the GNU Lesser General Public License (LGPL) along with
|
* received a copy of the GNU Lesser General Public License along with this
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
||||||
* the GSTL, please consider the option to obtain a commercial license for a
|
* the GCTL, please consider the option to obtain a commercial license for a
|
||||||
* fee. These licenses are offered by the GSTL's original author. As a rule,
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
||||||
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
||||||
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
||||||
* to include some description of your company and the realm of its activities.
|
* to include some description of your company and the realm of its activities.
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
/********************************************************
|
/********************************************************
|
||||||
* ██████╗ ███████╗████████╗██╗
|
* ██████╗ ██████╗████████╗██╗
|
||||||
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
||||||
* ██║ ███╗███████╗ ██║ ██║
|
* ██║ ███╗██║ ██║ ██║
|
||||||
* ██║ ██║╚════██║ ██║ ██║
|
* ██║ ██║██║ ██║ ██║
|
||||||
* ╚██████╔╝███████║ ██║ ███████╗
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
||||||
* ╚═════╝ ╚══════╝ ╚═╝ ╚══════╝
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
||||||
* Generic Scientific Template Library
|
* Geophysical Computational Tools & Library (GCTL)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
||||||
*
|
*
|
||||||
* The GSTL is distributed under a dual licensing scheme. You can redistribute
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
||||||
* it and/or modify it under the terms of the GNU Lesser General Public
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation, either version 2
|
* License as published by the Free Software Foundation, either version 2
|
||||||
* of the License, or (at your option) any later version. You should have
|
* of the License, or (at your option) any later version. You should have
|
||||||
* received a copy of the GNU Lesser General Public License (LGPL) along with
|
* received a copy of the GNU Lesser General Public License along with this
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
||||||
* the GSTL, please consider the option to obtain a commercial license for a
|
* the GCTL, please consider the option to obtain a commercial license for a
|
||||||
* fee. These licenses are offered by the GSTL's original author. As a rule,
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
||||||
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
||||||
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
||||||
* to include some description of your company and the realm of its activities.
|
* to include some description of your company and the realm of its activities.
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
/********************************************************
|
/********************************************************
|
||||||
* ██████╗ ███████╗████████╗██╗
|
* ██████╗ ██████╗████████╗██╗
|
||||||
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
||||||
* ██║ ███╗███████╗ ██║ ██║
|
* ██║ ███╗██║ ██║ ██║
|
||||||
* ██║ ██║╚════██║ ██║ ██║
|
* ██║ ██║██║ ██║ ██║
|
||||||
* ╚██████╔╝███████║ ██║ ███████╗
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
||||||
* ╚═════╝ ╚══════╝ ╚═╝ ╚══════╝
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
||||||
* Generic Scientific Template Library
|
* Geophysical Computational Tools & Library (GCTL)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
||||||
*
|
*
|
||||||
* The GSTL is distributed under a dual licensing scheme. You can redistribute
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
||||||
* it and/or modify it under the terms of the GNU Lesser General Public
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation, either version 2
|
* License as published by the Free Software Foundation, either version 2
|
||||||
* of the License, or (at your option) any later version. You should have
|
* of the License, or (at your option) any later version. You should have
|
||||||
* received a copy of the GNU Lesser General Public License (LGPL) along with
|
* received a copy of the GNU Lesser General Public License along with this
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
||||||
* the GSTL, please consider the option to obtain a commercial license for a
|
* the GCTL, please consider the option to obtain a commercial license for a
|
||||||
* fee. These licenses are offered by the GSTL's original author. As a rule,
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
||||||
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
||||||
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
||||||
* to include some description of your company and the realm of its activities.
|
* to include some description of your company and the realm of its activities.
|
||||||
|
@ -41,6 +41,7 @@ gctl::common_gradient::~common_gradient(){}
|
|||||||
|
|
||||||
void gctl::common_gradient::LCG_Ax(const array<double> &x, array<double> &ax)
|
void gctl::common_gradient::LCG_Ax(const array<double> &x, array<double> &ax)
|
||||||
{
|
{
|
||||||
|
// Ax product of the linear system
|
||||||
matvec(t_, G_, x, NoTrans);
|
matvec(t_, G_, x, NoTrans);
|
||||||
vecmul(t_, t_, w_);
|
vecmul(t_, t_, w_);
|
||||||
matvec(ax, G_, t_, Trans);
|
matvec(ax, G_, t_, Trans);
|
||||||
@ -64,24 +65,18 @@ void gctl::common_gradient::set_weights(const _1d_array &w)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gctl::common_gradient::set_exp_weight(double T)
|
|
||||||
{
|
|
||||||
T_ = T;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void gctl::common_gradient::init(size_t Ln, size_t Mn)
|
void gctl::common_gradient::init(size_t Ln, size_t Mn)
|
||||||
{
|
{
|
||||||
|
zero_iter_ = true;
|
||||||
Ln_ = Ln;
|
Ln_ = Ln;
|
||||||
Mn_ = Mn;
|
Mn_ = Mn;
|
||||||
T_ = 1.0;
|
|
||||||
g_.resize(Mn_);
|
g_.resize(Mn_);
|
||||||
B_.resize(Mn_);
|
B_.resize(Mn_);
|
||||||
G_.resize(Ln_, Mn_);
|
G_.resize(Ln_, Mn_);
|
||||||
t_.resize(Ln_);
|
t_.resize(Ln_);
|
||||||
gm_.resize(Ln_);
|
gm_.resize(Ln_);
|
||||||
lx_.resize(Ln_);
|
fx_.resize(Ln_);
|
||||||
lt_.resize(Ln_, 1.0);
|
fx0_.resize(Ln_);
|
||||||
w_.resize(Ln_, 1.0);
|
w_.resize(Ln_, 1.0);
|
||||||
x_.resize(Ln_, 1.0);
|
x_.resize(Ln_, 1.0);
|
||||||
filled_.resize(Ln_, false);
|
filled_.resize(Ln_, false);
|
||||||
@ -95,13 +90,16 @@ void gctl::common_gradient::fill_model_gradient(size_t id, double fx, const _1d_
|
|||||||
|
|
||||||
G_.fill_row(id, g);
|
G_.fill_row(id, g);
|
||||||
gm_[id] = g.module();
|
gm_[id] = g.module();
|
||||||
lx_[id] = fx;
|
fx_[id] = fx;
|
||||||
|
if (zero_iter_) fx0_[id] = fx;
|
||||||
filled_[id] = true;
|
filled_[id] = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const gctl::_1d_array &gctl::common_gradient::get_common_gradient(bool normalized, bool fixed_w)
|
const gctl::_1d_array &gctl::common_gradient::get_common_gradient(bool normalized, bool fixed_w)
|
||||||
{
|
{
|
||||||
|
zero_iter_ = false; // set to false after the first evaluation.
|
||||||
|
|
||||||
for (size_t i = 0; i < Ln_; i++)
|
for (size_t i = 0; i < Ln_; i++)
|
||||||
{
|
{
|
||||||
if (!filled_[i]) throw std::runtime_error("[gctl::common_gradient] Unfilled model gradient.");
|
if (!filled_[i]) throw std::runtime_error("[gctl::common_gradient] Unfilled model gradient.");
|
||||||
@ -114,13 +112,15 @@ const gctl::_1d_array &gctl::common_gradient::get_common_gradient(bool normalize
|
|||||||
double a;
|
double a;
|
||||||
for (size_t i = 0; i < Ln_; i++)
|
for (size_t i = 0; i < Ln_; i++)
|
||||||
{
|
{
|
||||||
a = abs(lx_[i] - lt_[i]);
|
// the bigger gradient module is, the bigger weight is
|
||||||
w_[i] = 1.0/pow(1.0/(1.0 + exp(-0.05*a) + 0.5), 6);
|
// the faster convergence is, the smaller weight is
|
||||||
|
a = gm_[i]*fx_[i]/fx0_[i];
|
||||||
|
w_[i] = 1.0/a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rcd_wgts_.push_back(w_);
|
rcd_wgts_.push_back(w_);
|
||||||
rcd_fxs_.push_back(lx_);
|
rcd_fxs_.push_back(fx_);
|
||||||
|
|
||||||
G_.normalize(RowMajor);
|
G_.normalize(RowMajor);
|
||||||
matvec(B_, G_, x_, Trans);
|
matvec(B_, G_, x_, Trans);
|
||||||
@ -130,19 +130,6 @@ const gctl::_1d_array &gctl::common_gradient::get_common_gradient(bool normalize
|
|||||||
LCG_Minimize(g_, B_, LCG_CG);
|
LCG_Minimize(g_, B_, LCG_CG);
|
||||||
|
|
||||||
if (normalized) g_.normalize();
|
if (normalized) g_.normalize();
|
||||||
else
|
|
||||||
{
|
|
||||||
g_.normalize();
|
|
||||||
matvec(t_, G_, g_, NoTrans);
|
|
||||||
|
|
||||||
double tmod = t_.module(L1);
|
|
||||||
double gmod = 0.0;
|
|
||||||
for (size_t i = 0; i < Ln_; i++)
|
|
||||||
{
|
|
||||||
gmod += gm_[i]*abs(t_[i])/tmod;
|
|
||||||
}
|
|
||||||
g_.scale(gmod);
|
|
||||||
}
|
|
||||||
return g_;
|
return g_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +138,7 @@ void gctl::common_gradient::save_records(std::string file)
|
|||||||
std::ofstream fout;
|
std::ofstream fout;
|
||||||
open_outfile(fout, file, ".csv");
|
open_outfile(fout, file, ".csv");
|
||||||
|
|
||||||
fout << "Num";
|
fout << "num";
|
||||||
for (size_t j = 0; j < Ln_; j++)
|
for (size_t j = 0; j < Ln_; j++)
|
||||||
{
|
{
|
||||||
fout << ",l" << j;
|
fout << ",l" << j;
|
||||||
@ -161,11 +148,10 @@ void gctl::common_gradient::save_records(std::string file)
|
|||||||
{
|
{
|
||||||
fout << ",w" << j;
|
fout << ",w" << j;
|
||||||
}
|
}
|
||||||
fout << std::endl;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < rcd_wgts_.size(); i++)
|
for (size_t i = 0; i < rcd_wgts_.size(); i++)
|
||||||
{
|
{
|
||||||
fout << i;
|
fout << std::endl << i;
|
||||||
for (size_t j = 0; j < Ln_; j++)
|
for (size_t j = 0; j < Ln_; j++)
|
||||||
{
|
{
|
||||||
fout << "," << rcd_fxs_[i][j];
|
fout << "," << rcd_fxs_[i][j];
|
||||||
@ -175,7 +161,6 @@ void gctl::common_gradient::save_records(std::string file)
|
|||||||
{
|
{
|
||||||
fout << "," << rcd_wgts_[i][j];
|
fout << "," << rcd_wgts_[i][j];
|
||||||
}
|
}
|
||||||
fout << std::endl;
|
|
||||||
}
|
}
|
||||||
fout.close();
|
fout.close();
|
||||||
return;
|
return;
|
||||||
|
@ -57,15 +57,14 @@ namespace gctl
|
|||||||
void set_solver(const lcg_para ¶);
|
void set_solver(const lcg_para ¶);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the weights for the loss functions
|
* @brief Set the weights for the loss functions.
|
||||||
|
*
|
||||||
|
* The number of weights equal to the number of the loss functions.
|
||||||
|
* The bigger weights is the calculated gradient is more dependent
|
||||||
|
* on the corresponding gradients.
|
||||||
*/
|
*/
|
||||||
void set_weights(const _1d_array &w);
|
void set_weights(const _1d_array &w);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Set the weight for the exponential weighting function
|
|
||||||
*/
|
|
||||||
void set_exp_weight(double T);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize the common_gradient object
|
* @brief Initialize the common_gradient object
|
||||||
*
|
*
|
||||||
@ -100,15 +99,15 @@ namespace gctl
|
|||||||
void save_records(std::string file);
|
void save_records(std::string file);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double T_;
|
bool zero_iter_;
|
||||||
size_t Ln_, Mn_; // lc: loss count, Ln_: loss_func number,Mn_: model number
|
size_t Ln_, Mn_; // Ln_: loss_func number,Mn_: model number
|
||||||
_2d_matrix G_;
|
_2d_matrix G_; // kernel martix
|
||||||
_1d_array B_, g_, t_, x_;
|
_1d_array B_, g_, t_, x_; // variables of the linear system
|
||||||
_1d_array gm_, w_;
|
_1d_array gm_, w_; // gradient module and functions' weight
|
||||||
_1d_array lx_, lt_;
|
_1d_array fx_, fx0_; // functions' value, initial functions' value
|
||||||
array<bool> filled_;
|
array<bool> filled_; // new gradient filled for the current round of evaluation
|
||||||
std::vector<array<double> > rcd_wgts_;
|
std::vector<array<double> > rcd_wgts_; // weights records
|
||||||
std::vector<array<double> > rcd_fxs_;
|
std::vector<array<double> > rcd_fxs_; // fx records
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
/********************************************************
|
/********************************************************
|
||||||
* ██████╗ ███████╗████████╗██╗
|
* ██████╗ ██████╗████████╗██╗
|
||||||
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
||||||
* ██║ ███╗███████╗ ██║ ██║
|
* ██║ ███╗██║ ██║ ██║
|
||||||
* ██║ ██║╚════██║ ██║ ██║
|
* ██║ ██║██║ ██║ ██║
|
||||||
* ╚██████╔╝███████║ ██║ ███████╗
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
||||||
* ╚═════╝ ╚══════╝ ╚═╝ ╚══════╝
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
||||||
* Generic Scientific Template Library
|
* Geophysical Computational Tools & Library (GCTL)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
||||||
*
|
*
|
||||||
* The GSTL is distributed under a dual licensing scheme. You can redistribute
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
||||||
* it and/or modify it under the terms of the GNU Lesser General Public
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation, either version 2
|
* License as published by the Free Software Foundation, either version 2
|
||||||
* of the License, or (at your option) any later version. You should have
|
* of the License, or (at your option) any later version. You should have
|
||||||
* received a copy of the GNU Lesser General Public License (LGPL) along with
|
* received a copy of the GNU Lesser General Public License along with this
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
||||||
* the GSTL, please consider the option to obtain a commercial license for a
|
* the GCTL, please consider the option to obtain a commercial license for a
|
||||||
* fee. These licenses are offered by the GSTL's original author. As a rule,
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
||||||
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
||||||
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
||||||
* to include some description of your company and the realm of its activities.
|
* to include some description of your company and the realm of its activities.
|
||||||
|
Loading…
Reference in New Issue
Block a user