Giter Club home page Giter Club logo

dcgp.js's Introduction

dcgp.js

JavaScript bindings for the differential Cartesian Genetic Programming library. The goal of this project is to provide a library that can run dcgp both on node.js and the web.

Installation

Install dcgp.js with npm:

npm install dcgp

Usage

import { initialise, KernelSet, Expression, algorithms } from 'dcgp'

await initialise(/* specify custom path to dcgp.wasm */);
const myKernelSet = new KernelSet('sum', 'diff', 'mul', 'div');
const myExpression = new Expression(2, 1, 2, 6, 5, 2, myKernelSet, 5);

// some simple dataset: y = 2x + 2
const inputs = [[0, 1, 2, 3, 4]];
const outputs = [[2, 4, 6, 8, 10]];

const { loss } = algorithms.muPlusLambda(myExpression, 2, 5, 50, inputs, outputs, [1]);

// Free memory 
myKernelSet.destroy()
myExpression.destroy()

Development

Anyone is welcome to help progress and improve this library. Tasks and issues can be found in the issues tab. If your problem/task is not in the tasks, feel free to create a new issue explaining your problem/task.

Prerequisite

  • docker

Installation

Note: the instructions shown below assume Linux or macOS. Comments are provided with instructions for Windows.

git clone https://github.com/mikeheddes/dcgp.js.git

cd dcgp.js

# Start the 32bit ubuntu image with all dcgp.js' dependencies installed
# Note: make sure the docker daemon is running
docker run -it -v "$(pwd):/root/repo" -w /root/repo mikeheddes/dcgp.js-dependencies bash

# Command for windows
# docker run -it -v "%CD%:/root/repo" -w /root/repo mikeheddes/dcgp.js-dependencies bash

# A bash environment will open
npm install

npm run build

# To test the generated bundle in the browser run the following command
npx http-server lib

# To leave the bash environment run
exit

VSCode IntelliSense support

To have IntelliSense support for the C++ files the include folder from the container needs to be copied to the local file system. For this you can use the following commands ones the docker images mikeheddes/dcgp.js-dependencies is running.

# Copy the container ID 
docker ps
docker cp REPLACE_WITH_THE_ID:/usr/local/include ./
docker cp REPLACE_WITH_THE_ID:/root/emsdk/emscripten/tag-1.38.30/system/include/ ./
docker cp REPLACE_WITH_THE_ID:/usr/include/eigen3 ./include/

dcgp.js's People

Contributors

mikeheddes avatar

Stargazers

whysosergious avatar Brad Pillow avatar Steven Zhao avatar Andrew avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

fagan2888 atfera

dcgp.js's Issues

Separate the C++ expression bindings

There is a linker error with print_my_diff in the dcgp/wrapped_functions.hpp file being declared multiple times when separating the Expression bindings to the expression.cpp file. This probably happens because both expression.cpp and kernel_set.cpp include dcgp/kernel_set.hpp which intern includes dcgp/wrapped_functions.hpp and therefor print_my_diff is declared in two object files and the linker doesn't know which one to pick.

Initialising dcgp returning a promise

Currently initialising dcgp immediately returns the classes and downloads, compiles and initialises the wasm file in the background. This makes it unsafe to call one of the returned classes because the wasm file might not be fully initialised yet which results in errors.

Initialising dcgp should return a promise that resolves with the initialised classes when the WebAssembly instance is downloaded, compiled and initialised so the user knows it's safe to use the dcgp classes.

Smaller emscripten glue code

The generated glue code by emscripten is currently ~5000 lines but contains a lot of unnecessary functions. Smaller and more customisable glue code would be a big improvement. The customisability also ties in with issue #2.

Provide custom dcgp.wasm path

Currently dcgp.js assumes that the dcgp.wasm file is in the current directory or in the root folder of the server. I would like to let the user of this library decide where the dcgp.wasm file should be stored. This could come in handy when using this library together with webpack to create a web app.

