Re: [R] Infinite loop

2011-03-22 Thread jim holtman
Some systems (both hardware and software) may have "watchdog" timers;
I was just using it to illustrate a point as to one way of breaking
out.  In the case of this loop, it probably run too fast to try and
put a timer on it; counting the iterations is good enough.  There are
some ways of creating timers in R, but I have not used them.

On Tue, Mar 22, 2011 at 1:32 PM, Hosack, Michael  wrote:
> Thank you Jim, I was not aware of watchdog timers. R is my first computer 
> language. I searched through the archives and found the function setTimeLimit 
> and applied it with low values for cpu and elapsed time. It provides a way 
> out of the loop which is what I needed. It's not a counter, but I could not 
> find any info. on them in the R archives.
>
> Thanks again
>
> Mike
>
>
> -Original Message-
> From: jim holtman [mailto:jholt...@gmail.com]
> Sent: Tuesday, March 22, 2011 1:00 PM
> To: Hosack, Michael
> Cc: r-help@R-project.org
> Subject: Re: [R] Infinite loop
>
> The simple thing to do is to put a sanity counter in the 'repeat'
> statement and if you have been through it a certain number of times,
> then exit.  Anytime you have a loop that might run forever, you should
> have some sanity/watchdog timer on it.
>
> On Tue, Mar 22, 2011 at 11:02 AM, Hosack, Michael  wrote:
>> R experts,
>>
>> Hello, I am trying to sample a vector 1:40 without replacement such that no 
>> element in the new vector
>> is within 7 units of either of its immediate neighbors. This is part of a 
>> larger program I am working
>> on. The following code works well about 65 % of the time (14/40). The 
>> problem I encounter happens when
>> the last element remaining to be sampled from the vector STRATA is within 7 
>> digits +- of the last element
>> in the vector s1, at which point an infinite loop occurs. At least that's 
>> what I think is happening.
>> Any help would be greatly appreciated.
>>
>> Thank you,
>>
>> Mike
>>
>> require(IRanges)
>> STRATA <- 1:40
>> s1 <- sample(STRATA, 1)
>> for (i in seq(from = 1, to = 39, by = 1)){
>>  repeat{
>>    tmp <- sample(STRATA, 1)
>>    if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7, 
>> s1[length(s1)]+7)) %in% tmp)) break
>>    }
>>  s1 <- c(s1,tmp)
>> }
>> s1
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Infinite loop

2011-03-22 Thread Hosack, Michael


Thank you so much Martyn. Now I can most likely avoid having to rerun my 
program 
multiple times.

Mike

-Original Message-
From: Martyn Byng [mailto:martyn.b...@nag.co.uk] 
Sent: Tuesday, March 22, 2011 1:21 PM
To: Hosack, Michael
Cc: r-help@R-project.org
Subject: RE: [R] Infinite loop

Hi,

This might do what you want:

iter <- 0
repeat {
  iter <- iter + 1
  ss <- numeric(40)
  ss[1] <- sample(1:40,1)
  for (i in 1:39) {
## calculate all possible step sizes that will give a new value in
the 1:40 range
pmove <- sample((1 - ss[i]):(40-ss[i]))
## drop all step sizes that puts the new value within 7 places of
the previous value
pmove <- pmove[abs(pmove)>7]
## calculate potential next values
pss <- pmove + ss[i]
## flag any values that are already in the sample
not.already.in <- !(pss%in%ss)
found <- any(not.already.in)
if (found) {
  ## use the first value that is not already in the sample
  ss[i+1] <- pss[not.already.in][1]
} else {
  ## all potential values are already in the sample, so choose
another starting point
  break
}
  }
  if (found) break
  if (iter > 100) {
cat("Giving up\n")
break
  }
}

It randomly chooses a starting value, then chooses the next value based
on a randomly selected step size rather than directly. It keeps doing
this until it either uses all possible values, or gets stuck in which
case it randomly selects another starting value. If it can't find a
solution in 100 goes it gives up.

Martyn

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Hosack, Michael
Sent: 22 March 2011 15:03
To: r-help@R-project.org
Subject: [R] Infinite loop

R experts,

Hello, I am trying to sample a vector 1:40 without replacement such that
no element in the new vector 
is within 7 units of either of its immediate neighbors. This is part of
a larger program I am working 
on. The following code works well about 65 % of the time (14/40). The
problem I encounter happens when 
the last element remaining to be sampled from the vector STRATA is
within 7 digits +- of the last element 
in the vector s1, at which point an infinite loop occurs. At least
that's what I think is happening. 
Any help would be greatly appreciated. 

Thank you,

Mike

require(IRanges)
STRATA <- 1:40
s1 <- sample(STRATA, 1)
for (i in seq(from = 1, to = 39, by = 1)){
  repeat{
tmp <- sample(STRATA, 1)
if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7,
s1[length(s1)]+7)) %in% tmp)) break
}
  s1 <- c(s1,tmp)
}
s1

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Infinite loop

2011-03-22 Thread Hosack, Michael
Thank you Jim, I was not aware of watchdog timers. R is my first computer 
language. I searched through the archives and found the function setTimeLimit 
and applied it with low values for cpu and elapsed time. It provides a way out 
of the loop which is what I needed. It's not a counter, but I could not find 
any info. on them in the R archives.

Thanks again

Mike


-Original Message-
From: jim holtman [mailto:jholt...@gmail.com] 
Sent: Tuesday, March 22, 2011 1:00 PM
To: Hosack, Michael
Cc: r-help@R-project.org
Subject: Re: [R] Infinite loop

