Genetic algorithm optimization in R does not consider sparse solutions Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What are good examples of genetic algorithms/genetic programming solutions?Genetic Algorithm optimization in C++Genetic algorithm: Request optimizationGenetic Algorithm OptimizationGenetic Algorithms (or optimization) in ROptimizing a genetic algorithm?Optimal parameters for genetic algorithmHow to optimize parameters using genetic algorithmsprovide an initial solution to the genetic ordering algorithm

What initially awakened the Balrog?

Time to Settle Down!

Using audio cues to encourage good posture

Is it fair for a professor to grade us on the possession of past papers?

Drawing without replacement: why is the order of draw irrelevant?

Chinese Seal on silk painting - what does it mean?

Why wasn't DOSKEY integrated with COMMAND.COM?

Why is my ESD wriststrap failing with nitrile gloves on?

Putting class ranking in CV, but against dept guidelines

Morning, Afternoon, Night Kanji

Is grep documentation about ignoring case wrong, since it doesn't ignore case in filenames?

How would a mousetrap for use in space work?

Should I follow up with an employee I believe overracted to a mistake I made?

Is there a kind of relay only consumes power when switching?

Does the Weapon Master feat grant you a fighting style?

SF book about people trapped in a series of worlds they imagine

Project Euler #1 in C++

What would you call this weird metallic apparatus that allows you to lift people?

Performance gap between vector<bool> and array

AppleTVs create a chatty alternate WiFi network

As a beginner, should I get a Squier Strat with a SSS config or a HSS?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Can a new player join a group only when a new campaign starts?

Sum letters are not two different



Genetic algorithm optimization in R does not consider sparse solutions



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What are good examples of genetic algorithms/genetic programming solutions?Genetic Algorithm optimization in C++Genetic algorithm: Request optimizationGenetic Algorithm OptimizationGenetic Algorithms (or optimization) in ROptimizing a genetic algorithm?Optimal parameters for genetic algorithmHow to optimize parameters using genetic algorithmsprovide an initial solution to the genetic ordering algorithm



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Simple knapsack type, binary optimization with 1 constraint using the GA package in R.



With a known optimal solution of say 5% of total population size, a 0 solution set is always returned. For some reason the search space never seems to include sparse solutions (e.g. binary 10000000000000000000, 01000000000000000000).



Here the optimal solution is 1 item selected when the max cost is set to 5, however a 0 set is always returned unless we up the max cost constraint to allow ~30% of the total population to be optimal.



library(GA)
options(scipen = 999)

set.seed(348821)
n <- 20
optim_n <- 1
a <- data.frame(item = c(1:n),
cost = c(rep(5, optim_n), sample(1000:5000, n - optim_n)),
value = sample(1:500, n))

a <- a[order(a$cost), ]
a$cum.cost <- cumsum(a$cost)

head(a)
item cost value cum.cost
1 1 5 208 5
19 19 1087 48 1092
20 20 1472 179 2564
5 5 1521 449 4085
15 15 1801 102 5886
13 13 2192 41 8078

# RHS Constraint
max_cost <- 5

# Fitness Function
fit_func <- function(x)
iter_cost <- x %*% a$cost
iter_value <- x %*% a$value

if(iter_cost > max_cost)
return(0)
else
return(iter_value)



# Run
select <- ga(type = "binary",
nBits = nrow(a),
maxiter = 1000,
run = 250,
fitness = fit_func,
popSize = 1000)

# Print Results
cat("n","Fitness Value: ", select@fitnessValue, "n",
"Items Selected: ", if(select@fitnessValue == 0) 0 else sum(select@solution) , "n",
"Min Optimal Selection:", nrow(a[a$cum.cost <= max_cost, ]))


And Result:



Fitness Value: 0 
Items Selected: 0
Min Optimal Selection: 1


I've tried ample combinations of parameters, including the pmutation, pcrossover, popSize, and run iterations, with no luck. I've also tried the genalg::rbga.bin() function with the same results.



No matter, when the optimal solution is sparse, I'm unable to return any solution.



I'm assuming it's either my design, fitness function, or possibly something to do with the stochastic GA approach. Any help is greatly appreciated.










