GAMS- Domain Violation for set - gams-math

I got two types of error in my GAMS code.
1- error **** 171 Domain violation for set
2- error 148, Dimension different - The symbol is referenced with more/less
**** indices as declared
I don't know where I am making a mistake.
Px1 is my equation name, representing the power of x, so represent as "px"
Px(x,t,w) is defined variable
Sets:
d2 'number of extreme points in the feasible region' /d2*d2/;
coeff 'combination coefficient of the feasible region of CHP unit' /a*e/;
x ’chp units’ /x1*x1/;
t ’time periods’ /t1*t24/;
w ’scenarios’ /w1*w20/;
Px1(x,t,w).. Px(x,t,w)=e=sum((d2),Px(x,d2)*coeff(x,t,w,d2));

Px1(x,t,w).. Px(x,t,w)=e=sum((d2),Px(x,d2)*coeff(x,t,w,d2));
You use Px here in two different ways: First, Px(x,t,w), which seems to match your declaration, but then as part of the sum as Px(x,d2), which must be wrong and seems to cause both error messages. That should probably be a different symbol?

Related

Error when using least_squares to solve constrained nonlinear system of equations

I'm trying to solve a nonlinear system of 7 equations with least_squares, as some variables are molar fractions and go between 0 and 1 (fsolve doesnt allow constraints as far as I've read). Heres the main part of the code:
def fun_t(w,x_2):
x_1=1-x_2;
z_A=w[0];
z_B=w[1];
z_AB=w[2];
gamma_A=w[3];
gamma_B=w[4];
gamma_AB=w[5];
eps=w[6]
K_gamma=gamma_AB/(gamma_A*gamma_B)
K=K_gamma*z_AB/(z_A*z_B)
phi_A=z_A*v_A/(z_A*v_A+z_B*v_B+z_AB*v_AB);
phi_B=z_B*v_B/(z_A*v_A+z_B*v_B+z_AB*v_AB);
phi_AB=z_AB*v_AB/(z_A*v_A+z_B*v_B+z_AB*v_AB);
return [z_AB-eps/(1-eps),
z_A-(x_2-eps)/(1-eps),
z_B-(x_1-eps)/(1-eps),
eps-(1-(1-4*K/(K+K_gamma)*x_1*x_2)**0.5)/2,
R*T*np.log(gamma_A)-v_A*(alphaT_AB*phi_B**2+alphaT_AAB*phi_AB**2+(alphaT_AB+alphaT_AAB-alphaT_BAB)*phi_B*phi_AB),
R*T*np.log(gamma_B)-v_B*(alphaT_AB*phi_A**2+alphaT_BAB*phi_AB**2+(alphaT_AB+alphaT_BAB+alphaT_AAB)*phi_A*phi_AB),
R*T*np.log(gamma_AB)-v_AB*(alphaT_AAB*phi_A**2+alphaT_BAB*phi_B**2+(alphaT_AAB+alphaT_BAB-alphaT_AB)*phi_A*phi_B)
]
x_2=np.linspace(0,1,11)
guess=[0.5,0.5,0.5,1,1,1,0.25];
roots_t=np.zeros(shape=(len(x_2),len(guess)))
for i in range(len(x_2)):
roots_t[i] = least_squares(fun_t, guess, bounds = ((0,0,0,-5,-5,-5,0),(1,1,1,5,5,5,0.5)), args=x_2[i])
I get the error TypeError: fun_t() argument after * must be an iterable, not numpy.float64. My goal is to give the elements of the array x_2 as the fuction's second argument in order to get the rest of the variables for a set of compositions. I dont understand why it cant iterate over x_2, as it worked when I was using fsolve.
I'm just starting using python for this kind of calculations, so any help will be much appreciated!

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.

I want to make vhdl divider

