I have three sets, I and J and K, I know that for defining a subset in GAMS I should write it this way,
I2(I) when set I2 is a subset of set I
The problem is that the third set, Set K, is a subset of both set I and J, and I don't know how to code that in GAMS.
Thanks in advance :)
PS
Someone with enough reputation create a GAMS tag please, cause there isn't anything related to this subject in the list.
If I and J are disjoint you can have
set I / i1*i10 /,
J / j1*j10 /,
I_U_J / set.I, set.J /,
K(I_U_J) / i1, i4, j3, j6 /;
If they are not disjoint the above code will give an error and you can, instead, do
set I_U_J / i1*i10 /,
I(I_U_J) / i1*i7 /,
J(I_U_J) / i3*i10/,
K(I_U_J) / i4*i8/;
Related
I am dealing with a discrete math optimization problem on a complete graph. My variables are the arcs but I want to delete the arcs that "cost too much". I have n nodes which means I have n² arcs.
I define the following set on AMPL
ARCS:={i in 1..n, j in 1..n : i!=j && d[i,j]<= R}
where d[i,j] is the cost on the arc (i,j) and R the limit I am putting.
My problem is that I don't know how to index the variables now. I know I can write
sum{ i in 1..n, j in 1..n : (i,j) in ARCS} blablabla[i,j]
But I think this is quit a tedious way to do. I thought I could write something like this:
sum{e in ARCS} blablabla[e[0],e[1]]
I'm not sure if AMPL supports referencing triple elements by position number the way you've written above. (I suspect not, since I don't recall AMPL ever taking a stance on zero- vs. one-indexing, which it would have to do in order to reference them this way.)
Instead, you can do this:
sum{(i,j) in ARCS} blablabla[i,j]
As well as being more succinct, this may also be more efficient for cases where the membership of ARCS is only a small percentage of possible (i,j) combinations, since it avoids the need to generate and test tuples that aren't in ARCS.
I tend to write some codes in GAMS that include a loop that exclude some indices ,how can I write some a loop like with exception?
u("p1","j1")*o1("p1","j2") - sum(t,v("p1","j1",t)*I1("p1","j2",t))=l=0;
u("p1","j1")*o1("p1","j3") - sum(t,v("p1","j1",t)*I1("p1","j3",t))=l=0;
in these equations u , o together and also v,I1 together have different indices j;
how can write this loop?
You can use a loop, but probably a better solution is to limit the equations to the right combination of sets. In any case it can be done using alias of the sets and the SameAs operator.
Alias(p,pp);
Alias(j,jj);
E_myEquation(p,j,pp,jj) $(not (SameAs(p,pp) and SameAs(j,jj)))..
u(p,j)*o1(pp,jj) - sum(t, v(p,j,t)*I1(pp,jj,t)) =l= 0;
This defines the equation for all combinations of (p,j) with itself, except for (p,j)==(p,j).
I hope I have understood the request correctly, but otherwise you can probably figure out the exact implementation you want, using the Alias and SameAs functions.
Question is from Hacker Rank, I want mathematical solution...
Find the last ten digits of the series...
1^1 + 2^2 + 3^3 + ⋯ + N^N
if N is too big number
as where 1 <= N <= 2000000
i have code that loop over N, but it takes too much time to complete for N>1000000.
any idea to reduce time ???
code:
n = int(input())
if(n>=1 and n<=2*1e6):
s=0
for i in range(1, n+1):
s+=(i**i)
print(s%10000000000)
This looks like Python, so I’ll assume that’s what you’re using (you should tag your question with the language you’re using).
The big problem is that i**i is humongous, so Python is using big integers to keep track of everything. Since 2e6^(2e6) has 12602060 digits, that’s too much to compute quickly (and way more than the 10 digits you need). This also means that my suggestion of moving the modulus into the loop probably wouldn’t have helped.
The solution is to take the modulus while you’re taking the exponentiation (for details see Modular exponentiation. Some implementations are here. Using Python makes this simpler, since you don’t need to worry about integer overflow (which, ironically, is what caused your original problem).
But Python makes this even easier, since pow allows you to specify an optional modulus. So you could rewrite your original code as:
n = int(input())
if ( 1<=n<=2e6 ):
s = 0
for i in range(1,n+1):
s += pow(i,i,10**10)
print(s%(10**10))
But we can simplify this further. Python also includes a sum function, so you could use a list comprehension and rewrite the above as
n = int(input())
if ( 1<=n<=2e6 ):
s = sum( pow(i,i,10**10) for i in range(1,n+1) )
print(s%(10**10))
But it’s silly to assign a variable for only one step. So you’d rewrite that as
n = int(input())
if ( 1<=n<=2e6 ):
print(sum( pow(i,i,10**10) for i in range(1,n+1) ) % 10**10)
But you may prefer to use the command line interface for Python, and not worry about checking the input:
sum( pow(i,i,10**10) for i in range(1,2*10**6+1) ) % 10**10
I am trying to understand recursion in Prolog, but it is certainly different than how it is in other languages like PHP, Java and C. I have been reading all the tutorials about it, but I still don't get specific, rather complicated cases.
For instance, in order to get the number of occurrences of an element in a list we have:
occurrence([], _, 0).
occurrence([H | T], H, N) :- !, occurrence(T, H, N1), N is N1 + 1.
occurrence([_ | T], H, N) :- occurrence(T, H, N).
which can be called using:
occurrence([1,4,9,1,2],1,R).
and it should result:
?- R=2
Why is line 3 even happening? What is it doing? I wrote this program without looking at the answer and I was done after the 2nd line. Of course it wasn't working though.
On the other hand, why is the "cut" occurring in there? I have been trying to print out the result after every call and I just get more and more confused.
Line 3 handles the case where the head of the list doesn't match the item you're looking for. Suppose you've passed the first element, you wind up in the recursive call to occurrence/3 which looks like:
occurrence([4,9,1,2], 1, 1).
Which rule matches? Rule 1 doesn't match because we don't have the empty list. Rule 2 doesn't match because 4 \= 1. The third rule is for this case, and it just says, you didn't find a 1 here, so keep looking with the tail of the list ([9,1,2]).
The cut is there because the third rule will match any list. The cut commits you to the choice. What choice? That you have the same value in the head of the list and as the element you're seeking, because that's the pattern that must be matched to enter the body of this rule. If you omit the cut, you're permitting a choice point after unifying the head of the list with the element being sought, which means you'll get more than one solution with unifications for R = 2, then R = 1, then R = 0. This is because each 1 could also just be ignored as in the third rule.
You can get rid of the cut by making the third rule conditional:
occurrence([H1|T], H2, N) :- H1 \= H2, occurrence(T, H2, N).
You could also use a conditional expression and have one rule combine rules 2 and 3:
occurrence([H|T], S, N) :-
(H = S
-> occurrence(T, S, N1), N is N1 + 1
; occurrence(T, S, N)
).
This is likely to be somewhat more efficient; the conditional expression doesn't create choice points the way additional rule bodies do. Prolog isn't psychic, so it can't detect that rules are mutually exclusive. I would prefer the logical formulation with multiple rules for readability though.
Hope this helps.
how to write a formula like
v_r (t)=∑_(n=0)^(N-1)▒〖A_r (L_2-L_1 ) e^j(ω_c t-4π/λ (R+υt+L_(1+L_2 )/2 cos〖(θ)sin(ω_r t+2πn/N)))〗 ┤) sinc(4π/λ-L_(2-L_1 )/2 cos(θ) sin(ω_r t+2πn/N))〗
in c#?
You have to convert the formula to something the compiler recognizes.
To it's equivalent using the a combination of basic algebra and the Math class like so:
p = rho*R*T + (B_0*R*T-A_0-((C_0) / (T*T))+((E_0) / (Math.Pow(T, 4))))*rho*rho +
(b*R*T-a-((d) / (T)))*Math.Pow(rho, 3) +
alpha*(a+((d) / (t)))*Math.Pow(rho, 6) +
((c*Math.Pow(rho, 3)) / (T*T))*(1+gamma*rho*rho)*Math.Exp(-gamma*rho*rho);
Example taken from: Converting Math Equations in C#
Well, first you have to figure out what all those symbols mean. I see the sigma which usually indicates sum-of, with ∑_(n=0)^(N-1) probably translating to:
N-1
∑
n=0
This generally means the sum of the following expression where n varies from 0 to N-1. So I gather you'd need a loop there.
The expression to be calculated within that loop consists of a lof of trigonometric-type functions involving π, θ, sin and cos, and the little known sinc which I assume is a typo :-)
The bottom line is that you need to understand the current expression before you can think about converting it to another form like a C# program. Short of knowing where it came from, or a little bit of context, we probably can't help you that much though there's always a possibility that we have a savant/genius here that will recognise that formula off the top of their head.