The limitation factor in this issue is the by emscripten created glue code dcgp.js. A start has been made in the index.js file, see the commented code. This however didn't work because the generated dcgp.wasm file needs some of the generated dcgp.js code.

Build piranha single threaded

WebAssembly by default only supports single threaded applications and piranha works multi-threaded. To resolve errors and remove the need for the user to set the experimental multi-threading flag in the browser piranha should be configured to only use the main thread.

Main issue is #10.

Remove null byte from equation

Expression.getEquation returns a string with a null byte "\0" at the end which might cause errors or inconsistencies in the JS code that uses the equation string.

A solution could be to add the following snipped before returning the equation

equation = equation.substring(0, equation.length - 1)

Add continuous integration

Setup CI so that when the master branch updates a new version of dcgp.js is published to NPM. This also implies that direct development on the master branch will be forbidden.

Add npm install script

It would be nice if the development setup of this project could all be done with npm install. For that to work some platform specific commands need to be executed.

Node OS.platform()

Add evolution algorithm

Add a C++ implementation of the ES-(1+λ) algorithm as implemented in this python example.

The algorithm function header is:

// returns the loss
unsigned es_algorithm(dcgp::expression<double> *self, unsigned offsprings, unsigned max_gen, double *x, double *yt)

At the end of the algorithm the best chromosome is set in the expression, this makes it possible to get the best chromosome from JS afterwards.

Replace current C++ build pipeline

Because of the progress of issue #10 it should now be possible to compile the current bindings with the unedited AuDi library and the dCGP library with flags for multi-treading. Currently a customised AuDi and dCGP version is used that disabled the functionalities that could not be compiled.

This also means that the other dependencies need to be added to the build/compile pipeline.

Add differentiable support

Add differentiable support by compiling with AuDi.

Dependencies

Requirements

  • To compile gmp a 32 bit system is required, I use i386/ubuntu:latest.
  • The m4, build-essentials, gcc-multilib, git, cmake, curl, node.js and python. I use the following command to install the dependencies:
apt-get -y update
apt-get -y install build-essential m4 gcc-multilib git cmake nodejs default-jre python3.6 python3-distutils vim curl
ln /usr/bin/python3.6 /usr/bin/python

Note: the command ln /usr/bin/python3.6 /usr/bin/python is needed to access python on the comand line instead of using Python with python3.6.

Compilation steps

Boost

curl -L -o boost_1_68_0.tar.bz2 https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.tar.bz2
tar --bzip2 -xf ./boost_1_68_0.tar.bz2
cd ./boost_1_68_0
./bootstrap.sh
# see Using boost with emscripten
sed -i 's/using gcc ;/using gcc : : "\/root\/emsdk\/emscripten\/tag-1.38.27\/em++" ;/g' project-config.jam 
./b2 variant=release threading=single --with-timer --with-chrono --with-serialization --with-system --with-test --with-iostreams --with-regex install

Eigen

# curl -L -o eigen_3_3_7.tar.bz2 http://bitbucket.org/eigen/eigen/get/3.3.7.tar.bz2
# tar --bzip2 -xf ./eigen_3_3_7.tar.bz2
# mkdir eigen-eigen-323c052e1731/build
# cd eigen-eigen-323c052e1731/build
# emconfigure cmake ..
# emmake make install
apt-get install libeigen3-dev

gmp

curl -L -o gmp_6_1_2.tar.bz2 https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
tar --bzip2 -xf ./gmp_6_1_2.tar.bz2
cd gmp-6.1.2
CC_FOR_BUILD=/usr/bin/gcc emconfigure ./configure --build i686-pc-linux-gnu --host none --disable-assembly --enable-cxx
# apply patches found in: https://github.com/marcosscriven/cgaljs/tree/master/components/gmp/patches
emmake make -j4
emmake make install

mpfr

