Generate sequences while minimising overlap











up vote
0
down vote

favorite












I am trying to come up with an algorithm in R to generate n sequences from a vector X of n amount of (all positive integer) input paramters while minimising the overlaps within an interval I of all sequence elements. There is an upperlimit ub for all x ⋹ X as well as the sequences generated.



Example:



ub = 100
n = 2
# initialise X with random values
X = sample(1:ub, n, replace=F)
# [1] 20 30
# generate sequences
S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
# [[1]]
# [1] 20 40 60 80 100

# [[2]]
# [1] 30 60 90


I currently implemented a function eval.f, which generates S from X and then iterates through all sequences s in S to check how many elements of s are within an interval I of all other elements of the other sequences in S:



ub = 100
n = 2
X = sample(1:ub, n, replace=F)
I=10
eval.f<-function(X,
I,
ub){
S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
return(sum(unlist(sapply(1:length(S), function(y){
sapply(1:y, function(z){
if(y!=z){
sapply(S[[y]],function(w) abs(w-S[[z]]))
}
})
}))<I))
}


For I = 10, the total number of overlaps would for above example be 5. However, I want to scale this to bigger values of n. Currently I implemented a simple loop with 10.000 iterations and randomly sample new values for X, every iteration counting the number of overlaps of the resulting sequences, retaining X with the lowest amount of overlaps:



# initialize
iterations<-10000
solutions<-X
overlaps<-eval.f(X,I,ub)
i<-1

while(i<iterations){
new_X<-sample(1:ub, n, replace=F)
new_overlaps<-eval.f(new_X,
I,
ub)
if (new_overlaps<overlaps){
overlaps<-new_overlaps
solutions<-new_X
}
if(overlaps==0) break
i<-i+1
}


