kandi background
Explore Kits

solutions | Solutions for ML competetions | Machine Learning library

 by   tuzzeg Java Version: Current License: No License

 by   tuzzeg Java Version: Current License: No License

Download this library from

kandi X-RAY | solutions Summary

solutions is a Java library typically used in Artificial Intelligence, Machine Learning, Tensorflow, Spark applications. solutions has no bugs, it has no vulnerabilities and it has low support. However solutions build file is not available. You can download it from GitHub.
Solutions for ML competetions.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • solutions has a low active ecosystem.
  • It has 34 star(s) with 17 fork(s). There are 3 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 1 open issues and 0 have been closed. On average issues are closed in 1710 days. There are no pull requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of solutions is current.
solutions Support
Best in #Machine Learning
Average in #Machine Learning
solutions Support
Best in #Machine Learning
Average in #Machine Learning

quality kandi Quality

  • solutions has 0 bugs and 0 code smells.
solutions Quality
Best in #Machine Learning
Average in #Machine Learning
solutions Quality
Best in #Machine Learning
Average in #Machine Learning

securitySecurity

  • solutions has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • solutions code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
solutions Security
Best in #Machine Learning
Average in #Machine Learning
solutions Security
Best in #Machine Learning
Average in #Machine Learning

license License

  • solutions does not have a standard license declared.
  • Check the repository for any license declaration and review the terms closely.
  • Without a license, all rights are reserved, and you cannot use the library in your applications.
solutions License
Best in #Machine Learning
Average in #Machine Learning
solutions License
Best in #Machine Learning
Average in #Machine Learning

buildReuse

  • solutions releases are not available. You will need to build from source code and install.
  • solutions has no build file. You will be need to create the build yourself to build the component from source.
  • solutions saves you 9147 person hours of effort in developing the same functionality from scratch.
  • It has 18706 lines of code, 880 functions and 173 files.
  • It has high code complexity. Code complexity directly impacts maintainability of the code.
solutions Reuse
Best in #Machine Learning
Average in #Machine Learning
solutions Reuse
Best in #Machine Learning
Average in #Machine Learning
Top functions reviewed by kandi - BETA

