Giter Club home page Giter Club logo

rminqa's Introduction

rminqa: Derivative-Free Optimization in R using C++.

Build Status cran version downloads total downloads

Features

Installation

Get the development version from github:

install.packages("devtools")
library(devtools)
devtools::install_github("ypan1988/rminqa", dependencies=TRUE)

Or the released version from CRAN:

install.packages("rminqa")

A Quick Example

An example of using minqa::bobyqa() in R environment:

fr <- function(x) {   ## Rosenbrock Banana function
  x1 <- x[1]
  x2 <- x[2]
  100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
(x1 <- minqa::bobyqa(c(1, 2), fr, lower = c(0, 0), upper = c(4, 4)))
## => optimum at c(1, 1) with fval = 0
str(x1) # see that the error code and msg are returned

Corresponding code written in C++ using package rminqa (file demo.cpp):

#include <cmath>  // std::pow

#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]

#include "rbobyqa.h"
// [[Rcpp::depends(rminqa)]]

using namespace rminqa;

class Rosen : public Functor {
public:
  double operator()(const arma::vec &x) override {
    double x1 = x(0);
    double x2 = x(1);
    return 100 * std::pow((x2 - x1 * x1), 2) + std::pow(1 - x1, 2);
  }
};

// [[Rcpp::export]]
void bobyqa_rosen() {
  Rosen rb;
  Rbobyqa<Rosen> opt;

  arma::vec x = {1, 2};
  opt.minimize(rb, x);

  Rcpp::Rcout << "-------------------------" << std::endl;
  Rcpp::Rcout << "par = \n" << opt.par() << std::endl;
  Rcpp::Rcout << "fval = " << opt.fval() << std::endl;
  Rcpp::Rcout << "feval = " << opt.feval() << std::endl;
  Rcpp::Rcout << "msg = " << opt.msg() << std::endl;
  Rcpp::Rcout << "-------------------------" << std::endl;
}

Compile and run the function in R:

library(Rcpp)
sourceCpp("~/demo.cpp") # you may need to change the directory
bobyqa_rosen()

Then you will get expected output as follows:

-------------------------
par = 
   1.0000
   1.0000

fval = 9.98796e-15
feval = 341
msg = Normal exit from bobyqa
-------------------------

rminqa's People

Contributors

ypan1988 avatar samuel-watson avatar samueliwatson avatar

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.