curl -L -o mpfr-4.0.1.tar.bz2 https://www.mpfr.org/mpfr-4.0.1/mpfr-4.0.1.tar.bz2
tar --bzip2 -xf ./mpfr-4.0.1.tar.bz2
curl -L -o mpfr-4.0.1-allpatches https://www.mpfr.org/mpfr-4.0.1/allpatches
cd mpfr-4.0.1
patch -N -Z -p1 < ../mpfr-4.0.1-allpatches
emconfigure ./configure --build i686-pc-linux-gnu --host none --with-gmp-include=/usr/local/include/gmp --with-gmp-lib=/usr/local/lib
emmake make -j 4
emmake make install

mp++

git clone https://github.com/bluescarni/mppp.git
cd mppp
git checkout tags/v0.9
mkdir build && cd build
emconfigure cmake .. -DMPPP_WITH_MPFR=ON -DGMP_INCLUDE_DIR=/usr/local/include/gmp -DGMP_LIBRARY=/usr/local/lib -DMPFR_INCLUDE_DIR=/usr/local/include/mpfr -DMPFR_LIBRARY=/usr/local/lib
emmake make install

piranha

git clone https://github.com/bluescarni/piranha.git
cd piranha
git checkout tags/v0.11
mkdir build && cd build
# disable all try_compile statements in ../CMakeLists.txt and in ../cmake_modules/PiranhaCompilerLinkerSettings.cmake
emconfigure cmake .. -DGMP_INCLUDE_DIR=/usr/local/include -DMPFR_INCLUDE_DIR=/usr/local/include -DMPFR_LIBRARIES=/usr/local/lib -DGMP_LIBRARIES=/usr/local/lib -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR_RELEASE=/usr/local/lib
emmake make install

audi

git clone https://github.com/darioizzo/audi.git
cd audi
git checkout tags/1.6.1
mkdir build && cd build
# disable try_compile statements in ../CMakeLists.txt
emconfigure cmake .. -DAUDI_WITH_MPPP=ON -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR=/usr/local/lib -DEIGEN3_INCLUDE_DIR=/usr/local/include/eigen3 -DPiranha_INCLUDE_DIR=/usr/local/include -DGMP_INCLUDE_DIR=/usr/local/include -DGMP_LIBRARY=/usr/local/lib -DMPFR_INCLUDE_DIR=/usr/local/include -DMPFR_LIBRARY=/usr/local/lib -DAUDI_BUILD_TESTS=OFF
emmake make install

dcgp

git clone https://github.com/darioizzo/dcgp.git
mkdir dcgp/build && cd dcgp/build
# disable try_compile statements and statements with TBB in ../CMakeLists.txt 
emconfigure cmake .. -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR_RELEASE=/usr/local/lib -DEIGEN3_INCLUDE_DIR=/usr/include/eigen3/ -DPiranha_INCLUDE_DIR=/usr/local/include/ -DGMP_INCLUDE_DIR=/usr/local/include/ -DGMP_LIBRARY=/usr/local/lib/ -DMPFR_INCLUDE_DIR=/usr/local/include/ -DMPFR_LIBRARY=/usr/local/lib/ -DAUDI_INCLUDE_DIRS=/usr/local/include/ -DDCGP_BUILD_TESTS=OFF
emmake make install

Resources

The following links might be helpful for figuring out how to compile all the necessary libraries correctly.

  1. dCGP Github
  2. Boost Getting Started on Unix Variants
  3. Using boost with emscripten
  4. Building Boost with emscripten
  5. Boost Building the Unit Test Framework
  6. Status on using webidl binder or embind for C++ bindings
  7. Compiling GMP to WASM instructions
  8. Compiling GMP/MPFR with Emscripten
  9. Error when configuring gmp, missing m4
  10. GMP and MPFR comilation script
  11. Setting Up a 32-bit chroot Environment in Ubuntu
  12. 32-bit Ubuntu docker image
  13. Compiling GMP/MPFR practical example
  14. GMP/MPFR/BOOST python build script with emscripten

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.