Now my question: since I want to minimise the total amount of overlaps of all sequences s in S within I, my guess is that this could be achieved using non-linear programming, however I am unsure of the formulation and implementation in R. Would greatly appreciate any input!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to come up with an algorithm in R to generate n sequences from a vector X of n amount of (all positive integer) input paramters while minimising the overlaps within an interval I of all sequence elements. There is an upperlimit ub for all x ⋹ X as well as the sequences generated.



    Example:



    ub = 100
    n = 2
    # initialise X with random values
    X = sample(1:ub, n, replace=F)
    # [1] 20 30
    # generate sequences
    S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
    # [[1]]
    # [1] 20 40 60 80 100

    # [[2]]
    # [1] 30 60 90


    I currently implemented a function eval.f, which generates S from X and then iterates through all sequences s in S to check how many elements of s are within an interval I of all other elements of the other sequences in S:



    ub = 100
    n = 2
    X = sample(1:ub, n, replace=F)
    I=10
    eval.f<-function(X,
    I,
    ub){
    S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
    return(sum(unlist(sapply(1:length(S), function(y){
    sapply(1:y, function(z){
    if(y!=z){
    sapply(S[[y]],function(w) abs(w-S[[z]]))
    }
    })
    }))<I))
    }


    For I = 10, the total number of overlaps would for above example be 5. However, I want to scale this to bigger values of n. Currently I implemented a simple loop with 10.000 iterations and randomly sample new values for X, every iteration counting the number of overlaps of the resulting sequences, retaining X with the lowest amount of overlaps:



    # initialize
    iterations<-10000
    solutions<-X
    overlaps<-eval.f(X,I,ub)
    i<-1

    while(i<iterations){
    new_X<-sample(1:ub, n, replace=F)
    new_overlaps<-eval.f(new_X,
    I,
    ub)
    if (new_overlaps<overlaps){
    overlaps<-new_overlaps
    solutions<-new_X
    }
    if(overlaps==0) break
    i<-i+1
    }


    Now my question: since I want to minimise the total amount of overlaps of all sequences s in S within I, my guess is that this could be achieved using non-linear programming, however I am unsure of the formulation and implementation in R. Would greatly appreciate any input!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to come up with an algorithm in R to generate n sequences from a vector X of n amount of (all positive integer) input paramters while minimising the overlaps within an interval I of all sequence elements. There is an upperlimit ub for all x ⋹ X as well as the sequences generated.



      Example:



      ub = 100
      n = 2
      # initialise X with random values
      X = sample(1:ub, n, replace=F)
      # [1] 20 30
      # generate sequences
      S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
      # [[1]]
      # [1] 20 40 60 80 100

      # [[2]]
      # [1] 30 60 90


      I currently implemented a function eval.f, which generates S from X and then iterates through all sequences s in S to check how many elements of s are within an interval I of all other elements of the other sequences in S:



      ub = 100
      n = 2
      X = sample(1:ub, n, replace=F)
      I=10
      eval.f<-function(X,
      I,
      ub){
      S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
      return(sum(unlist(sapply(1:length(S), function(y){
      sapply(1:y, function(z){
      if(y!=z){
      sapply(S[[y]],function(w) abs(w-S[[z]]))
      }
      })
      }))<I))
      }


      For I = 10, the total number of overlaps would for above example be 5. However, I want to scale this to bigger values of n. Currently I implemented a simple loop with 10.000 iterations and randomly sample new values for X, every iteration counting the number of overlaps of the resulting sequences, retaining X with the lowest amount of overlaps:



      # initialize
      iterations<-10000
      solutions<-X
      overlaps<-eval.f(X,I,ub)
      i<-1

      while(i<iterations){
      new_X<-sample(1:ub, n, replace=F)
      new_overlaps<-eval.f(new_X,
      I,
      ub)
      if (new_overlaps<overlaps){
      overlaps<-new_overlaps
      solutions<-new_X
      }
      if(overlaps==0) break
      i<-i+1
      }


      Now my question: since I want to minimise the total amount of overlaps of all sequences s in S within I, my guess is that this could be achieved using non-linear programming, however I am unsure of the formulation and implementation in R. Would greatly appreciate any input!










      share|improve this question













      I am trying to come up with an algorithm in R to generate n sequences from a vector X of n amount of (all positive integer) input paramters while minimising the overlaps within an interval I of all sequence elements. There is an upperlimit ub for all x ⋹ X as well as the sequences generated.



      Example:



      ub = 100
      n = 2
      # initialise X with random values
      X = sample(1:ub, n, replace=F)
      # [1] 20 30
      # generate sequences
      S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
      # [[1]]
      # [1] 20 40 60 80 100

      # [[2]]
      # [1] 30 60 90


      I currently implemented a function eval.f, which generates S from X and then iterates through all sequences s in S to check how many elements of s are within an interval I of all other elements of the other sequences in S:



      ub = 100
      n = 2
      X = sample(1:ub, n, replace=F)
      I=10
      eval.f<-function(X,
      I,
      ub){
      S = sapply(X, function(x) cumsum(rep(x,floor(ub/x))))
      return(sum(unlist(sapply(1:length(S), function(y){
      sapply(1:y, function(z){
      if(y!=z){
      sapply(S[[y]],function(w) abs(w-S[[z]]))
      }
      })
      }))<I))
      }


      For I = 10, the total number of overlaps would for above example be 5. However, I want to scale this to bigger values of n. Currently I implemented a simple loop with 10.000 iterations and randomly sample new values for X, every iteration counting the number of overlaps of the resulting sequences, retaining X with the lowest amount of overlaps:



      # initialize
      iterations<-10000
      solutions<-X
      overlaps<-eval.f(X,I,ub)
      i<-1

      while(i<iterations){
      new_X<-sample(1:ub, n, replace=F)
      new_overlaps<-eval.f(new_X,
      I,
      ub)
      if (new_overlaps<overlaps){
      overlaps<-new_overlaps
      solutions<-new_X
      }
      if(overlaps==0) break
      i<-i+1
      }


      Now my question: since I want to minimise the total amount of overlaps of all sequences s in S within I, my guess is that this could be achieved using non-linear programming, however I am unsure of the formulation and implementation in R. Would greatly appreciate any input!







      r optimization






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:39









      victor_v

      607




      607





























          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',
          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%2f53205993%2fgenerate-sequences-while-minimising-overlap%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205993%2fgenerate-sequences-while-minimising-overlap%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check