Git Product home page Git Product logo

optimizemethod's Introduction

OptimizeMethod

简介

传统常见最优化方法封装,支持跨平台:Windows, Linux , macOS,包括单峰区间搜索、黄金分割区间搜索、精确线搜索(单峰区间+黄金分割)、Armijo 非精确线搜索、最速下降法、拟牛顿BFGS算法、拟牛顿DFP算法等。

依赖库(已包含在3rdparty内)

spdlog - 轻量C++日志库

Eigen - 矩阵C++库

算例

Assignment 1

使用最速下降法求 $f(x_1, x_2) = 3 x_1^2 + 2x_2^2 - 4 x_1 - 6x_2$ ,要求线搜索方法使用单峰区间搜索方法 + 黄金分割法,使用迭代初始点 $(0,0)^T$

double target_function(Eigen::VectorXd x)
{
    double const x0 = x[0];
    double const y0 = x[1];
    return 3 * x0 * x0 + 2 * y0 * y0 - 4 * x0 - 6 * y0;
}

int main()
{
    Eigen::VectorXd x0(2);
    x0 << 0, 0;
    OptimizeMethod opti = OptimizeMethod(target_function, spdlog::level::debug);
    opti.gradient_descent(100, 1e-6, 0.01, x0);
    #if defined(_WIN32) || defined(_WIN64)
    system("pause");
    #endif
}

image

Assignment 2

分别使用拟牛顿方法中BFGS方法与DFP方法,采用Armijo准则进行非精确线搜索方法,使用迭代初始点 $(2,2)^T$ ,计算 $f(x_1, x_2) = x_1^2 - x_1 x_2 + x_2^2 + 2x_1 - 4x_2$ 的最小值。

#include <OptimizeMethod.h>
#include <spdlog/spdlog.h>
#include <cstdlib>

double target_function(Eigen::VectorXd x)
{
    double const x1 = x[0];
    double const x2 = x[1];
    return x1 * x1 - x1 * x2 + x2 * x2 + 2 * x1 - 4 * x2;
}

Eigen::VectorXd grad_target_function(Eigen::VectorXd x)
{
    Eigen::VectorXd grad = Eigen::VectorXd::Zero(x.size());
    double const x1 = x[0];
    double const x2 = x[1];
    grad[0] = 2 * x1 - x2 + 2;
    grad[1] = 2 * x2 - x1 - 4;
    return grad;
}

int main()
{
    Eigen::VectorXd x0(2);
    x0 << -2, 2;
    OptimizeMethod opti = OptimizeMethod(target_function, grad_target_function, spdlog::level::debug);
    opti.Newton_BFGS(100, 0.5, 0.2, 1e-6, 0.0001, x0);
    opti.Newton_DFP(100, 0.5, 0.2, 1e-6, 0.0001, x0);
    #if defined(_WIN32) || defined(_WIN64)
    system("pause");
    #endif
}

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.