kandi has reviewed solutions and discovered the below as its top functions. This is intended to give you an instant insight into solutions implemented functionality, and help decide if they suit your requirements.

  • Gets the scores for a test case .
    • Reads data from the train file .
      • Main function for testing .
        • calculate the length of the vocabulary
          • Returns the cosine difference between two strings .
            • Creates the statistics .
              • Read the products from the document
                • Add score .
                  • Performs a search .
                    • Creates a simple spell checker .

                      Get all kandi verified functions for this library.

                      Get all kandi verified functions for this library.

                      solutions Key Features

                      Solutions for ML competetions

                      Python/Docker ImportError: cannot import name 'json' from itsdangerous

                      copy iconCopydownload iconDownload
                      pip install Flask=2.0.3
                      
                      pip install Flask=2.0.1
                      
                      pip install Flask=2.1.0
                      
                      pip install Flask==1.1.4
                      pip install markupsafe==2.0.1
                      
                      pip install Flask=2.0.1
                      
                      pip install Flask=2.1.0
                      
                      pip install Flask==1.1.4
                      pip install markupsafe==2.0.1
                      
                      pip install Flask=2.0.1
                      
                      pip install Flask=2.1.0
                      
                      pip install Flask==1.1.4
                      pip install markupsafe==2.0.1
                      

                      Uncaught ReferenceError: Buffer is not defined

                      copy iconCopydownload iconDownload
                      const webpack = require('webpack');
                      
                      module.exports = {
                          // ...
                      
                          plugins: [
                              // Work around for Buffer is undefined:
                              // https://github.com/webpack/changelog-v5/issues/10
                              new webpack.ProvidePlugin({
                                  Buffer: ['buffer', 'Buffer'],
                              }),
                              new webpack.ProvidePlugin({
                                  process: 'process/browser',
                              }),
                          ],
                      
                      
                          resolve: {
                              extensions: [ '.ts', '.js' ],
                              fallback: {
                                  "stream": require.resolve("stream-browserify"),
                                  "buffer": require.resolve("buffer")
                              }
                          },
                      
                      
                      const webpack = require('webpack');
                      
                      module.exports = {
                          // ...
                      
                          plugins: [
                              // Work around for Buffer is undefined:
                              // https://github.com/webpack/changelog-v5/issues/10
                              new webpack.ProvidePlugin({
                                  Buffer: ['buffer', 'Buffer'],
                              }),
                              new webpack.ProvidePlugin({
                                  process: 'process/browser',
                              }),
                          ],
                      
                      
                          resolve: {
                              extensions: [ '.ts', '.js' ],
                              fallback: {
                                  "stream": require.resolve("stream-browserify"),
                                  "buffer": require.resolve("buffer")
                              }
                          },
                      
                      
                      "buffer": "^6.0.3",
                      "process": "^0.11.10",
                      "stream-browserify": "^3.0.0"
                      "react-app-rewired": "^2.2.1" --dev
                      
                      "scripts": {
                          "debug": "react-scripts start",
                          "build": "react-scripts build",
                          "test": "react-scripts test",
                          "eject": "react-scripts eject"
                      },
                      
                        "scripts": {
                          "debug": "react-app-rewired start",
                          "build": "react-app-rewired build",
                          "test": "react-app-rewired test",
                          "eject": "react-app-rewired eject"
                        },
                      
                      const webpack = require("webpack")
                      
                      module.exports = function override(config, env) {
                          //do stuff with the webpack config...
                          config.resolve.fallback = {
                              ...config.resolve.fallback,
                              stream: require.resolve("stream-browserify"),
                              buffer: require.resolve("buffer"),
                          }
                          config.resolve.extensions = [...config.resolve.extensions, ".ts", ".js"]
                          config.plugins = [
                              ...config.plugins,
                              new webpack.ProvidePlugin({
                                  process: "process/browser",
                                  Buffer: ["buffer", "Buffer"],
                              }),
                          ]
                          // console.log(config.resolve)
                          // console.log(config.plugins)
                      
                          return config
                      }
                      
                      "buffer": "^6.0.3",
                      "process": "^0.11.10",
                      "stream-browserify": "^3.0.0"
                      "react-app-rewired": "^2.2.1" --dev
                      
                      "scripts": {
                          "debug": "react-scripts start",
                          "build": "react-scripts build",
                          "test": "react-scripts test",
                          "eject": "react-scripts eject"
                      },
                      
                        "scripts": {
                          "debug": "react-app-rewired start",
                          "build": "react-app-rewired build",
                          "test": "react-app-rewired test",
                          "eject": "react-app-rewired eject"
                        },
                      
                      const webpack = require("webpack")
                      
                      module.exports = function override(config, env) {
                          //do stuff with the webpack config...
                          config.resolve.fallback = {
                              ...config.resolve.fallback,
                              stream: require.resolve("stream-browserify"),
                              buffer: require.resolve("buffer"),
                          }
                          config.resolve.extensions = [...config.resolve.extensions, ".ts", ".js"]
                          config.plugins = [
                              ...config.plugins,
                              new webpack.ProvidePlugin({
                                  process: "process/browser",
                                  Buffer: ["buffer", "Buffer"],
                              }),
                          ]
                          // console.log(config.resolve)
                          // console.log(config.plugins)
                      
                          return config
                      }
                      
                      "buffer": "^6.0.3",
                      "process": "^0.11.10",
                      "stream-browserify": "^3.0.0"
                      "react-app-rewired": "^2.2.1" --dev
                      
                      "scripts": {
                          "debug": "react-scripts start",
                          "build": "react-scripts build",
                          "test": "react-scripts test",
                          "eject": "react-scripts eject"
                      },
                      
                        "scripts": {
                          "debug": "react-app-rewired start",
                          "build": "react-app-rewired build",
                          "test": "react-app-rewired test",
                          "eject": "react-app-rewired eject"
                        },
                      
                      const webpack = require("webpack")
                      
                      module.exports = function override(config, env) {
                          //do stuff with the webpack config...
                          config.resolve.fallback = {
                              ...config.resolve.fallback,
                              stream: require.resolve("stream-browserify"),
                              buffer: require.resolve("buffer"),
                          }
                          config.resolve.extensions = [...config.resolve.extensions, ".ts", ".js"]
                          config.plugins = [
                              ...config.plugins,
                              new webpack.ProvidePlugin({
                                  process: "process/browser",
                                  Buffer: ["buffer", "Buffer"],
                              }),
                          ]
                          // console.log(config.resolve)
                          // console.log(config.plugins)
                      
                          return config
                      }
                      
                      "buffer": "^6.0.3",
                      "process": "^0.11.10",
                      "stream-browserify": "^3.0.0"
                      "react-app-rewired": "^2.2.1" --dev
                      
                      "scripts": {
                          "debug": "react-scripts start",
                          "build": "react-scripts build",
                          "test": "react-scripts test",
                          "eject": "react-scripts eject"
                      },
                      
                        "scripts": {
                          "debug": "react-app-rewired start",
                          "build": "react-app-rewired build",
                          "test": "react-app-rewired test",
                          "eject": "react-app-rewired eject"
                        },
                      
                      const webpack = require("webpack")
                      
                      module.exports = function override(config, env) {
                          //do stuff with the webpack config...
                          config.resolve.fallback = {
                              ...config.resolve.fallback,
                              stream: require.resolve("stream-browserify"),
                              buffer: require.resolve("buffer"),
                          }
                          config.resolve.extensions = [...config.resolve.extensions, ".ts", ".js"]
                          config.plugins = [
                              ...config.plugins,
                              new webpack.ProvidePlugin({
                                  process: "process/browser",
                                  Buffer: ["buffer", "Buffer"],
                              }),
                          ]
                          // console.log(config.resolve)
                          // console.log(config.plugins)
                      
                          return config
                      }
                      
                      npm install --save buffer
                      
                      // webpack.config.js
                      const webpack = require("webpack");
                      
                      module.exports = {
                        plugins: {
                          new webpack.ProvidePlugin({
                            Buffer: ['buffer', 'Buffer'],
                          }),
                          // ..
                        }
                        // ..
                      }
                      
                      npm install --save buffer
                      
                      // webpack.config.js
                      const webpack = require("webpack");
                      
                      module.exports = {
                        plugins: {
                          new webpack.ProvidePlugin({
                            Buffer: ['buffer', 'Buffer'],
                          }),
                          // ..
                        }
                        // ..
                      }
                      

                      Flutter iOS Error Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x203913130)

                      copy iconCopydownload iconDownload
                          Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported
                          deployment target versions is 9.0 to 15.2.99. (in target 'AppAuth' from project 'Pods')
                          /Users/zack/Desktop/halseon/Merchant/halseon_merchants/ios/Pods/Pods.xcodeproj: warning: The iOS
                          Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported
                          deployment target versions is 9.0 to 15.2.99. (in target 'GoogleSignIn' from project 'Pods')
                      
                      $ rm ios/Flutter/Flutter.podspec
                      $ flutter clean
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      flutter clean
                      rm -Rf ios/Pods
                      rm -Rf ios/.symlinks
                      rm -Rf ios/Flutter/Flutter.framework
                      rm -Rf ios/Flutter/Flutter.podspec
                      rm ios/Podfile
                      
                      cd ios
                      flutter pub get
                      pod cache clean --all
                      pod install
                      
                          or  (below for m1 chip incase above command thrown error in pod install)
                      
                      #1 Install ffi
                      sudo arch -x86_64 gem install ffi
                      
                      #2 Re-install dependencies
                      arch -x86_64 pod install
                      
                      1. Open ios/Pods/Pods.xcodeproj in Xcode
                      2. Change the iOS Deployment info from 8.0 to 9.0
                      
                      From xcode clean / build again / run. Hope this should work. Before please confirm whether all the build settings for iOS configured properly or not.
                      
                      Check platform :ios, '10.0' is uncommented
                      
                      target 'CustomImageNotifications' do
                        use_frameworks!
                        pod 'Firebase/Analytics'
                        pod 'Firebase/Messaging'
                      end 
                      
                      % flutter clean
                      
                      % cd ios
                      % arch -x86_64 pod update
                      

                      ESLint Definition for rule 'import/extensions' was not found

                      copy iconCopydownload iconDownload
                      "plugins": ["import"],
                      
                      "extends": ["plugin:import/recommended"]
                      
                      "plugins": ["import"],
                      
                      "extends": ["plugin:import/recommended"]
                      
                           "eslint": "^8.7.0",
                      +    "eslint-config-airbnb-base": "^15.0.0",
                           "eslint-config-airbnb-typescript": "^16.1.0",
                      +    "eslint-plugin-import": "^2.25.4",
                      
                         plugins: ["@typescript-eslint"],
                         extends: [
                      +    "airbnb-base",
                           "airbnb-typescript/base",
                      
                           "eslint": "^8.7.0",
                      +    "eslint-config-airbnb-base": "^15.0.0",
                           "eslint-config-airbnb-typescript": "^16.1.0",
                      +    "eslint-plugin-import": "^2.25.4",
                      
                         plugins: ["@typescript-eslint"],
                         extends: [
                      +    "airbnb-base",
                           "airbnb-typescript/base",
                      
                      {
                        "compilerOptions": {
                          "allowSyntheticDefaultImports": true,
                        }
                      }
                      

                      Which rows/columns are duplicates of which others in R matrices?

                      copy iconCopydownload iconDownload
                      l <- asplit(x, 1L)
                      match(l, l)
                      
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      l <- asplit(x, 1L)
                      match(l, l)
                      
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      l <- do.call(paste, data.frame(x))
                      match(l, l)
                      [1] 1 1 3 3 5 5 1 3 9 1
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      library(microbenchmark)
                      library(data.table)
                      getDTthreads() # 4
                      
                      f_asplit <- function(x) {
                        l <- asplit(x, 1L)
                        match(l, l) }
                      
                      f_paste <- function(x) {
                        s <- do.call(paste, as.data.frame(x))
                        match(s, s) }
                      
                      f_interaction <- function(x) {
                        z <- as.integer(interaction(as.data.frame(x)))
                        match(z, z) }
                      
                      f_grouping <- function(x) {
                        g <- do.call(grouping, as.data.frame(x))
                        o <- order(g, method = "radix")
                        e <- attr(g, "ends")
                        z <- rep.int(seq_along(e), c(e[1L], e[-1L] - e[-length(e)]))[o]
                        match(z, z) }
                      
                      f_join <- function(x) {
                        d <- as.data.table(x)
                        d[d, on = names(d), mult = "first", which = TRUE] }
                      
                      f_frank_average <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "average")
                        match(r, r) }
                      
                      f_frank_dense <- function(x) {
                        d <- as.data.table(x)
                        r <- frank(d, ties.method = "dense")
                        match(r, r) }
                      
                      Rcpp::sourceCpp('<copy source code from @MikaelJagan\'s answer here>')
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2L)
                      
                      microbenchmark(
                        f_asplit(x), 
                        f_paste(x), 
                        f_interaction(x),
                        f_grouping(x),
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr         min          lq        mean     median          uq         max neval
                              f_asplit(x) 17369.93905 18861.91195 19070.21298 19013.0180 19207.29194 22420.71085    10
                               f_paste(x)   502.63884   507.35077   509.01823   509.2443   511.72301   515.10083    10
                         f_interaction(x)   234.19311   236.52494   241.80098   238.7392   242.32923   259.75644    10
                            f_grouping(x)   182.25226   182.89358   187.09642   184.6124   187.10444   208.15532    10
                                f_join(x)   119.43460   120.86829   123.16607   122.9332   125.07169   128.44722    10
                       f_frank_average(x)   104.40150   107.53607   111.00268   108.5597   116.80375   121.83675    10
                         f_frank_dense(x)    86.60926    88.29555    91.42976    90.4716    92.32413    99.30659    10
                                f_rcpp(x)   459.02304   464.79855   472.43669   468.2492   470.25508   523.06734    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 20L)
                      
                      system.time(f_interaction(x))
                      
                      Error: cannot allocate vector of size 7.5 Gb
                      Timing stopped at: 173.2 6.05 200.4
                      
                      microbenchmark(
                        f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr          min           lq         mean       median           uq          max neval
                              f_asplit(x)   5416.08762   5681.23523   5731.89246   5732.31779   5905.44517   5913.77141    10
                               f_paste(x)    592.92990    604.15083    629.31101    623.78679    637.81814    724.83871    10
                            f_grouping(x)     63.89522     64.14134     65.42723     65.11530     66.00557     68.06045    10
                                f_join(x)    340.73722    342.18096    353.35774    352.08861    359.88480    382.13480    10
                       f_frank_average(x)     69.90496     70.81840     72.29819     72.04409     73.11977     77.44347    10
                         f_frank_dense(x)     52.58033     53.33760     54.42029     54.01672     55.63532     56.99664    10
                                f_rcpp(x) 184096.21999 184816.36584 185774.76817 186218.58335 186696.31674 186781.24972    10
                      
                      set.seed(1L)
                      x <- matrix(sample(10L, size = 1e+07L, replace = TRUE), ncol = 2000L)
                      
                      microbenchmark(
                        ## f_asplit(x),
                        f_paste(x),
                        ## f_interaction(x),
                        f_grouping(x), 
                        f_join(x),
                        f_frank_average(x),
                        f_frank_dense(x),
                        ## f_rcpp(x),
                        times = 10L,
                        check = "identical",
                        setup = gc(FALSE)
                      )
                      
                      Unit: milliseconds
                                     expr        min         lq       mean     median         uq        max neval
                               f_paste(x) 1067.47994 1075.45148 1083.17391 1080.72997 1089.74027 1102.45249    10
                            f_grouping(x)   19.24007   19.50026   19.86404   19.79002   20.25302   20.60127    10
                                f_join(x)  616.66706  621.29854  630.61460  628.16315  636.39097  650.16180    10
                       f_frank_average(x)   59.82007   61.41706   62.68610   62.99318   64.56520   64.88463    10
                         f_frank_dense(x)   58.03648   60.59857   63.50526   61.99278   66.03694   71.30638    10
                      
                      > ave(1:nrow(x), x[, 1], x[, 2], FUN = function(v) v[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(m2)))
                      
                      > ave(seq_along(z), z, FUN = function(x) x[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(x)))
                      
                      > match(z, z)
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      set.seed(1)
                      v <- sample(1:10, 1e7, replace = TRUE)
                      m2 <- matrix(v, ncol = 2)
                      
                      microbenchmark(
                        ave1 = {
                          ave(1:nrow(m2), m2[, 1], m2[, 2], FUN = function(v) v[1])
                        },
                        ave2 = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          ave(seq_along(z), z, FUN = function(x) x[1])
                        },
                        match = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          match(z, z)
                        },
                        times = 10L
                      )
                      
                      Unit: milliseconds
                        expr      min       lq     mean   median       uq       max neval
                        ave1 648.0755 655.9521 715.8848 701.1927 747.4759  885.9838    10
                        ave2 785.4868 883.2935 913.3867 899.1789 929.6571 1050.9020    10
                       match 417.1598 447.3718 507.0462 495.8791 551.9436  625.0841    10
                      
                      > ave(1:nrow(x), x[, 1], x[, 2], FUN = function(v) v[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(m2)))
                      
                      > ave(seq_along(z), z, FUN = function(x) x[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(x)))
                      
                      > match(z, z)
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      set.seed(1)
                      v <- sample(1:10, 1e7, replace = TRUE)
                      m2 <- matrix(v, ncol = 2)
                      
                      microbenchmark(
                        ave1 = {
                          ave(1:nrow(m2), m2[, 1], m2[, 2], FUN = function(v) v[1])
                        },
                        ave2 = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          ave(seq_along(z), z, FUN = function(x) x[1])
                        },
                        match = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          match(z, z)
                        },
                        times = 10L
                      )
                      
                      Unit: milliseconds
                        expr      min       lq     mean   median       uq       max neval
                        ave1 648.0755 655.9521 715.8848 701.1927 747.4759  885.9838    10
                        ave2 785.4868 883.2935 913.3867 899.1789 929.6571 1050.9020    10
                       match 417.1598 447.3718 507.0462 495.8791 551.9436  625.0841    10
                      
                      > ave(1:nrow(x), x[, 1], x[, 2], FUN = function(v) v[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(m2)))
                      
                      > ave(seq_along(z), z, FUN = function(x) x[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(x)))
                      
                      > match(z, z)
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      set.seed(1)
                      v <- sample(1:10, 1e7, replace = TRUE)
                      m2 <- matrix(v, ncol = 2)
                      
                      microbenchmark(
                        ave1 = {
                          ave(1:nrow(m2), m2[, 1], m2[, 2], FUN = function(v) v[1])
                        },
                        ave2 = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          ave(seq_along(z), z, FUN = function(x) x[1])
                        },
                        match = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          match(z, z)
                        },
                        times = 10L
                      )
                      
                      Unit: milliseconds
                        expr      min       lq     mean   median       uq       max neval
                        ave1 648.0755 655.9521 715.8848 701.1927 747.4759  885.9838    10
                        ave2 785.4868 883.2935 913.3867 899.1789 929.6571 1050.9020    10
                       match 417.1598 447.3718 507.0462 495.8791 551.9436  625.0841    10
                      
                      > ave(1:nrow(x), x[, 1], x[, 2], FUN = function(v) v[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(m2)))
                      
                      > ave(seq_along(z), z, FUN = function(x) x[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(x)))
                      
                      > match(z, z)
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      set.seed(1)
                      v <- sample(1:10, 1e7, replace = TRUE)
                      m2 <- matrix(v, ncol = 2)
                      
                      microbenchmark(
                        ave1 = {
                          ave(1:nrow(m2), m2[, 1], m2[, 2], FUN = function(v) v[1])
                        },
                        ave2 = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          ave(seq_along(z), z, FUN = function(x) x[1])
                        },
                        match = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          match(z, z)
                        },
                        times = 10L
                      )
                      
                      Unit: milliseconds
                        expr      min       lq     mean   median       uq       max neval
                        ave1 648.0755 655.9521 715.8848 701.1927 747.4759  885.9838    10
                        ave2 785.4868 883.2935 913.3867 899.1789 929.6571 1050.9020    10
                       match 417.1598 447.3718 507.0462 495.8791 551.9436  625.0841    10
                      
                      > ave(1:nrow(x), x[, 1], x[, 2], FUN = function(v) v[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(m2)))
                      
                      > ave(seq_along(z), z, FUN = function(x) x[1])
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      > z <- as.integer(interaction(as.data.frame(x)))
                      
                      > match(z, z)
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      set.seed(1)
                      v <- sample(1:10, 1e7, replace = TRUE)
                      m2 <- matrix(v, ncol = 2)
                      
                      microbenchmark(
                        ave1 = {
                          ave(1:nrow(m2), m2[, 1], m2[, 2], FUN = function(v) v[1])
                        },
                        ave2 = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          ave(seq_along(z), z, FUN = function(x) x[1])
                        },
                        match = {
                          z <- as.integer(interaction(as.data.frame(m2)))
                          match(z, z)
                        },
                        times = 10L
                      )
                      
                      Unit: milliseconds
                        expr      min       lq     mean   median       uq       max neval
                        ave1 648.0755 655.9521 715.8848 701.1927 747.4759  885.9838    10
                        ave2 785.4868 883.2935 913.3867 899.1789 929.6571 1050.9020    10
                       match 417.1598 447.3718 507.0462 495.8791 551.9436  625.0841    10
                      
                      Rcpp::sourceCpp(code = '
                      #include <Rcpp.h>
                      using namespace Rcpp;
                      
                      // [[Rcpp::export]]
                      IntegerVector f_rcpp(IntegerMatrix x)
                      {
                        int m = x.nrow();
                        int n = x.ncol();
                        IntegerVector res(m);
                        if (n == 0) {
                          res.fill(1);
                        } else {
                          int i, ki, p, kp, j;
                          for (i = 0; i < m; ++i) {
                            for (p = 0; p < i; ++p) {
                              for (j = 0, ki = i, kp = p; j < n; ++j) {
                                if (x[kp] != x[ki]) {
                                  break;
                                }
                                ki += m;
                                kp += m;
                              }
                              if (j == n) {
                                res[i] = p + 1;
                                break;
                              }
                            }
                            if (p == i) {
                              res[i] = i + 1;
                            }
                          }
                        }
                        return res;
                      }
                      ')
                      
                      f_rcpp(x)
                      
                       [1] 1 1 3 3 5 5 1 3 9 1
                      
                      Rcpp::sourceCpp(code = '
                      #include <Rcpp.h>
                      using namespace Rcpp;
                      
                      // [[Rcpp::export]]
                      IntegerVector f_rcpp(IntegerMatrix x)
                      {
                        int m = x.nrow();
                        int n = x.ncol();
                        IntegerVector res(m);
                        if (n == 0) {
                          res.fill(1);
                        } else {
                          int i, ki, p, kp, j;
                          for (i = 0; i < m; ++i) {
                            for (p = 0; p < i; ++p) {
                              for (j = 0, ki = i, kp = p; j < n; ++j) {
                                if (x[kp] != x[ki]) {
                                  break;
                                }
                                ki += m;
                                kp += m;
                              }
                              if (j == n) {
                                res[i] = p + 1;
                                break;
                              }
                            }
                            if (p == i) {
                              res[i] = i + 1;
                            }
                          }
                        }
                        return res;
                      }
                      ')
                      
                      f_rcpp(x)
                      
                       [1] 1 1 3 3 5 5 1 3 9 1
                      

                      Cypress Test Runner unexpectedly exited via a exit event with signal SIGSEGV in circleCI

                      copy iconCopydownload iconDownload
                      npm install cypress@8.3.0
                      

                      Print first few and last few lines of file through a pipe with &quot;...&quot; in the middle

                      copy iconCopydownload iconDownload
                      (head -n 2; echo "..."; tail -n 2) < file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      (head -n 2; echo "..."; tail -n 2) < file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v top=2 -v bot=2 'FNR == NR {++n; next} FNR <= top || FNR > n-top; FNR == top+1 {print "..."}' file{,}
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v head=2 -v tail=2 'FNR==NR && FNR<=head
                      FNR==NR && cnt++==head {print "..."}
                      NR>FNR && FNR>(cnt-tail)' file file
                      
                      perl -0777 -lanE 'BEGIN{$head=2; $tail=2;}
                      END{say join("\n", @F[0..$head-1],("..."),@F[-$tail..-1]);}' file   
                      
                      awk -v head=2 -v tail=2 'FNR<=head
                      {lines[FNR]=$0}
                      END{
                          print "..."
                          for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
                      }' file
                      
                      head -2 file; echo "..."; tail -2 file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v head=2 -v tail=2 'FNR==NR && FNR<=head
                      FNR==NR && cnt++==head {print "..."}
                      NR>FNR && FNR>(cnt-tail)' file file
                      
                      perl -0777 -lanE 'BEGIN{$head=2; $tail=2;}
                      END{say join("\n", @F[0..$head-1],("..."),@F[-$tail..-1]);}' file   
                      
                      awk -v head=2 -v tail=2 'FNR<=head
                      {lines[FNR]=$0}
                      END{
                          print "..."
                          for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
                      }' file
                      
                      head -2 file; echo "..."; tail -2 file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v head=2 -v tail=2 'FNR==NR && FNR<=head
                      FNR==NR && cnt++==head {print "..."}
                      NR>FNR && FNR>(cnt-tail)' file file
                      
                      perl -0777 -lanE 'BEGIN{$head=2; $tail=2;}
                      END{say join("\n", @F[0..$head-1],("..."),@F[-$tail..-1]);}' file   
                      
                      awk -v head=2 -v tail=2 'FNR<=head
                      {lines[FNR]=$0}
                      END{
                          print "..."
                          for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
                      }' file
                      
                      head -2 file; echo "..."; tail -2 file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v head=2 -v tail=2 'FNR==NR && FNR<=head
                      FNR==NR && cnt++==head {print "..."}
                      NR>FNR && FNR>(cnt-tail)' file file
                      
                      perl -0777 -lanE 'BEGIN{$head=2; $tail=2;}
                      END{say join("\n", @F[0..$head-1],("..."),@F[-$tail..-1]);}' file   
                      
                      awk -v head=2 -v tail=2 'FNR<=head
                      {lines[FNR]=$0}
                      END{
                          print "..."
                          for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
                      }' file
                      
                      head -2 file; echo "..."; tail -2 file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      awk -v head=2 -v tail=2 'FNR==NR && FNR<=head
                      FNR==NR && cnt++==head {print "..."}
                      NR>FNR && FNR>(cnt-tail)' file file
                      
                      perl -0777 -lanE 'BEGIN{$head=2; $tail=2;}
                      END{say join("\n", @F[0..$head-1],("..."),@F[-$tail..-1]);}' file   
                      
                      awk -v head=2 -v tail=2 'FNR<=head
                      {lines[FNR]=$0}
                      END{
                          print "..."
                          for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
                      }' file
                      
                      head -2 file; echo "..."; tail -2 file
                      
                      1
                      2
                      ...
                      9
                      10
                      
                      sed '1,2b
                           3c\
                      ...
                           N
                           $!D'
                      
                      sed '1,2b
                           3c\
                      ...
                           $!{h;d;}
                           H;g'
                      
                      sed '1,2b
                           3c\
                      ...
                           N
                           $!D'
                      
                      sed '1,2b
                           3c\
                      ...
                           $!{h;d;}
                           H;g'
                      
                      h=2 t=3
                      
                      cat temp | awk -v head=${h} -v tail=${t} '
                          { if (NR <= head) print $0
                            lines[NR % tail] = $0
                          }
                      
                      END { print "..."
                      
                            if (NR < tail) i=0
                            else           i=NR
                      
                            do { i=(i+1)%tail
                                 print lines[i]
                               } while (i != (NR % tail) )
                          }'
                      
                      1
                      2
                      ...
                      8
                      9
                      10
                      
                      $ cat temp4
                      1
                      2
                      3
                      4
                      
                      $ cat temp4 | awk -v head=${h} -v tail=${t} '...'
                      1
                      2
                      3
                      ...
                      2
                      3
                      4
                      
                      h=2 t=3
                      
                      cat temp | awk -v head=${h} -v tail=${t} '
                          { if (NR <= head) print $0
                            lines[NR % tail] = $0
                          }
                      
                      END { print "..."
                      
                            if (NR < tail) i=0
                            else           i=NR
                      
                            do { i=(i+1)%tail
                                 print lines[i]
                               } while (i != (NR % tail) )
                          }'
                      
                      1
                      2
                      ...
                      8
                      9
                      10
                      
                      $ cat temp4
                      1
                      2
                      3
                      4
                      
                      $ cat temp4 | awk -v head=${h} -v tail=${t} '...'
                      1
                      2
                      3
                      ...
                      2
                      3
                      4
                      
                      h=2 t=3
                      
                      cat temp | awk -v head=${h} -v tail=${t} '
                          { if (NR <= head) print $0
                            lines[NR % tail] = $0
                          }
                      
                      END { print "..."
                      
                            if (NR < tail) i=0
                            else           i=NR
                      
                            do { i=(i+1)%tail
                                 print lines[i]
                               } while (i != (NR % tail) )
                          }'
                      
                      1
                      2
                      ...
                      8
                      9
                      10
                      
                      $ cat temp4
                      1
                      2
                      3
                      4
                      
                      $ cat temp4 | awk -v head=${h} -v tail=${t} '...'
                      1
                      2
                      3
                      ...
                      2
                      3
                      4
                      
                      h=2 t=3
                      
                      cat temp | awk -v head=${h} -v tail=${t} '
                          { if (NR <= head) print $0
                            lines[NR % tail] = $0
                          }
                      
                      END { print "..."
                      
                            if (NR < tail) i=0
                            else           i=NR
                      
                            do { i=(i+1)%tail
                                 print lines[i]
                               } while (i != (NR % tail) )
                          }'
                      
                      1
                      2
                      ...
                      8
                      9
                      10
                      
                      $ cat temp4
                      1
                      2
                      3
                      4
                      
                      $ cat temp4 | awk -v head=${h} -v tail=${t} '...'
                      1
                      2
                      3
                      ...
                      2
                      3
                      4
                      

                      Selecting such vector elements so that the sum of elements is exactly equal to the specified value

                      copy iconCopydownload iconDownload
                      #install.packages("gbp")
                      library(gbp)
                      #> Loading required package: magrittr
                      #> Loading required package: data.table
                      x <- c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      
                      test <- gbp1d_solver_dpp(p = x, w = x, c = 23745L)
                      
                      list_of_selected_items <- x[as.logical(test$k)]
                      list_of_selected_items
                      #> [1]  236   51  308  458 5486 4290   31 3533 9352
                      sum(list_of_selected_items)
                      #> [1] 23745
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findSumm = function(x, sfind, nmax=1, tmax=1){
                        if(sum(x)<sfind) stop("Impossible solution! sum(x)<sfind!")
                      
                        fTimeSec = function() as.numeric(Sys.time()-l$tstart, units="secs")
                        #The current selection of vector element
                        sel = c(TRUE, rep(FALSE, length(x)-1))
                        #List of intermediate states of the vector sel
                        lsel = list()
                        #List with a collection of parameters and results
                        l = list(
                          x = sort(x, TRUE),
                          tstart = Sys.time(),
                          chosen = list(),
                          xfind = list(),
                          time = c(),
                          stop = FALSE,
                          reason = "")
                      
                        while(TRUE) {
                          #Maximum Runtime Test
                          if(fTimeSec()>tmax) {
                            l$reason = "Calculation time is greater than tmax.\n"
                            l$stop = TRUE
                            break
                          }
                      
                          #Record the solution and test the number of solutions
                          if(sum(l$x[sel])==sfind){
                            #Save solution
                            l$chosen[[length(l$chosen)+1]] = sel
                            l$xfind[[length(l$xfind)+1]] = l$x[sel]
                            l$time = c(l$time, fTimeSec())
                      
                            #Test the number of solutions
                            if(length(l$chosen)==nmax){
                              l$reason = "Already found nmax solutions.\n"
                              l$stop = TRUE
                              break
                            }
                          }
                      
                          idx = which(sel)
                          if(idx[length(idx)]==length(sel)) {
                            if(length(lsel)==0) break
                            sel=lsel[[length(lsel)]]
                            idx = which(sel)
                            lsel[length(lsel)]=NULL
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                      
                          if(sum(l$x[sel])>=sfind){
                            sel[idx[length(idx)]]=FALSE
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          } else {
                            lsel[[length(lsel)+1]] = sel  #Save the current state of sel vector
                            sel[idx[length(idx)]+1]=TRUE
                            next
                          }
                        }
                        if(length(l$chosen)==0 & !l$stop) stop("No solutions!")
                      
                        l$reason = paste(l$reason, "Found", length(l$chosen),
                                         "solutions in time", signif(fTimeSec(), 3), "seconds.\n")
                        cat(l$reason)
                        return(l)
                      }
                      
                      findSumm(1:5, 20)$xfind
                      #Error in findSumm(1:5, 20) : Impossible solution! sum(x)<sfind!
                      
                      findSumm(c(1,2,7), 5)$xfind
                      #Error in findSumm(c(1, 2, 7), 5) : No solutions!
                      
                      findSumm(1:5, 14, 10, 10)$xfind
                      # Found 1 solutions in time 0.007 seconds.
                      # [[1]]
                      # [1] 5 4 3 2
                      
                      findSumm(1:5, 5, 10, 10)$xfind
                      # Found 3 solutions in time 0.001 seconds.
                      # [[1]]
                      # [1] 5
                      #
                      # [[2]]
                      # [1] 4 1
                      #
                      # [[3]]
                      # [1] 3 2
                      
                      findSumm(1:5, 7, 10, 10)$xfind
                      # Found 3 solutions in time 0.004 seconds.
                      # [[1]]
                      # [1] 5 2
                      #
                      # [[2]]
                      # [1] 4 3
                      #
                      # [[3]]
                      # [1] 4 2 1
                      
                      x= c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                        31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                        3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                        9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                        96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                        5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                        15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                        7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                        88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                        4910L, 8984L, 465L)
                      
                      findSumm(x, 23745, 1, 10)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.008 seconds.
                      # [1] 9789 9787 4091   77    1
                      
                      l = findSumm(x, 23745, +Inf, 60)
                      library(tidyverse)
                      library(ggpmisc)
                      df = tibble(
                        n = 1:length(l$chosen),
                        t = l$time
                      )
                      
                      df %>% ggplot(aes(t,n))+
                        geom_line(size=0.1)+
                        geom_smooth(method = lm, formula = y~x)+
                        stat_poly_eq(formula = y~x,
                                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                                     parse = TRUE)
                      
                      library(microbenchmark)
                      ggplot2::autoplot(microbenchmark(findSumm(x, 23745), 
                                                       gbp1d_solver_dpp(p = x, w = x, c = 23745L), 
                                                       times=100))
                      
                      x=c(234L, 1891L, 3187L, 38417L, 2155L, 6857L, 71692L, 463575L, 
                          800L, 2195L, 820L, 9735L, 913L, 62685L, 920597L, 864L, 903L, 
                          478L, 2828L, 99371L, 3109L, 379L, 8544L, 444L, 772L, 571L, 226L, 
                          94L, 378L, 60253L, 10920L, 47626L, 671L, 45163L, 27767L, 62498L, 
                          87706L, 4966L, 4615L, 14897L, 261L, 684L, 3780L, 97L, 705L, 7313L, 
                          3629L, 436L, 5076L, 3198L, 731L, 56634L, 67411L, 249L, 403L, 
                          82728L, 9986L, 643662L, 11045L, 934L, 8154L, 289L, 4452L, 624L, 
                          4876L, 86859L, 933L, 2372L, 6493L, 773566L, 6599L, 459L, 2024L, 
                          80425L, 591L, 6262L, 35033L, 89607L, 6435L, 14917L, 9559L, 67983L, 
                          82365L, 88127L, 466L, 758L, 11605L, 828L, 410L, 557L, 2991L, 
                          808L, 8512L, 273605L, 294L, 4666L, 27L, 26337L, 7340L, 682L, 
                          46480L, 19903L, 699L, 700L, 58L, 136L, 852L, 909L, 64316L, 9109L, 
                          876L, 6382L, 803L, 295L, 9539L, 26271L, 1906L, 23639L, 9022L, 
                          9513L, 169L, 65427L, 861864L, 743L, 91L, 9039L, 247L, 58749L, 
                          5674L, 65959L, 99126L, 7765L, 5934L, 13881L, 77696L, 66894L, 
                          977L, 6279L, 46273L, 919L, 6307L, 316L, 420113L, 61336L, 70L, 
                          6148L, 257L, 17804L, 14L, 989L, 16907L, 36L, 25L, 333L, 224L, 
                          119L, 4000L, 9438L, 5439L, 748L, 16532L, 4847L, 939L, 9504L, 
                          2782L, 424L, 64034L, 5306L, 30247L, 6636L, 3976L, 60588L, 180L, 
                          78118L, 1L, 61866L, 9501L, 15834L, 66712L, 77219L, 448L, 612L, 
                          5339L, 58413L, 4785L, 2191L, 35711L, 84383L, 6261L, 896L, 24353L, 
                          54868L, 288L, 8059L, 867L, 687L, 94667L, 1713L, 1507L, 71048L, 
                          882L, 4155L, 97230L, 49492L, 47839L, 793L, 263L, 63160L, 9062L, 
                          3518L, 55956L, 6626L, 14619L, 636L, 1127L, 970L, 5512L, 118117L, 
                          2370L, 802L, 98333L, 6089L, 1076L, 80L, 305L, 3995L, 437L, 49L, 
                          9207L, 2021L, 7554L, 9486L, 33501L, 55745L, 967L, 24857L, 692L, 
                          4148L, 464957L, 2381L, 3876L, 3246L, 1478L, 308L, 98068L, 532L, 
                          4670L, 7965L, 940L, 467L, 777L, 68749L, 2739L, 23951L, 831L, 
                          60763L, 12047L, 75620L, 650L, 69584L, 294122L, 41149L, 9657L, 
                          780L, 153054L, 37990L, 16L, 894L, 15500L, 31873L, 3800L, 472L, 
                          50989L, 8767L, 8209L, 2929L, 4751L, 38L, 47403L, 64941L, 28042L, 
                          49020L, 81785L, 299L, 936L, 63136L, 3L, 42033L, 1750L, 1147L, 
                          273L, 62668L, 41L, 5829L, 686L, 511L, 65019L, 842L, 88716L, 96217L, 
                          9442L, 6324L, 197L, 55422L, 630L, 665L, 3921L, 726L, 766916L, 
                          43944L, 9035L, 573L, 77942L, 29689L, 749L, 95240L, 281L, 1933L, 
                          78265L, 812L, 854L, 17445L, 8855L, 2940L, 6057L, 46689L, 999L, 
                          381L, 347L, 50199L, 161L, 534L, 804L, 99043L, 13183L, 679L, 432L, 
                          38887L, 575L, 781L, 2023L, 187077L, 89498L, 85L, 16780L, 3731L, 
                          45904L, 13861L, 3971L, 301L, 4175L, 9427L, 126913L, 845L, 175L, 
                          1684L, 9064L, 56647L, 116L, 479672L, 6754L, 441L, 412L, 97091L, 
                          4062L, 598L, 146L, 423L, 2715L, 198939L, 80577L, 76385L, 2088L, 
                          139L, 647L, 246L, 85002L, 898L, 50939L, 135L, 46388L, 623L, 17928L, 
                          63072L, 346L, 78582L, 16691L, 838L, 44L, 5181L, 7918L, 3650L, 
                          35L, 8825L, 9758L, 22677L, 9838L, 2239L, 9001L, 96689L, 570L, 
                          47373L, 507L, 6378L, 40839L, 11677L, 937874L, 2485L, 22188L, 
                          20413L, 13L, 877L, 5578L, 428L, 61L, 3200L, 5444L, 85540L, 640L, 
                          94460L, 310L, 6043L, 3771L, 6167L, 476L, 9365L, 1956L, 143L, 
                          7841L, 4957L, 3309L, 9317L, 41434L, 97881L, 51853L, 474L, 3098L, 
                          7109L, 93976L, 545L, 28475L, 2066L, 4959L, 7410L, 293L, 8246L, 
                          43L, 721L, 2260L, 72854L, 100L, 61382L, 107L, 5637L, 891L, 256L, 
                          442L, 84440L, 55792L, 195L, 24074L, 19L, 57376L, 59159L, 805253L, 
                          193329L, 3636L, 98954L, 968L, 380L, 5203L, 90157L, 71907L, 35497L, 
                          41769L, 1683L, 1984L, 5765L, 832L, 411L, 4888L, 9801L, 710L, 
                          2325L, 40L, 32927L, 435L, 66L, 66301L, 94776L, 48234L, 28977L, 
                          122312L, 48L, 359L, 572L, 753L, 945L, 32241L, 328L, 55976L, 128L, 
                          815794L, 57894L, 576L, 60131L, 342448L, 8913L, 33506L, 20448L, 
                          58750L, 637L, 82086L, 635710L, 96772L, 272L, 938L, 4863L, 737L, 
                          949L, 4804L, 3446L, 92319L, 28883L, 6032L, 53970L, 9394L, 5630L, 
                          71583L, 136862L, 23161L, 8545L, 54249L, 213666L, 668L, 893L, 
                          881126L, 8252L, 584L, 83L, 13754L, 244156L, 530L, 64574L, 22009L, 
                          89204L, 34992L, 85992L, 82697L, 50L, 95845L, 3096L, 42L, 554949L, 
                          325L, 2092L, 28L, 3830L, 893583L, 625L, 3740L, 4513L, 9938L, 
                          910L, 8868L, 9614L, 41281L, 27915L, 25839L, 4417L, 5730L, 2825L, 
                          683L, 550L, 88838L, 9248L, 961L, 2748L, 7259L, 53220L, 2179L, 
                          4036L, 46014L, 83725L, 8211L, 6957L, 6886L, 4653L, 6300L, 80437L, 
                          135885L, 23745L, 9536L, 78L, 652590L, 1037L, 5293L, 492L, 7467L, 
                          71685L, 890L, 5023L, 96524L, 17465L, 53665L, 21508L, 463L, 159L, 
                          311L, 764L, 27534L, 71L, 2504L, 270L, 6449L, 13449L, 302L, 88L, 
                          3893L, 22007L, 9208L, 680618L, 878L, 14721L, 20L, 322374L, 644L, 
                          944669L, 57334L, 233L, 982L, 870L, 950L, 121L, 254L, 4226L, 45L, 
                          61823L, 9626L, 58590L, 6552L, 3920L, 68L, 3644L, 35775L, 4591L, 
                          636207L, 78314L, 408L, 371L, 984L, 7089L, 4679L, 2233L, 756L, 
                          20527L, 178L, 80573L, 589923L, 120L, 7938L, 894842L, 6563L, 569L, 
                          91110L, 620L, 786288L, 46022L, 396L, 762533L, 145964L, 7732L, 
                          60L, 274L, 87869L, 227L, 6706L, 707L, 955L, 48246L, 771L, 29001L, 
                          14224L, 5173L, 20215L, 7566L, 1564L, 733L, 3568L, 3570L, 39256L, 
                          925L, 41577L, 348L, 68267L, 151L, 98572L, 1389L, 5421L, 69043L, 
                          42434L, 27597L, 53320L, 46051L, 1686L, 59L, 361L, 747579L, 5044L, 
                          73873L, 28894L, 8146L, 353L, 2622L, 664L, 349L, 90764L, 8920L, 
                          716L, 14903L, 96055L, 89L, 94239L, 416L, 7896L, 232L, 5543L, 
                          61664L, 6709L, 2L, 14275L, 2954L, 917416L, 3567L, 42086L, 99956L, 
                          86112L, 206L, 64L, 25956L, 57112L, 425L, 6507L, 28034L, 991L, 
                          8444L, 140L, 1461L, 68783L, 347633L, 87696L, 593L, 164L, 837L, 
                          8793L, 965L, 8811L, 97412L, 351L, 23L, 66808L, 8308L, 14245L, 
                          12519L, 3019L, 1920L, 813L, 485L, 979L, 929L, 2970L, 32447L, 
                          8962L, 867973L, 40534L, 551L, 20941L, 49413L, 188L, 948L, 9018L, 
                          187252L, 3919L, 45963L, 358L, 7211L, 959L, 47L, 4220L, 36086L, 
                          1645L, 33056L, 300L, 29682L, 9152L, 431L, 364L, 2211L, 3779L, 
                          4633L, 22500L, 33980L, 794L, 84558L, 488L, 732L, 6686L, 15042L, 
                          906L, 13553L, 6115L, 153L, 866L, 3624L, 329L, 6875L, 86L, 6298L, 
                          57424L, 17582L, 955879L, 40945L, 4858L, 694L, 755L, 499L, 406L, 
                          564L, 874L, 1695L, 43961L, 578L, 9063L, 505L, 5856L, 4484L, 76708L, 
                          712L, 23348L, 986L, 275L, 996L, 8966L, 220L, 7008L, 849L, 953460L, 
                          3062L, 278L, 26L, 8547L, 16895L, 98289L, 815L, 25135L, 956L, 
                          370L, 8221L, 72674L, 31711L, 73L, 41667L, 2915L, 797L, 41309L, 
                          4257L, 8148L, 5723L, 2124L, 8306L, 53388L, 33520L, 680L, 893759L, 
                          40133L, 94791L, 988L, 162L, 79366L, 37625L, 7125L, 50947L, 171L, 
                          99558L, 166L, 90717L, 5807L, 606L, 98592L, 59207L, 966L, 61299L, 
                          7553L, 9678L, 62322L, 156L, 267L, 8478L, 59554L, 2264L, 28338L, 
                          899L, 9719L, 98L, 51403L, 6302L, 265L, 79929L, 101L, 5227L, 972L, 
                          145L, 48018L, 90140L, 698L, 8L, 5751L, 26083L, 1295L, 78124L, 
                          383L, 2776L, 80204L, 210L, 3422L, 36064L, 46L, 4953L, 20271L, 
                          3916L, 767L, 601372L, 56575L, 5237L, 5621L, 6705L, 1191L, 63768L, 
                          1016L, 313L, 2285L, 12489L, 2755L, 338L, 7518L, 2630L, 421L, 
                          6554L, 306L, 113L, 57197L, 885L, 9445L, 37364L, 86630L, 2460L, 
                          715L, 10829L, 9914L, 6635L, 229L, 525L, 839L, 3278L, 969L, 182L, 
                          187L, 7022L, 554L, 6489L, 15791L, 4157L, 47048L, 9447L, 152L, 
                          1419L, 22618L, 5194L, 609L, 923L, 768L, 6248L, 714L, 1159L, 825893L, 
                          53492L, 19731L, 65167L, 96325L, 336L, 4443L, 843L, 62960L, 9788L, 
                          35032L, 284L, 4647L, 360L, 11297L, 1515L)
                      
                      findSumm(x, 9568447L)$xfind[[1]]
                      # Already found nmax solutions.
                      # Found 1 solutions in time 0.065 seconds.
                      # [1] 955879 953460 944669 937874 920597 917416 894842 893759 893583 881126 347633  27597      8      3      1
                      
                      findAllSubsets <- function(v, s, r = c()) {
                        if (s == 0) {
                          return(list(r))
                        } else {
                          res <- list()
                          v <- v[v<=s]
                          for (k in seq_along(v)) {
                            if (length(r) == 0 || (tail(r, 1) >= v[k] & sum(v[k:length(v)]) >= s)) {
                              res[[k]] <- Recall(v[-k], s - v[k], c(r, v[k]))
                            }
                          }
                          return(unlist(res, recursive = FALSE))
                        }
                      }
                      
                      > x <- 1:10
                      
                      > S <- 23
                      
                      > findAllSubsets(x, S)
                      [[1]]
                      [1] 7 6 4 3 2 1
                      
                      [[2]]
                      [1] 7 6 5 3 2
                      
                      [[3]]
                      [1] 7 6 5 4 1
                      
                      [[4]]
                      [1] 8 5 4 3 2 1
                      
                      [[5]]
                      [1] 8 6 4 3 2
                      
                      [[6]]
                      [1] 8 6 5 3 1
                      
                      [[7]]
                      [1] 8 6 5 4
                      
                      [[8]]
                      [1] 8 7 4 3 1
                      
                      [[9]]
                      [1] 8 7 5 2 1
                      
                      [[10]]
                      [1] 8 7 5 3
                      
                      [[11]]
                      [1] 8 7 6 2
                      
                      [[12]]
                      [1] 9 5 4 3 2
                      
                      [[13]]
                      [1] 9 6 4 3 1
                      
                      [[14]]
                      [1] 9 6 5 2 1
                      
                      [[15]]
                      [1] 9 6 5 3
                      
                      [[16]]
                      [1] 9 7 4 2 1
                      
                      [[17]]
                      [1] 9 7 4 3
                      
                      [[18]]
                      [1] 9 7 5 2
                      
                      [[19]]
                      [1] 9 7 6 1
                      
                      [[20]]
                      [1] 9 8 3 2 1
                      
                      [[21]]
                      [1] 9 8 4 2
                      
                      [[22]]
                      [1] 9 8 5 1
                      
                      [[23]]
                      [1] 9 8 6
                      
                      [[24]]
                      [1] 10  5  4  3  1
                      
                      [[25]]
                      [1] 10  6  4  2  1
                      
                      [[26]]
                      [1] 10  6  4  3
                      
                      [[27]]
                      [1] 10  6  5  2
                      
                      [[28]]
                      [1] 10  7  3  2  1
                      
                      [[29]]
                      [1] 10  7  4  2
                      
                      [[30]]
                      [1] 10  7  5  1
                      
                      [[31]]
                      [1] 10  7  6
                      
                      [[32]]
                      [1] 10  8  3  2
                      
                      [[33]]
                      [1] 10  8  4  1
                      
                      [[34]]
                      [1] 10  8  5
                      
                      [[35]]
                      [1] 10  9  3  1
                      
                      [[36]]
                      [1] 10  9  4
                      
                      subsetS <- function(x, target) {
                        v <- x[x <= target]
                        with(adagio::subsetsum(v, target), v[inds])
                      }
                      
                      > subsetS(x,23745)
                      [1]  236   51  308  458 5486 4290   31 3533 9352
                      
                      set.seed(0)
                      x <- sample(100000L,200)
                      
                      target <- 237450
                      ggplot2::autoplot(microbenchmark(
                        findSumm(x, target),
                        gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        times = 100
                      ))
                      
                      findAllSubsets <- function(v, s, r = c()) {
                        if (s == 0) {
                          return(list(r))
                        } else {
                          res <- list()
                          v <- v[v<=s]
                          for (k in seq_along(v)) {
                            if (length(r) == 0 || (tail(r, 1) >= v[k] & sum(v[k:length(v)]) >= s)) {
                              res[[k]] <- Recall(v[-k], s - v[k], c(r, v[k]))
                            }
                          }
                          return(unlist(res, recursive = FALSE))
                        }
                      }
                      
                      > x <- 1:10
                      
                      > S <- 23
                      
                      > findAllSubsets(x, S)
                      [[1]]
                      [1] 7 6 4 3 2 1
                      
                      [[2]]
                      [1] 7 6 5 3 2
                      
                      [[3]]
                      [1] 7 6 5 4 1
                      
                      [[4]]
                      [1] 8 5 4 3 2 1
                      
                      [[5]]
                      [1] 8 6 4 3 2
                      
                      [[6]]
                      [1] 8 6 5 3 1
                      
                      [[7]]
                      [1] 8 6 5 4
                      
                      [[8]]
                      [1] 8 7 4 3 1
                      
                      [[9]]
                      [1] 8 7 5 2 1
                      
                      [[10]]
                      [1] 8 7 5 3
                      
                      [[11]]
                      [1] 8 7 6 2
                      
                      [[12]]
                      [1] 9 5 4 3 2
                      
                      [[13]]
                      [1] 9 6 4 3 1
                      
                      [[14]]
                      [1] 9 6 5 2 1
                      
                      [[15]]
                      [1] 9 6 5 3
                      
                      [[16]]
                      [1] 9 7 4 2 1
                      
                      [[17]]
                      [1] 9 7 4 3
                      
                      [[18]]
                      [1] 9 7 5 2
                      
                      [[19]]
                      [1] 9 7 6 1
                      
                      [[20]]
                      [1] 9 8 3 2 1
                      
                      [[21]]
                      [1] 9 8 4 2
                      
                      [[22]]
                      [1] 9 8 5 1
                      
                      [[23]]
                      [1] 9 8 6
                      
                      [[24]]
                      [1] 10  5  4  3  1
                      
                      [[25]]
                      [1] 10  6  4  2  1
                      
                      [[26]]
                      [1] 10  6  4  3
                      
                      [[27]]
                      [1] 10  6  5  2
                      
                      [[28]]
                      [1] 10  7  3  2  1
                      
                      [[29]]
                      [1] 10  7  4  2
                      
                      [[30]]
                      [1] 10  7  5  1
                      
                      [[31]]
                      [1] 10  7  6
                      
                      [[32]]
                      [1] 10  8  3  2
                      
                      [[33]]
                      [1] 10  8  4  1
                      
                      [[34]]
                      [1] 10  8  5
                      
                      [[35]]
                      [1] 10  9  3  1
                      
                      [[36]]
                      [1] 10  9  4
                      
                      subsetS <- function(x, target) {
                        v <- x[x <= target]
                        with(adagio::subsetsum(v, target), v[inds])
                      }
                      
                      > subsetS(x,23745)
                      [1]  236   51  308  458 5486 4290   31 3533 9352
                      
                      set.seed(0)
                      x <- sample(100000L,200)
                      
                      target <- 237450
                      ggplot2::autoplot(microbenchmark(
                        findSumm(x, target),
                        gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        times = 100
                      ))
                      
                      findAllSubsets <- function(v, s, r = c()) {
                        if (s == 0) {
                          return(list(r))
                        } else {
                          res <- list()
                          v <- v[v<=s]
                          for (k in seq_along(v)) {
                            if (length(r) == 0 || (tail(r, 1) >= v[k] & sum(v[k:length(v)]) >= s)) {
                              res[[k]] <- Recall(v[-k], s - v[k], c(r, v[k]))
                            }
                          }
                          return(unlist(res, recursive = FALSE))
                        }
                      }
                      
                      > x <- 1:10
                      
                      > S <- 23
                      
                      > findAllSubsets(x, S)
                      [[1]]
                      [1] 7 6 4 3 2 1
                      
                      [[2]]
                      [1] 7 6 5 3 2
                      
                      [[3]]
                      [1] 7 6 5 4 1
                      
                      [[4]]
                      [1] 8 5 4 3 2 1
                      
                      [[5]]
                      [1] 8 6 4 3 2
                      
                      [[6]]
                      [1] 8 6 5 3 1
                      
                      [[7]]
                      [1] 8 6 5 4
                      
                      [[8]]
                      [1] 8 7 4 3 1
                      
                      [[9]]
                      [1] 8 7 5 2 1
                      
                      [[10]]
                      [1] 8 7 5 3
                      
                      [[11]]
                      [1] 8 7 6 2
                      
                      [[12]]
                      [1] 9 5 4 3 2
                      
                      [[13]]
                      [1] 9 6 4 3 1
                      
                      [[14]]
                      [1] 9 6 5 2 1
                      
                      [[15]]
                      [1] 9 6 5 3
                      
                      [[16]]
                      [1] 9 7 4 2 1
                      
                      [[17]]
                      [1] 9 7 4 3
                      
                      [[18]]
                      [1] 9 7 5 2
                      
                      [[19]]
                      [1] 9 7 6 1
                      
                      [[20]]
                      [1] 9 8 3 2 1
                      
                      [[21]]
                      [1] 9 8 4 2
                      
                      [[22]]
                      [1] 9 8 5 1
                      
                      [[23]]
                      [1] 9 8 6
                      
                      [[24]]
                      [1] 10  5  4  3  1
                      
                      [[25]]
                      [1] 10  6  4  2  1
                      
                      [[26]]
                      [1] 10  6  4  3
                      
                      [[27]]
                      [1] 10  6  5  2
                      
                      [[28]]
                      [1] 10  7  3  2  1
                      
                      [[29]]
                      [1] 10  7  4  2
                      
                      [[30]]
                      [1] 10  7  5  1
                      
                      [[31]]
                      [1] 10  7  6
                      
                      [[32]]
                      [1] 10  8  3  2
                      
                      [[33]]
                      [1] 10  8  4  1
                      
                      [[34]]
                      [1] 10  8  5
                      
                      [[35]]
                      [1] 10  9  3  1
                      
                      [[36]]
                      [1] 10  9  4
                      
                      subsetS <- function(x, target) {
                        v <- x[x <= target]
                        with(adagio::subsetsum(v, target), v[inds])
                      }
                      
                      > subsetS(x,23745)
                      [1]  236   51  308  458 5486 4290   31 3533 9352
                      
                      set.seed(0)
                      x <- sample(100000L,200)
                      
                      target <- 237450
                      ggplot2::autoplot(microbenchmark(
                        findSumm(x, target),
                        gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        times = 100
                      ))
                      
                      findAllSubsets <- function(v, s, r = c()) {
                        if (s == 0) {
                          return(list(r))
                        } else {
                          res <- list()
                          v <- v[v<=s]
                          for (k in seq_along(v)) {
                            if (length(r) == 0 || (tail(r, 1) >= v[k] & sum(v[k:length(v)]) >= s)) {
                              res[[k]] <- Recall(v[-k], s - v[k], c(r, v[k]))
                            }
                          }
                          return(unlist(res, recursive = FALSE))
                        }
                      }
                      
                      > x <- 1:10
                      
                      > S <- 23
                      
                      > findAllSubsets(x, S)
                      [[1]]
                      [1] 7 6 4 3 2 1
                      
                      [[2]]
                      [1] 7 6 5 3 2
                      
                      [[3]]
                      [1] 7 6 5 4 1
                      
                      [[4]]
                      [1] 8 5 4 3 2 1
                      
                      [[5]]
                      [1] 8 6 4 3 2
                      
                      [[6]]
                      [1] 8 6 5 3 1
                      
                      [[7]]
                      [1] 8 6 5 4
                      
                      [[8]]
                      [1] 8 7 4 3 1
                      
                      [[9]]
                      [1] 8 7 5 2 1
                      
                      [[10]]
                      [1] 8 7 5 3
                      
                      [[11]]
                      [1] 8 7 6 2
                      
                      [[12]]
                      [1] 9 5 4 3 2
                      
                      [[13]]
                      [1] 9 6 4 3 1
                      
                      [[14]]
                      [1] 9 6 5 2 1
                      
                      [[15]]
                      [1] 9 6 5 3
                      
                      [[16]]
                      [1] 9 7 4 2 1
                      
                      [[17]]
                      [1] 9 7 4 3
                      
                      [[18]]
                      [1] 9 7 5 2
                      
                      [[19]]
                      [1] 9 7 6 1
                      
                      [[20]]
                      [1] 9 8 3 2 1
                      
                      [[21]]
                      [1] 9 8 4 2
                      
                      [[22]]
                      [1] 9 8 5 1
                      
                      [[23]]
                      [1] 9 8 6
                      
                      [[24]]
                      [1] 10  5  4  3  1
                      
                      [[25]]
                      [1] 10  6  4  2  1
                      
                      [[26]]
                      [1] 10  6  4  3
                      
                      [[27]]
                      [1] 10  6  5  2
                      
                      [[28]]
                      [1] 10  7  3  2  1
                      
                      [[29]]
                      [1] 10  7  4  2
                      
                      [[30]]
                      [1] 10  7  5  1
                      
                      [[31]]
                      [1] 10  7  6
                      
                      [[32]]
                      [1] 10  8  3  2
                      
                      [[33]]
                      [1] 10  8  4  1
                      
                      [[34]]
                      [1] 10  8  5
                      
                      [[35]]
                      [1] 10  9  3  1
                      
                      [[36]]
                      [1] 10  9  4
                      
                      subsetS <- function(x, target) {
                        v <- x[x <= target]
                        with(adagio::subsetsum(v, target), v[inds])
                      }
                      
                      > subsetS(x,23745)
                      [1]  236   51  308  458 5486 4290   31 3533 9352
                      
                      set.seed(0)
                      x <- sample(100000L,200)
                      
                      target <- 237450
                      ggplot2::autoplot(microbenchmark(
                        findSumm(x, target),
                        gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        times = 100
                      ))
                      
                      findAllSubsets <- function(v, s, r = c()) {
                        if (s == 0) {
                          return(list(r))
                        } else {
                          res <- list()
                          v <- v[v<=s]
                          for (k in seq_along(v)) {
                            if (length(r) == 0 || (tail(r, 1) >= v[k] & sum(v[k:length(v)]) >= s)) {
                              res[[k]] <- Recall(v[-k], s - v[k], c(r, v[k]))
                            }
                          }
                          return(unlist(res, recursive = FALSE))
                        }
                      }
                      
                      > x <- 1:10
                      
                      > S <- 23
                      
                      > findAllSubsets(x, S)
                      [[1]]
                      [1] 7 6 4 3 2 1
                      
                      [[2]]
                      [1] 7 6 5 3 2
                      
                      [[3]]
                      [1] 7 6 5 4 1
                      
                      [[4]]
                      [1] 8 5 4 3 2 1
                      
                      [[5]]
                      [1] 8 6 4 3 2
                      
                      [[6]]
                      [1] 8 6 5 3 1
                      
                      [[7]]
                      [1] 8 6 5 4
                      
                      [[8]]
                      [1] 8 7 4 3 1
                      
                      [[9]]
                      [1] 8 7 5 2 1
                      
                      [[10]]
                      [1] 8 7 5 3
                      
                      [[11]]
                      [1] 8 7 6 2
                      
                      [[12]]
                      [1] 9 5 4 3 2
                      
                      [[13]]
                      [1] 9 6 4 3 1
                      
                      [[14]]
                      [1] 9 6 5 2 1
                      
                      [[15]]
                      [1] 9 6 5 3
                      
                      [[16]]
                      [1] 9 7 4 2 1
                      
                      [[17]]
                      [1] 9 7 4 3
                      
                      [[18]]
                      [1] 9 7 5 2
                      
                      [[19]]
                      [1] 9 7 6 1
                      
                      [[20]]
                      [1] 9 8 3 2 1
                      
                      [[21]]
                      [1] 9 8 4 2
                      
                      [[22]]
                      [1] 9 8 5 1
                      
                      [[23]]
                      [1] 9 8 6
                      
                      [[24]]
                      [1] 10  5  4  3  1
                      
                      [[25]]
                      [1] 10  6  4  2  1
                      
                      [[26]]
                      [1] 10  6  4  3
                      
                      [[27]]
                      [1] 10  6  5  2
                      
                      [[28]]
                      [1] 10  7  3  2  1
                      
                      [[29]]
                      [1] 10  7  4  2
                      
                      [[30]]
                      [1] 10  7  5  1
                      
                      [[31]]
                      [1] 10  7  6
                      
                      [[32]]
                      [1] 10  8  3  2
                      
                      [[33]]
                      [1] 10  8  4  1
                      
                      [[34]]
                      [1] 10  8  5
                      
                      [[35]]
                      [1] 10  9  3  1
                      
                      [[36]]
                      [1] 10  9  4
                      
                      subsetS <- function(x, target) {
                        v <- x[x <= target]
                        with(adagio::subsetsum(v, target), v[inds])
                      }
                      
                      > subsetS(x,23745)
                      [1]  236   51  308  458 5486 4290   31 3533 9352
                      
                      set.seed(0)
                      x <- sample(100000L,200)
                      
                      target <- 237450
                      ggplot2::autoplot(microbenchmark(
                        findSumm(x, target),
                        gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        times = 100
                      ))
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      cc <- \(x, target, warn = TRUE){
                          cc_env <- environment()
                          tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                              if (.Internal(exists("out", cc_env, "integer", TRUE))){
                                  return(cc_env[["out"]])
                              } 
                              if(warn) warning("No subset of x can sum up to target!", call. = F)
                              NA
                          })
                      }
                      aux = \(x, target, env, out = 0L){
                          s <- sum(out)
                          if(s == target) {
                              env[["out"]] <- out[-1L]
                              stop(call. = F)
                          }
                          if(s > target) return() # avoid eternal loop
                          for(i in seq_along(x)){
                              n = x[i]
                              left = x[(i+1L):length(x)]
                              aux(left, target, env, c(out, n)) # recursion here
                          }
                      }
                      
                      > cc(x, y)  
                       [1]  236  407   51  308   72 9787  458 5486   42 4290   31 1102   24  100  669  245   86   77   13   91   25   97    6
                      [24]   14    5   10    1   12
                      
                      library(adagio)
                      library(gbp)
                      x = c(236L, 407L, 51L, 308L, 72L, 9787L, 458L, 5486L, 42L, 4290L,
                           31L, 3533L, 1102L, 24L, 100L, 669L, 9352L, 4091L, 2751L, 3324L,
                           3193L, 245L, 86L, 98932L, 77L, 13L, 9789L, 91L, 999L, 25L, 25379L,
                           9626L, 9092L, 622L, 97L, 57L, 2911L, 6L, 405L, 894L, 1760L, 9634L,
                           96L, 9765L, 223L, 765L, 743L, 5960L, 14L, 50L, 89L, 348L, 5875L,
                           5L, 58602L, 397L, 1181L, 94L, 954L, 7901L, 836L, 8810L, 52L,
                           15L, 48L, 26L, 4L, 66L, 5265L, 80L, 282L, 231L, 76L, 661L, 7604L,
                           7406L, 58L, 10L, 903L, 49446L, 80921L, 1L, 876L, 334L, 63L, 796L,
                           88L, 413L, 1214L, 2983L, 9518L, 595L, 708L, 53L, 321L, 12L, 634L,
                           4910L, 8984L, 465L)
                      target <- 23745L
                      ggplot2::autoplot(microbenchmark::microbenchmark(
                        findSumm(x, target),
                        gbp::gbp1d_solver_dpp(p = x, w = x, c = target),
                        subsetS(x, target),
                        cc(x, target, warn = F), # slightly faster than warn = T
                        times = 100
                      ))
                      
                      > bench::bench_memory(cc(x, target, warn = F))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1    42.2KB <Rprofmem [140 x 3]>
                      > bench::bench_memory(subsetS(x, target))
                      # A tibble: 1 x 2
                        mem_alloc memory              
                        <bch:byt> <list>              
                      1     5.4MB <Rprofmem [346 x 3]>
                      
                      set.seed(1000)
                      x <- sample(100000L, 200L)
                      target <- 237450L
                      
                      x <- c(1L, 2L)
                      target <- 5L # impossible to reach with 1 and 2.
                      
                      > findSumm(x, target)
                      Error in findSumm(x, target) : Impossible solution! sum(x)<sfind!
                      
                      > gbp::gbp1d_solver_dpp(p = x, w = x, c = target)
                      C++ object <0000023130596180> of class 'gbp1d' <000002313b255230>
                      
                      > subsetS(x, target)
                      [1] NA NA
                      Warning message:
                      In adagio::subsetsum(v, target) :
                        Total sum of 'S' is smaller than 't'; no solution possible.
                      
                      > cc(x, target)
                      [1] NA
                      Warning message:
                      No subset of x can sum up to target! 
                      
                      cc_explained <- \(x, target, warn = TRUE){
                        cc_env <- environment()
                        print(environment())
                        print("----")
                        tryCatch(aux(x, target, cc_env), error = \(e){ # R4.1 new anonymous function
                          if (.Internal(exists("out", cc_env, "integer", TRUE))){
                            print("----")
                            print(environment())
                            return(cc_env[["out"]])
                          } 
                          if(warn) warning("No subset of x can sum up to target!", call. = F)
                          NA
                        })
                      }
                      aux = \(x, target, env, out = 0L){
                        print(environment())
                        s <- sum(out)
                        if(s == target) {
                          lobstr::cst()
                          env[["out"]] <- out[-1L]
                          stop(call. = F)
                        }
                        if(s > target) return() # avoid eternal loop
                        for(i in seq_along(x)){
                          n = x[i]
                          left = x[(i+1L):length(x)]
                          aux(left, target, env, c(out, n)) # recursion here
                        }
                      }
                      cc_explained(x,y)
                      
                          x
                        1. \-global::cc_explained(x, y)
                        2.   +-base::tryCatch(...)
                        3.   | \-base:::tryCatchList(expr, classes, parentenv, handlers)
                        4.   |   \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
                        5.   |     \-base:::doTryCatch(return(expr), name, parentenv, handler)
                        6.   \-global::aux(x, target, cc_env)
                        7.     \-global::aux(left, target, env, c(out, n))
                      ...
                       
                       33.                                                         \-global::aux(left, target, env, c(out, n))
                       34.                                                           \-global::aux(left, target, env, c(out, n))
                       35.   
                      
                      library(RcppAlgos)
                      comboGeneral(x, 5,
                                   constraintFun = "sum",
                                   comparisonFun = "==",
                                   limitConstraints = 23745L,
                                   upper = 1L)
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      
                      GetPartition <- function(x, target, n_res = 1L) {
                          
                          len_x <- length(x)
                          n_soln <- 0L
                          res <- list()
                          
                          m <- 1L
                          i <- 1L
                      
                          while (n_soln < n_res || m > len_x) {
                              combs <- comboGeneral(
                                  x, m, constraintFun = "sum",
                                  comparisonFun = "==", limitConstraints = target,
                                  upper = min(choose(len_x, m), n_res - n_soln) 
                              )
                      
                              if (nrow(combs)) {
                                  n_soln <- n_soln + nrow(combs)
                                  res[[i]] <- combs
                                  i <- i + 1L
                              }
                              
                              m <- m + 1L
                          }
                          
                          return(res)
                      }
                      
                      > GetPartition(x, target = 23745L, n_res = 13L)
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> [2,]   76 5875 8810 8984
                      #> [3,]   86 4910 8984 9765
                      #> [4,]   97 5486 8810 9352
                      #> [5,]   97 5960 7901 9787
                      #> [6,]  100 4091 9765 9789
                      #> [7,]  231 4091 9634 9789
                      #> [8,]  236 4910 8810 9789
                      #> [9,]  465 5486 8810 8984
                      #> 
                      #> [[2]]
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      #> [2,]    1   63 4290 9626 9765
                      #> [3,]    1   63 4910 8984 9787
                      #> [4,]    1   77 4091 9787 9789
                      
                      system.time(print(GetPartition(x, 23745L)))
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> 
                      #>  user  system elapsed 
                      #> 0.015   0.000   0.016
                      
                      system.time(print(GetPartition(x_big, 9568447L)))
                      #> [[1]]
                      #>        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]
                      #> [1,] 762533 773566 805253 825893 861864 881126 893583 917416 937874 953460 955879
                      #> 
                      #>  user  system elapsed 
                      #> 0.006   0.000   0.006
                      
                      library(RcppAlgos)
                      comboGeneral(x, 5,
                                   constraintFun = "sum",
                                   comparisonFun = "==",
                                   limitConstraints = 23745L,
                                   upper = 1L)
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      
                      GetPartition <- function(x, target, n_res = 1L) {
                          
                          len_x <- length(x)
                          n_soln <- 0L
                          res <- list()
                          
                          m <- 1L
                          i <- 1L
                      
                          while (n_soln < n_res || m > len_x) {
                              combs <- comboGeneral(
                                  x, m, constraintFun = "sum",
                                  comparisonFun = "==", limitConstraints = target,
                                  upper = min(choose(len_x, m), n_res - n_soln) 
                              )
                      
                              if (nrow(combs)) {
                                  n_soln <- n_soln + nrow(combs)
                                  res[[i]] <- combs
                                  i <- i + 1L
                              }
                              
                              m <- m + 1L
                          }
                          
                          return(res)
                      }
                      
                      > GetPartition(x, target = 23745L, n_res = 13L)
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> [2,]   76 5875 8810 8984
                      #> [3,]   86 4910 8984 9765
                      #> [4,]   97 5486 8810 9352
                      #> [5,]   97 5960 7901 9787
                      #> [6,]  100 4091 9765 9789
                      #> [7,]  231 4091 9634 9789
                      #> [8,]  236 4910 8810 9789
                      #> [9,]  465 5486 8810 8984
                      #> 
                      #> [[2]]
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      #> [2,]    1   63 4290 9626 9765
                      #> [3,]    1   63 4910 8984 9787
                      #> [4,]    1   77 4091 9787 9789
                      
                      system.time(print(GetPartition(x, 23745L)))
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> 
                      #>  user  system elapsed 
                      #> 0.015   0.000   0.016
                      
                      system.time(print(GetPartition(x_big, 9568447L)))
                      #> [[1]]
                      #>        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]
                      #> [1,] 762533 773566 805253 825893 861864 881126 893583 917416 937874 953460 955879
                      #> 
                      #>  user  system elapsed 
                      #> 0.006   0.000   0.006
                      
                      library(RcppAlgos)
                      comboGeneral(x, 5,
                                   constraintFun = "sum",
                                   comparisonFun = "==",
                                   limitConstraints = 23745L,
                                   upper = 1L)
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      
                      GetPartition <- function(x, target, n_res = 1L) {
                          
                          len_x <- length(x)
                          n_soln <- 0L
                          res <- list()
                          
                          m <- 1L
                          i <- 1L
                      
                          while (n_soln < n_res || m > len_x) {
                              combs <- comboGeneral(
                                  x, m, constraintFun = "sum",
                                  comparisonFun = "==", limitConstraints = target,
                                  upper = min(choose(len_x, m), n_res - n_soln) 
                              )
                      
                              if (nrow(combs)) {
                                  n_soln <- n_soln + nrow(combs)
                                  res[[i]] <- combs
                                  i <- i + 1L
                              }
                              
                              m <- m + 1L
                          }
                          
                          return(res)
                      }
                      
                      > GetPartition(x, target = 23745L, n_res = 13L)
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> [2,]   76 5875 8810 8984
                      #> [3,]   86 4910 8984 9765
                      #> [4,]   97 5486 8810 9352
                      #> [5,]   97 5960 7901 9787
                      #> [6,]  100 4091 9765 9789
                      #> [7,]  231 4091 9634 9789
                      #> [8,]  236 4910 8810 9789
                      #> [9,]  465 5486 8810 8984
                      #> 
                      #> [[2]]
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      #> [2,]    1   63 4290 9626 9765
                      #> [3,]    1   63 4910 8984 9787
                      #> [4,]    1   77 4091 9787 9789
                      
                      system.time(print(GetPartition(x, 23745L)))
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> 
                      #>  user  system elapsed 
                      #> 0.015   0.000   0.016
                      
                      system.time(print(GetPartition(x_big, 9568447L)))
                      #> [[1]]
                      #>        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]
                      #> [1,] 762533 773566 805253 825893 861864 881126 893583 917416 937874 953460 955879
                      #> 
                      #>  user  system elapsed 
                      #> 0.006   0.000   0.006
                      
                      library(RcppAlgos)
                      comboGeneral(x, 5,
                                   constraintFun = "sum",
                                   comparisonFun = "==",
                                   limitConstraints = 23745L,
                                   upper = 1L)
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      
                      GetPartition <- function(x, target, n_res = 1L) {
                          
                          len_x <- length(x)
                          n_soln <- 0L
                          res <- list()
                          
                          m <- 1L
                          i <- 1L
                      
                          while (n_soln < n_res || m > len_x) {
                              combs <- comboGeneral(
                                  x, m, constraintFun = "sum",
                                  comparisonFun = "==", limitConstraints = target,
                                  upper = min(choose(len_x, m), n_res - n_soln) 
                              )
                      
                              if (nrow(combs)) {
                                  n_soln <- n_soln + nrow(combs)
                                  res[[i]] <- combs
                                  i <- i + 1L
                              }
                              
                              m <- m + 1L
                          }
                          
                          return(res)
                      }
                      
                      > GetPartition(x, target = 23745L, n_res = 13L)
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> [2,]   76 5875 8810 8984
                      #> [3,]   86 4910 8984 9765
                      #> [4,]   97 5486 8810 9352
                      #> [5,]   97 5960 7901 9787
                      #> [6,]  100 4091 9765 9789
                      #> [7,]  231 4091 9634 9789
                      #> [8,]  236 4910 8810 9789
                      #> [9,]  465 5486 8810 8984
                      #> 
                      #> [[2]]
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      #> [2,]    1   63 4290 9626 9765
                      #> [3,]    1   63 4910 8984 9787
                      #> [4,]    1   77 4091 9787 9789
                      
                      system.time(print(GetPartition(x, 23745L)))
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> 
                      #>  user  system elapsed 
                      #> 0.015   0.000   0.016
                      
                      system.time(print(GetPartition(x_big, 9568447L)))
                      #> [[1]]
                      #>        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]
                      #> [1,] 762533 773566 805253 825893 861864 881126 893583 917416 937874 953460 955879
                      #> 
                      #>  user  system elapsed 
                      #> 0.006   0.000   0.006
                      
                      library(RcppAlgos)
                      comboGeneral(x, 5,
                                   constraintFun = "sum",
                                   comparisonFun = "==",
                                   limitConstraints = 23745L,
                                   upper = 1L)
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      
                      GetPartition <- function(x, target, n_res = 1L) {
                          
                          len_x <- length(x)
                          n_soln <- 0L
                          res <- list()
                          
                          m <- 1L
                          i <- 1L
                      
                          while (n_soln < n_res || m > len_x) {
                              combs <- comboGeneral(
                                  x, m, constraintFun = "sum",
                                  comparisonFun = "==", limitConstraints = target,
                                  upper = min(choose(len_x, m), n_res - n_soln) 
                              )
                      
                              if (nrow(combs)) {
                                  n_soln <- n_soln + nrow(combs)
                                  res[[i]] <- combs
                                  i <- i + 1L
                              }
                              
                              m <- m + 1L
                          }
                          
                          return(res)
                      }
                      
                      > GetPartition(x, target = 23745L, n_res = 13L)
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> [2,]   76 5875 8810 8984
                      #> [3,]   86 4910 8984 9765
                      #> [4,]   97 5486 8810 9352
                      #> [5,]   97 5960 7901 9787
                      #> [6,]  100 4091 9765 9789
                      #> [7,]  231 4091 9634 9789
                      #> [8,]  236 4910 8810 9789
                      #> [9,]  465 5486 8810 8984
                      #> 
                      #> [[2]]
                      #>      [,1] [,2] [,3] [,4] [,5]
                      #> [1,]    1   31 4290 9634 9789
                      #> [2,]    1   63 4290 9626 9765
                      #> [3,]    1   63 4910 8984 9787
                      #> [4,]    1   77 4091 9787 9789
                      
                      system.time(print(GetPartition(x, 23745L)))
                      #> [[1]]
                      #>      [,1] [,2] [,3] [,4]
                      #> [1,]   42 4290 9626 9787
                      #> 
                      #>  user  system elapsed 
                      #> 0.015   0.000   0.016
                      
                      system.time(print(GetPartition(x_big, 9568447L)))
                      #> [[1]]
                      #>        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]
                      #> [1,] 762533 773566 805253 825893 861864 881126 893583 917416 937874 953460 955879
                      #> 
                      #>  user  system elapsed 
                      #> 0.006   0.000   0.006
                      

                      Minimise cost of reallocating individuals

                      copy iconCopydownload iconDownload
                      ----     83 VARIABLE move.L  moves needed to meet capacity
                      
                                       zone1       zone2       zone3
                      
                      catA.zone1                       6
                      catA.zone4                      29          62
                      catC.zone4          27
                      
                      
                      ----     83 VARIABLE alloc.L  new allocation
                      
                                 zone1       zone2       zone3       zone4
                      
                      catA         132         180         196
                      catB          99          83         121         196
                      catC         187          81          33          39
                      catD          47          49          34          96
                      
                      
                      ----     83 VARIABLE totcost.L             =       12.400  total cost
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      
                      library(tidyverse)
                      library(lpSolve)
                      
                      move_new_only  <- TRUE # means population in place can't be reallocated
                      categories     <- unique(population_and_demand_by_category_and_zone$category)
                      zones          <- unique(population_and_demand_by_category_and_zone$zone)
                      n_cat          <- length(categories)
                      n_zones        <- length(zones)
                      
                      # empty coefficient arrays
                      move_coefs_template  <- array(0, c(n_zones, n_zones, n_cat), 
                                                    dimnames = list(zones, zones, categories))
                      alloc_coefs_template <- matrix(0, n_zones, n_cat, 
                                                     dimnames = list(zones, categories))
                      
                      build_zone_by_category_matrix <- function(data, col) {
                        data %>% 
                          pivot_wider(
                            id_cols = zone, names_from = category, values_from = {{col}}) %>% 
                          as.data.frame() %>% 
                          `row.names<-`(.$zone) %>% 
                          select(-zone) %>% 
                          as.matrix()
                      }
                      
                      demand_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, demand)
                      
                      cost_mat <- build_zone_by_category_matrix(costs, cost)
                      
                      population_mat <- build_zone_by_category_matrix(
                        population_and_demand_by_category_and_zone, population)
                      
                      # stack the cost matrix vertically to build an array of all move coefficients
                      coefs_obj <- move_coefs_template
                      for(i in 1:n_zones) {
                        coefs_obj[i,,] <- cost_mat
                      }
                      # flatten it for `lp`s `objective.in` argument, adding alloc coefs
                      f.obj <- c(coefs_obj, alloc_coefs_template)
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_arrivals <- move_coefs_template
                          coefs_arrivals[,z, categ] <- 1
                          coefs_departures <- move_coefs_template
                          coefs_departures[z,, categ] <- 1
                          coefs_moves <- coefs_arrivals - coefs_departures
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- -1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(coefs_moves, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con1 <- do.call(rbind, coefs_con)
                      f.dir1 <- rep("==", n_zones * n_cat)
                      f.rhs1 <- -c(t(demand_mat)) # transposing so we start with all zone 1 and so on
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_alloc <- alloc_coefs_template
                        coefs_alloc[z, ] <- 1
                        coefs_con[[z]] <- c(move_coefs_template, coefs_alloc)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con2 <- do.call(rbind, coefs_con)
                      f.dir2 <- rep("<=", n_zones)
                      
                      f.rhs2 <- demand_and_capacity_by_zone$capacity
                      
                      coefs_con <- list() 
                      for (z in zones) {
                        coefs_con_zone <- list() 
                        for(categ in categories) {
                          coefs_alloc <- alloc_coefs_template
                          coefs_alloc[z, categ] <- 1
                          # flatten the array
                          coefs_con_zone[[categ]] <- c(move_coefs_template, coefs_alloc)
                        }
                        coefs_con[[z]] <- do.call(rbind, coefs_con_zone)
                      }
                      
                      # stack the flattened arrays to build a matrix
                      f.con3 <- do.call(rbind, coefs_con)
                      f.dir3 <- rep(">=", n_zones * n_cat)
                      
                      if (move_new_only) {
                        f.rhs3 <- c(t(population_mat))
                      } else {
                        f.rhs3 <- rep(0, n_zones * n_cat)
                      }
                      
                      f.con <- rbind(f.con1, f.con2, f.con3)
                      f.dir <- c(f.dir1, f.dir2, f.dir3)
                      f.rhs <- c(f.rhs1, f.rhs2, f.rhs3)
                      
                      # compute the solution and visualize it in the array
                      results_raw <- lp("min", f.obj, f.con, f.dir, f.rhs)$solution
                      results_moves <- move_coefs_template
                      results_moves[] <- 
                        results_raw[1:length(results_moves)]
                      results_allocs <- alloc_coefs_template
                      results_allocs[] <- 
                        results_raw[length(results_moves)+(1:length(results_allocs))]
                      results_moves
                      #> , , A
                      #> 
                      #>    1 2 3 4
                      #> 1  0 0 0 0
                      #> 2  0 0 3 0
                      #> 3  0 0 0 0
                      #> 4 13 0 2 0
                      #> 
                      #> , , B
                      #> 
                      #>   1 2  3 4
                      #> 1 0 0  0 0
                      #> 2 0 0  0 0
                      #> 3 0 0  0 0
                      #> 4 0 0 57 0
                      #> 
                      #> , , C
                      #> 
                      #>   1 2 3 4
                      #> 1 0 0 0 0
                      #> 2 0 0 0 0
                      #> 3 0 0 0 0
                      #> 4 8 0 0 0
                      #> 
                      #> , , D
                      #> 
                      #>   1  2 3 4
                      #> 1 0  0 0 0
                      #> 2 0  0 0 0
                      #> 3 0  0 0 0
                      #> 4 0 38 0 0
                      
                      results_allocs
                      #>     A   B   C  D
                      #> 1 151  99 168 47
                      #> 2 142  83  81 87
                      #> 3 139 178  33 34
                      #> 4  76 139  58 58
                      
                      # format as tidy data frame
                      results_df <-
                        as.data.frame.table(results_moves) %>% 
                        setNames(c("from", "to", "category", "n")) %>% 
                        filter(n != 0) %>% 
                        mutate_at(c("from", "to"), as.numeric) %>% 
                        mutate_if(is.factor, as.character)
                      
                      results_df
                      #>   from to category  n
                      #> 1    4  1        A 13
                      #> 2    2  3        A  3
                      #> 3    4  3        A  2
                      #> 4    4  3        B 57
                      #> 5    4  1        C  8
                      #> 6    4  2        D 38
                      
                      population_and_demand_by_category_and_zone <-
                        bind_rows(
                        results_df %>% 
                        group_by(category, zone = to) %>% 
                        summarize(correction = sum(n), .groups = "drop"),
                        results_df %>% 
                          group_by(category, zone = from) %>% 
                          summarize(correction = -sum(n), .groups = "drop"),
                        ) %>% 
                        left_join(population_and_demand_by_category_and_zone, ., by = c("category", "zone")) %>% 
                        replace_na(list(correction =0)) %>% 
                        mutate(new_population = demand + correction)
                      
                      population_and_demand_by_category_and_zone
                      #> # A tibble: 16 × 6
                      #>    category  zone population demand correction new_population
                      #>    <chr>    <dbl>      <dbl>  <dbl>      <dbl>          <dbl>
                      #>  1 A            1        115    138      13               151
                      #>  2 A            2        121    145      -3.00            142
                      #>  3 A            3        112    134       5.00            139
                      #>  4 A            4         76     91     -15.0              76
                      #>  5 B            1         70     99       0                99
                      #>  6 B            2         59     83       0                83
                      #>  7 B            3         86    121      57               178
                      #>  8 B            4        139    196     -57               139
                      #>  9 C            1        142    160       8               168
                      #> 10 C            2         72     81       0                81
                      #> 11 C            3         29     33       0                33
                      #> 12 C            4         58     66      -8                58
                      #> 13 D            1         22     47       0                47
                      #> 14 D            2         23     49      38                87
                      #> 15 D            3         16     34       0                34
                      #> 16 D            4         45     96     -38                58
                      
                      
                      demand_and_capacity_by_zone <-
                        population_and_demand_by_category_and_zone %>% 
                        group_by(zone) %>% 
                        summarise(population = sum(population), correction = sum(correction), new_population = sum(new_population)) %>% 
                        left_join(demand_and_capacity_by_zone, ., by = "zone")
                      #> `summarise()` ungrouping output (override with `.groups` argument)
                        
                      demand_and_capacity_by_zone
                      #> # A tibble: 4 × 7
                      #>    zone demand capacity capacity_exceeded population correction new_population
                      #>   <dbl>  <dbl>    <dbl> <lgl>                  <dbl>      <dbl>          <dbl>
                      #> 1     1    444      465 FALSE                    349         21            465
                      #> 2     2    358      393 FALSE                    275         35            393
                      #> 3     3    322      500 FALSE                    243         62            384
                      #> 4     4    449      331 TRUE                     318       -118            331
                      

                      Android Studio error &quot;Installed Build Tools revision 31.0.0 is corrupted&quot;

                      copy iconCopydownload iconDownload
                      ln %ANDROID_HOME%/sdk/build-tools/31.0.0/d8 %ANDROID_HOME%/sdk/build-tools/31.0.0/dx && ln %ANDROID_HOME%/sdk/build-tools/31.0.0/lib/d8.jar %ANDROID_HOME%/sdk/build-tools/31.0.0/lib/dx.jar
                      

                      Community Discussions

                      Trending Discussions on solutions
                      • Python/Docker ImportError: cannot import name 'json' from itsdangerous
                      • Uncaught ReferenceError: Buffer is not defined
                      • Emulator is not working in android studio bumblebee
                      • Flutter iOS Error Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x203913130)
                      • ESLint Definition for rule 'import/extensions' was not found
                      • Java 17: Maven doesn't give much information about the error that happened, why?
                      • Xcode 13 instruments is not a developer tool or in PATH (React native)
                      • Which rows/columns are duplicates of which others in R matrices?
                      • Cypress Test Runner unexpectedly exited via a exit event with signal SIGSEGV in circleCI
                      • Print first few and last few lines of file through a pipe with &quot;...&quot; in the middle
                      Trending Discussions on solutions

                      QUESTION

                      Python/Docker ImportError: cannot import name 'json' from itsdangerous

                      Asked 2022-Mar-31 at 12:49

                      I am trying to get a Flask and Docker application to work but when I try and run it using my docker-compose up command in my Visual Studio terminal, it gives me an ImportError called ImportError: cannot import name 'json' from itsdangerous. I have tried to look for possible solutions to this problem but as of right now there are not many on here or anywhere else. The only two solutions I could find are to change the current installation of MarkupSafe and itsdangerous to a higher version: https://serverfault.com/questions/1094062/from-itsdangerous-import-json-as-json-importerror-cannot-import-name-json-fr and another one on GitHub that tells me to essentially change the MarkUpSafe and itsdangerous installation again https://github.com/aws/aws-sam-cli/issues/3661, I have also tried to make a virtual environment named veganetworkscriptenv to install the packages but that has also failed as well. I am currently using Flask 2.0.0 and Docker 5.0.0 and the error occurs on line eight in vegamain.py.

                      Here is the full ImportError that I get when I try and run the program:

                      veganetworkscript-backend-1  | Traceback (most recent call last):
                      veganetworkscript-backend-1  |   File "/app/vegamain.py", line 8, in <module>
                      veganetworkscript-backend-1  |     from flask import Flask
                      veganetworkscript-backend-1  |   File "/usr/local/lib/python3.9/site-packages/flask/__init__.py", line 19, in <module>
                      veganetworkscript-backend-1  |     from . import json
                      veganetworkscript-backend-1  |   File "/usr/local/lib/python3.9/site-packages/flask/json/__init__.py", line 15, in <module>
                      veganetworkscript-backend-1  |     from itsdangerous import json as _json
                      veganetworkscript-backend-1  | ImportError: cannot import name 'json' from 'itsdangerous' (/usr/local/lib/python3.9/site-packages/itsdangerous/__init__.py)
                      veganetworkscript-backend-1 exited with code 1
                      

                      Here are my requirements.txt, vegamain.py, Dockerfile, and docker-compose.yml files:

                      requirements.txt:

                      Flask==2.0.0
                      Flask-SQLAlchemy==2.4.4
                      SQLAlchemy==1.3.20
                      Flask-Migrate==2.5.3
                      Flask-Script==2.0.6
                      Flask-Cors==3.0.9
                      requests==2.25.0
                      mysqlclient==2.0.1
                      pika==1.1.0
                      wolframalpha==4.3.0
                      

                      vegamain.py:

                      # Veganetwork (C) TetraSystemSolutions 2022
                      # all rights are reserved.  
                      # 
                      # Author: Trevor R. Blanchard Feb-19-2022-Jul-30-2022
                      #
                      
                      # get our imports in order first
                      from flask import Flask # <-- error occurs here!!!
                      
                      # start the application through flask.
                      app = Flask(__name__)
                      
                      # if set to true will return only a "Hello World" string.
                      Debug = True
                      
                      # start a route to the index part of the app in flask.
                      @app.route('/')
                      def index():
                          if (Debug == True):
                              return 'Hello World!'
                          else:
                              pass
                      
                      # start the flask app here --->
                      if __name__ == '__main__':
                          app.run(debug=True, host='0.0.0.0') 
                      

                      Dockerfile:

                      FROM python:3.9
                      ENV PYTHONUNBUFFERED 1
                      WORKDIR /app
                      COPY requirements.txt /app/requirements.txt
                      RUN pip install -r requirements.txt
                      COPY . /app
                      

                      docker-compose.yml:

                      version: '3.8'
                      services:
                        backend:
                          build:
                            context: .
                            dockerfile: Dockerfile
                          command: 'python vegamain.py'
                          ports:
                            - 8004:5000
                          volumes:
                            - .:/app
                          depends_on:
                            - db
                      
                      #  queue:
                      #    build:
                      #      context: .
                      #      dockerfile: Dockerfile
                      #    command: 'python -u consumer.py'
                      #    depends_on:
                      #      - db
                      
                        db:
                          image: mysql:5.7.22
                          restart: always
                          environment:
                            MYSQL_DATABASE: admin
                            MYSQL_USER: root
                            MYSQL_PASSWORD: root
                            MYSQL_ROOT_PASSWORD: root
                          volumes:
                            - .dbdata:/var/lib/mysql
                          ports:
                            - 33069:3306
                      

                      How exactly can I fix this code? thank you!

                      ANSWER

                      Answered 2022-Feb-20 at 12:31

                      I was facing the same issue while running docker containers with flask.

                      I downgraded Flask to 1.1.4 and markupsafe to 2.0.1 which solved my issue.

                      Check this for reference.

                      Source https://stackoverflow.com/questions/71189819

                      Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                      Vulnerabilities

                      No vulnerabilities reported

                      Install solutions

                      You can download it from GitHub.
                      You can use solutions like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the solutions component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

                      Support

                      For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

                      DOWNLOAD this Library from

                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 430 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit

                      Share this Page

                      share link
                      Consider Popular Machine Learning Libraries
                      Try Top Libraries by tuzzeg
                      Compare Machine Learning Libraries with Highest Support
                      Compare Machine Learning Libraries with Highest Quality
                      Compare Machine Learning Libraries with Highest Security
                      Compare Machine Learning Libraries with Permissive License
                      Compare Machine Learning Libraries with Highest Reuse
                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 430 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit

                      • © 2022 Open Weaver Inc.