The simple thing to do is to put a sanity counter in the 'repeat'
statement and if you have been through it a certain number of times,
then exit.  Anytime you have a loop that might run forever, you should
have some sanity/watchdog timer on it.

On Tue, Mar 22, 2011 at 11:02 AM, Hosack, Michael  wrote:
> R experts,
>
> Hello, I am trying to sample a vector 1:40 without replacement such that no 
> element in the new vector
> is within 7 units of either of its immediate neighbors. This is part of a 
> larger program I am working
> on. The following code works well about 65 % of the time (14/40). The problem 
> I encounter happens when
> the last element remaining to be sampled from the vector STRATA is within 7 
> digits +- of the last element
> in the vector s1, at which point an infinite loop occurs. At least that's 
> what I think is happening.
> Any help would be greatly appreciated.
>
> Thank you,
>
> Mike
>
> require(IRanges)
> STRATA <- 1:40
> s1 <- sample(STRATA, 1)
> for (i in seq(from = 1, to = 39, by = 1)){
>  repeat{
>    tmp <- sample(STRATA, 1)
>    if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7, 
> s1[length(s1)]+7)) %in% tmp)) break
>    }
>  s1 <- c(s1,tmp)
> }
> s1
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Infinite loop

2011-03-22 Thread Martyn Byng
Hi,

This might do what you want:

iter <- 0
repeat {
  iter <- iter + 1
  ss <- numeric(40)
  ss[1] <- sample(1:40,1)
  for (i in 1:39) {
## calculate all possible step sizes that will give a new value in
the 1:40 range
pmove <- sample((1 - ss[i]):(40-ss[i]))
## drop all step sizes that puts the new value within 7 places of
the previous value
pmove <- pmove[abs(pmove)>7]
## calculate potential next values
pss <- pmove + ss[i]
## flag any values that are already in the sample
not.already.in <- !(pss%in%ss)
found <- any(not.already.in)
if (found) {
  ## use the first value that is not already in the sample
  ss[i+1] <- pss[not.already.in][1]
} else {
  ## all potential values are already in the sample, so choose
another starting point
  break
}
  }
  if (found) break
  if (iter > 100) {
cat("Giving up\n")
break
  }
}

It randomly chooses a starting value, then chooses the next value based
on a randomly selected step size rather than directly. It keeps doing
this until it either uses all possible values, or gets stuck in which
case it randomly selects another starting value. If it can't find a
solution in 100 goes it gives up.

Martyn

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Hosack, Michael
Sent: 22 March 2011 15:03
To: r-help@R-project.org
Subject: [R] Infinite loop

R experts,

Hello, I am trying to sample a vector 1:40 without replacement such that
no element in the new vector 
is within 7 units of either of its immediate neighbors. This is part of
a larger program I am working 
on. The following code works well about 65 % of the time (14/40). The
problem I encounter happens when 
the last element remaining to be sampled from the vector STRATA is
within 7 digits +- of the last element 
in the vector s1, at which point an infinite loop occurs. At least
that's what I think is happening. 
Any help would be greatly appreciated. 

Thank you,

Mike

require(IRanges)
STRATA <- 1:40
s1 <- sample(STRATA, 1)
for (i in seq(from = 1, to = 39, by = 1)){
  repeat{
tmp <- sample(STRATA, 1)
if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7,
s1[length(s1)]+7)) %in% tmp)) break
}
  s1 <- c(s1,tmp)
}
s1

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Infinite loop

2011-03-22 Thread jim holtman
The simple thing to do is to put a sanity counter in the 'repeat'
statement and if you have been through it a certain number of times,
then exit.  Anytime you have a loop that might run forever, you should
have some sanity/watchdog timer on it.

On Tue, Mar 22, 2011 at 11:02 AM, Hosack, Michael  wrote:
> R experts,
>
> Hello, I am trying to sample a vector 1:40 without replacement such that no 
> element in the new vector
> is within 7 units of either of its immediate neighbors. This is part of a 
> larger program I am working
> on. The following code works well about 65 % of the time (14/40). The problem 
> I encounter happens when
> the last element remaining to be sampled from the vector STRATA is within 7 
> digits +- of the last element
> in the vector s1, at which point an infinite loop occurs. At least that's 
> what I think is happening.
> Any help would be greatly appreciated.
>
> Thank you,
>
> Mike
>
> require(IRanges)
> STRATA <- 1:40
> s1 <- sample(STRATA, 1)
> for (i in seq(from = 1, to = 39, by = 1)){
>  repeat{
>    tmp <- sample(STRATA, 1)
>    if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7, 
> s1[length(s1)]+7)) %in% tmp)) break
>    }
>  s1 <- c(s1,tmp)
> }
> s1
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Infinite loop

2011-03-22 Thread Hosack, Michael
R experts,

Hello, I am trying to sample a vector 1:40 without replacement such that no 
element in the new vector 
is within 7 units of either of its immediate neighbors. This is part of a 
larger program I am working 
on. The following code works well about 65 % of the time (14/40). The problem I 
encounter happens when 
the last element remaining to be sampled from the vector STRATA is within 7 
digits +- of the last element 
in the vector s1, at which point an infinite loop occurs. At least that's what 
I think is happening. 
Any help would be greatly appreciated. 

Thank you,

Mike

require(IRanges)
STRATA <- 1:40
s1 <- sample(STRATA, 1)
for (i in seq(from = 1, to = 39, by = 1)){
  repeat{
tmp <- sample(STRATA, 1)
if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7, 
s1[length(s1)]+7)) %in% tmp)) break
}
  s1 <- c(s1,tmp)
}
s1

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.