i'm now using FPGA spartan3,
i want to calculate 'result' which is represented below formula.
and the 'result' should be returned as integer type. So i set up all the variables with integer type but it doesn't work.
result <=((a*b*7894*7)/(w*temp_constant));
i've set a,b,c,w,temp_constant as variables
variable a : integer range 0 to 99;
variable b : integer range 0 to 9999;
variable w : integer range 0 to 200;
variable temp_constant : integer range 0 to 99;
but the operator '/' doesn't work at this synthesis. the error msg was
'Operator '/' must have constant operands or first operand must be power of 2"'
The error message is almost (see the note below) 100% clear: divisions are not supported by your synthesis tool, except with constant operands (the result is computed by the synthesizer in the constant propagation phase) or with divisors that are powers of 2 (the division is a simple right shift).
One possible reason for this limitation of your synthesis tool is that there are many of ways to compute integer divisions in hardware and typing just / in a VHDL code is not enough to chose among them. There may be other reasons.
In your case where operands are not constants, and the divisor is not a power of 2, you must design this divider yourself at a lower level. If you have no idea about hardware implementations of integer dividers you will have to search a bit. This is a very classical topic, it should be easy to find good resources. Just a hint: pre-computing all inverses in fixed point representation, storing them in a read-only memory and using multiplications instead of divisions is an option.
Note: I find the error message you got (first operand must be power of 2) a bit surprising. Unless the term first operand is supposed to designate the divisor, which is not that common, it is probably a bug and the correct error message should be: second operand must be power of 2. Or, even better: divisor must be power of 2.

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.

Can I run a GA to optimize wavelet transform?

I am running a wavelet transform (cmor) to estimate damping and frequencies that exists in a signal.cmor has 2 parameters that I can change them to get more accurate results. center frequency(Fc) and bandwidth frequency(Fb). If I construct a signal with few freqs and damping then I can measure the error of my estimation(fig 2). but in actual case I have a signal and I don't know its freqs and dampings so I can't measure the error.so a friend in here suggested me to reconstruct the signal and find error by measuring the difference between the original and reconstructed signal e(t)=|x(t)−x^(t)|.
so my question is:
Does anyone know a better function to find the error between reconstructed and original signal,rather than e(t)=|x(t)−x^(t)|.
can I use GA to search for Fb and Fc? or do you know a better search method?
Hope this picture shows what I mean, the actual case is last one. others are for explanations
Thanks in advance
You say you don't know the error until after running the wavelet transform, but that's fine. You just run a wavelet transform for every individual the GA produces. Those individuals with lower errors are considered fitter and survive with greater probability. This may be very slow, but conceptually at least, that's the idea.
Let's define a Chromosome datatype containing an encoded pair of values, one for the frequency and another for the damping parameter. Don't worry too much about how their encoded for now, just assume it's an array of two doubles if you like. All that's important is that you have a way to get the values out of the chromosome. For now, I'll just refer to them by name, but you could represent them in binary, as an array of doubles, etc. The other member of the Chromosome type is a double storing its fitness.
We can obviously generate random frequency and damping values, so let's create say 100 random Chromosomes. We don't know how to set their fitness yet, but that's fine. Just set it to zero at first. To set the real fitness value, we're going to have to run the wavelet transform once for each of our 100 parameter settings.
for Chromosome chr in population
chr.fitness = run_wavelet_transform(chr.frequency, chr.damping)
end
Now we have 100 possible wavelet transforms, each with a computed error, stored in our set called population. What's left is to select fitter members of the population, breed them, and allow the fitter members of the population and offspring to survive into the next generation.
while not done
offspring = new_population()
while count(offspring) < N
parent1, parent2 = select_parents(population)
child1, child2 = do_crossover(parent1, parent2)
mutate(child1)
mutate(child2)
child1.fitness = run_wavelet_transform(child1.frequency, child1.damping)
child2.fitness = run_wavelet_transform(child2.frequency, child2.damping)
offspring.add(child1)
offspring.add(child2)
end while
population = merge(population, offspring)
end while
There are a bunch of different ways to do the individual steps like select_parents, do_crossover, mutate, and merge here, but the basic structure of the GA stays pretty much the same. You just have to run a brand new wavelet decomposition for every new offspring.