share|improve this question






























    0















    Simple knapsack type, binary optimization with 1 constraint using the GA package in R.



    With a known optimal solution of say 5% of total population size, a 0 solution set is always returned. For some reason the search space never seems to include sparse solutions (e.g. binary 10000000000000000000, 01000000000000000000).



    Here the optimal solution is 1 item selected when the max cost is set to 5, however a 0 set is always returned unless we up the max cost constraint to allow ~30% of the total population to be optimal.



    library(GA)
    options(scipen = 999)

    set.seed(348821)
    n <- 20
    optim_n <- 1
    a <- data.frame(item = c(1:n),
    cost = c(rep(5, optim_n), sample(1000:5000, n - optim_n)),
    value = sample(1:500, n))

    a <- a[order(a$cost), ]
    a$cum.cost <- cumsum(a$cost)

    head(a)
    item cost value cum.cost
    1 1 5 208 5
    19 19 1087 48 1092
    20 20 1472 179 2564
    5 5 1521 449 4085
    15 15 1801 102 5886
    13 13 2192 41 8078

    # RHS Constraint
    max_cost <- 5

    # Fitness Function
    fit_func <- function(x)
    iter_cost <- x %*% a$cost
    iter_value <- x %*% a$value

    if(iter_cost > max_cost)
    return(0)
    else
    return(iter_value)



    # Run
    select <- ga(type = "binary",
    nBits = nrow(a),
    maxiter = 1000,
    run = 250,
    fitness = fit_func,
    popSize = 1000)

    # Print Results
    cat("n","Fitness Value: ", select@fitnessValue, "n",
    "Items Selected: ", if(select@fitnessValue == 0) 0 else sum(select@solution) , "n",
    "Min Optimal Selection:", nrow(a[a$cum.cost <= max_cost, ]))


    And Result:



    Fitness Value: 0 
    Items Selected: 0
    Min Optimal Selection: 1


    I've tried ample combinations of parameters, including the pmutation, pcrossover, popSize, and run iterations, with no luck. I've also tried the genalg::rbga.bin() function with the same results.



    No matter, when the optimal solution is sparse, I'm unable to return any solution.



    I'm assuming it's either my design, fitness function, or possibly something to do with the stochastic GA approach. Any help is greatly appreciated.










    share|improve this question


























      0












      0








      0








      Simple knapsack type, binary optimization with 1 constraint using the GA package in R.



      With a known optimal solution of say 5% of total population size, a 0 solution set is always returned. For some reason the search space never seems to include sparse solutions (e.g. binary 10000000000000000000, 01000000000000000000).



      Here the optimal solution is 1 item selected when the max cost is set to 5, however a 0 set is always returned unless we up the max cost constraint to allow ~30% of the total population to be optimal.



      library(GA)
      options(scipen = 999)

      set.seed(348821)
      n <- 20
      optim_n <- 1
      a <- data.frame(item = c(1:n),
      cost = c(rep(5, optim_n), sample(1000:5000, n - optim_n)),
      value = sample(1:500, n))

      a <- a[order(a$cost), ]
      a$cum.cost <- cumsum(a$cost)

      head(a)
      item cost value cum.cost
      1 1 5 208 5
      19 19 1087 48 1092
      20 20 1472 179 2564
      5 5 1521 449 4085
      15 15 1801 102 5886
      13 13 2192 41 8078

      # RHS Constraint
      max_cost <- 5

      # Fitness Function
      fit_func <- function(x)
      iter_cost <- x %*% a$cost
      iter_value <- x %*% a$value

      if(iter_cost > max_cost)
      return(0)
      else
      return(iter_value)



      # Run
      select <- ga(type = "binary",
      nBits = nrow(a),
      maxiter = 1000,
      run = 250,
      fitness = fit_func,
      popSize = 1000)

      # Print Results
      cat("n","Fitness Value: ", select@fitnessValue, "n",
      "Items Selected: ", if(select@fitnessValue == 0) 0 else sum(select@solution) , "n",
      "Min Optimal Selection:", nrow(a[a$cum.cost <= max_cost, ]))


      And Result:



      Fitness Value: 0 
      Items Selected: 0
      Min Optimal Selection: 1


      I've tried ample combinations of parameters, including the pmutation, pcrossover, popSize, and run iterations, with no luck. I've also tried the genalg::rbga.bin() function with the same results.



      No matter, when the optimal solution is sparse, I'm unable to return any solution.



      I'm assuming it's either my design, fitness function, or possibly something to do with the stochastic GA approach. Any help is greatly appreciated.










      share|improve this question
















      Simple knapsack type, binary optimization with 1 constraint using the GA package in R.



      With a known optimal solution of say 5% of total population size, a 0 solution set is always returned. For some reason the search space never seems to include sparse solutions (e.g. binary 10000000000000000000, 01000000000000000000).



      Here the optimal solution is 1 item selected when the max cost is set to 5, however a 0 set is always returned unless we up the max cost constraint to allow ~30% of the total population to be optimal.



      library(GA)
      options(scipen = 999)

      set.seed(348821)
      n <- 20
      optim_n <- 1
      a <- data.frame(item = c(1:n),
      cost = c(rep(5, optim_n), sample(1000:5000, n - optim_n)),
      value = sample(1:500, n))

      a <- a[order(a$cost), ]
      a$cum.cost <- cumsum(a$cost)

      head(a)
      item cost value cum.cost
      1 1 5 208 5
      19 19 1087 48 1092
      20 20 1472 179 2564
      5 5 1521 449 4085
      15 15 1801 102 5886
      13 13 2192 41 8078

      # RHS Constraint
      max_cost <- 5

      # Fitness Function
      fit_func <- function(x)
      iter_cost <- x %*% a$cost
      iter_value <- x %*% a$value

      if(iter_cost > max_cost)
      return(0)
      else
      return(iter_value)



      # Run
      select <- ga(type = "binary",
      nBits = nrow(a),
      maxiter = 1000,
      run = 250,
      fitness = fit_func,
      popSize = 1000)

      # Print Results
      cat("n","Fitness Value: ", select@fitnessValue, "n",
      "Items Selected: ", if(select@fitnessValue == 0) 0 else sum(select@solution) , "n",
      "Min Optimal Selection:", nrow(a[a$cum.cost <= max_cost, ]))


      And Result:



      Fitness Value: 0 
      Items Selected: 0
      Min Optimal Selection: 1


      I've tried ample combinations of parameters, including the pmutation, pcrossover, popSize, and run iterations, with no luck. I've also tried the genalg::rbga.bin() function with the same results.



      No matter, when the optimal solution is sparse, I'm unable to return any solution.



      I'm assuming it's either my design, fitness function, or possibly something to do with the stochastic GA approach. Any help is greatly appreciated.







      r optimization sparse-matrix genetic-algorithm knapsack-problem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 11 at 13:48







      Brian

















      asked Mar 8 at 20:19









      BrianBrian

      807




      807






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070448%2fgenetic-algorithm-optimization-in-r-does-not-consider-sparse-solutions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070448%2fgenetic-algorithm-optimization-in-r-does-not-consider-sparse-solutions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved