Can't get my for loops to work - jython

Okay, so I'm trying to have the program do three steps...
1: choose a number between (m,n) which are parameters being passed in (Set to variable "repeat")
2: choose a random number between 0 and the variable "repeat" from step one. (Set to variable "o")
3: subract "o" from "repeat" and set that result to variable "p"
thus I get a number (lets say 100)
then a random number from 0-100 (lets say 40)
and then I get the difference of 100-40 (60...)
I then want the program to run a for loop "o" (40) times and another for loop "p" (60) times...
the code for the for loops section looks like this (keep in mind there is more code before this... It just doesn't really pertain to this question:
def randomStars(pic,m,n):
repeat=random.randint(200,300)
o=random.randint(0,repeat)
p=repeat-o
for i in o:
star(pic,x,y)
for j in p:
largeStar(pic,x,y)
show(pic)
What's happening is I'm getting an error message on the line:
for i in o:
that says "iteration over non-sequence
inappropriate argument type
I've also added print statements after the 3 variables are set and they are working...
ex.1 repeat=230; o=103; p=127
ex.2 repeat=221; o=72; p=149
and then I immediately try to get the for loop to run "o"number of times and I get the above error message... I don't see how it is a non-sequence. But perhaps I'm simply not understanding the definition of a sequence

o and p are integers. For for loops you need something that is iterable. I thing you can change it to:
for i in range(o):
This is range() documentation for Python 2.x

Related

Summation iterated over a variable length

I have written an optimization problem in pyomo and need a constraint, which contains a summation that has a variable length:
u_i_t[i, t]*T_min_run - sum (tnewnew in (t-T_min_run+1)..t-1) u_i_t[i,tnewnew] <= sum (tnew in t..(t+T_min_run-1)) u_i_t[i,tnew]
T is my actual timeline and N my machines
usually I iterate over t, but I need to guarantee the machines are turned on for certain amount of time.
def HP_on_rule(model, i, t):
return model.u_i_t[i, t]*T_min_run - sum(model.u_i_t[i, tnewnew] for tnewnew in range((t-T_min_run+1), (t-1))) <= sum(model.u_i_t[i, tnew] for tnew in range(t, (t+T_min_run-1)))
model.HP_on_rule = Constraint(N, rule=HP_on_rule)
I hope you can provide me with the correct formulation in pyomo/python.
The problem is that t is a running variable and I do not know how to implement this in Python. tnew is only a help variable. E.g. t=6 (variable), T_min_run=3 (constant) and u_i_t is binary [00001111100000...] then I get:
1*3 - 1 <= 3
As I said, I do not know how to implement this in my code and the current version is not running.
TypeError: HP_on_rule() missing 1 required positional argument: 't'
It seems like you didn't provide all your arguments to the function rule.
Since t is a parameter of your function, I assume that it corresponds to an element of set T (your timeline).
Then, your last line of your code example should include not only the set N, but also the set T. Try this:
model.HP_on_rule = Constraint(N, T, rule=HP_on_rule)
Please note: Building a Constraint with a "for each" part, you must provide the Pyomo Sets that you want to iterate over at the begining of the call for Constraint construction. As a rule of thumb, your constraint rule function should have 1 more argument than the number of Pyomo Sets specified in the Constraint initilization line.

Netlogo: how to register the tick-advance counter as a variable?

I would like to register the tick-advance counter as a variable and use it for calculation in mathematical formulas, for example, (A + B) * tickadvance. However, Netlogo seems to be unable to register the tick-advance counter as a variable. Below is a sample syntax where "Expected reporter" error has been issued. This does not go well.
globals [tickadvance example-f]
;Omission;
set tickadvance (tick-advance 0.001) ;"Expected reporter" is occurring with this syntax.
set example-f ((A + B) * tickadvance)
Any advice?
You need to do it the other way around. In setup you can include set tickadvance 0.001, and then in your code, you can call tick-advance tickadvance.
Alternatively, if you really just want ticks, see the answer of #JensB.
If you are simply wanting the value of the counter, then you just need ticks (that is, tick is the command to advance the counter, and ticks reports the current value of the counter, note the s at the end)

Gnuplot summation issue

I am trying to make a plot of a simple mutation accumulation process on a binary tree...
My technical problem in gnuplot, is that is that I want to plot the possibility of getting 2 mutations on a specific lineage on the graph, here is the equation which determines it:
P_{2 mutation} = sum[k=0:n] (m/(2**(k+1)/(1-(1/2)**k)))(1-exp(-muk))
(dont bother with the formula im not sure that this is the correct one yet :))
where n is the number of levels of the binary tree, mu is the mutation rate, and m is the number of previously randomly thrown mutations onto the graphs edges...
I want to make a plot which is this possibility depending on the levels of the binary tree...
Therefore I wrote a script which is something like this:
set term pngcairo size 800,600
set title "Két mutáció megjelenésének valószínűsége, egy n szintű bináris fa egyik sejtvonalában"
set xlabel"szintek száma (n)"
set ylabel"Két mutáció megjelenésének valószínűsége (P_{2^{lin})"
set xrange[1:10]
set yrange[0:1]
set output '2mutvalsz.png'
set multiplot
do for[i=1:10]{
mu = 0.1+(i*0.1)
m = 4
f(x)=(x/((2**(x+1))*(1-(0.5)**x)))
if(m<floor(f(x)))
{
p(x)=sum [k=0:floor(x)](m*(1/((2**(x+1))*(1-(0.5)**x))))*(1-exp(-mu*k))
}
else
{
p(x)=1
}
plot p(x) lt i lw 1
}
unset multiplot
set output
So my problem is, that I dont know if it is correct to do what I do in the
if statement...
What I want is to behold the statement m< f(x) where f(x) is the number of edges in respect of n, which is an integer value therefore I use floor(f(x)), and sum through the x values (which are the number of levels what has to be an integer too... so floor(x), like a heavyside function to make the x axis discrete) in the sum...
And also I get an error message:
gnuplot> load '2mutvalsz.plt'
line 27: undefined variable: x
where line 27 is the end of the do for loop...
So my question is that is it a correct way to make a summation integer the x values and of course why I get the error message...
Thank you, and I hope everything is clear...
The error message is produced because the if statement in your script is interpreted when Gnuplot loads the script - it tries to evaluate the condition of the if statement and since the variable x is not defined, it produces the mentioned message.
You could put everything together using the ternary operator as:
p(x)=( m<floor(f(x)) )?( sum [k=0:floor(x)](m*(1/((2**(x+1))*(1-(0.5)**x))))*(1-exp(-mu*k)) ):1;
However, since the function f(x) is on the imposed x-range of [0,1] less than 1, the condition m<floor(f(x)) will be always false.

error in LDA in r: each row of the input matrix needs to contain at least one non-zero entry

I am a starter in text mining topic. When I run LDA() over a huge dataset with 996165 observations, it displays the following error:
Error in LDA(dtm, k, method = "Gibbs", control = list(nstart = nstart, :
Each row of the input matrix needs to contain at least one non-zero entry.
I am pretty sure that there is no missing values in my corpus and also. The table of "DocumentTermMatrix" and "simple_triplet_matrix" is:
table(is.na(dtm[[1]]))
#FALSE
#57100956
table(is.na(dtm[[2]]))
#FALSE
#57100956
A little confused how "57100956" comes. But as my dataset is pretty large, I don't know how to check why does this error occurs. My LDA command is:
ldaOut<-LDA(dtm,k, method="Gibbs", control=list(nstart=nstart, seed = seed, best=best, burnin = burnin, iter = iter, thin=thin))
Can anyone provide some insights? Thanks.
In my opinion the problem is not the presence of missing values, but the presence of all 0 rows.
To check it:
raw.sum=apply(table,1,FUN=sum) #sum by raw each raw of the table
Then you can delete all raws which are all 0 doing:
table=table[raw.sum!=0,]
Now table should has all "non 0" raws.
I had the same problem. The design matrix, dtm, in your case, had rows with all zeroes because dome documents did not contain certain words (i.e. their frequency was zero). I suppose this somehow causes a singular matrix problem somewhere along the line. I fixed this by adding a common word to each of the documents so that every row would have at least one non-zero entry. At the very least, the LDA ran successfully and classified each of the documents. Hope this helps!

I want my loop to run until the result of the equation equals the previous result

I am writing a code in FORTRAN to calculate the position of the planets. I need to solve Kepler's equation using newtons numerical method and I am having trouble with my loop. The code is shown below. I can compile it with no error messages, but it wont run. It just gets stuck in a forever loop. I want the loop to run until E(i) and the following result are equal. Any help would be greatly appreciated.
do while (E(i)/=E(i+1))
E(1)=M
E(i+1)=E(i)-((M-E(i)+(p*sin(E(i))))/((p*cos(E(i)))-1))
end do
Also, how do i define the variable 'E' at the beginning of my program. I currently have this but the '11' is only because I originally had my loop run 10 times. If I don't specify the number of times I want the loop to run, how do i define the variable?
double precision :: E(11)
I did not check if your convergence implementation is correct, but you definitely do not want to check for exact equality. You must allow for some inexactness, because floating point calculations are inexact
(abs(E(i)-E(i+1))<eps)
where eps is some small number.
As #agentp suggests you don't change i, so you always work with 2 values E(i) and E(i+1) (i being whatever you set it before the loop). You do not use any other elements of your array.
For the array which can have any length, depending on your need, you could theoretically use
double precision, allocatable :: E(:)
but you probably do not want to use an array at all and you just want two scalar values
double precision :: E1, E2
E1 = M
do
E2 = E1 - ((M-E1+(p*sin(E1))) / ((p*cos(E1))-1))
if (abs(E1-E2) < eps) exit
E1 = E2
end do
Note that the kind notation (real(....)) is preferable in new code instead of the old double precision.