How to write a while loop in GAMS - gams-math

Can anyone help me with this problem please. I want to add to i one by one and put the amount of x(i) equal to 1 in each step, so i wrote it as below but it's not working
loop(i,
x('0')=1;
t('0')=1;
while(t>m,
ord(i)=ord(i)+1;
display i;
x(i)=1;
display x;
t(i)=t(i-1) +1;
);
);
And by the way m is a variable which is calculated before this, in an equation.

If m is a variable from a model that was solved before, you should check its level using the .l attribute:
...
while(t>m.l,
...

Look at this page to have a better idea of how to make a while loop in GAMS. Also look at this code because it may help you:
root=minroot;
*find a sign switch
while(signswitch=0 and root le maxroot,
root=root+inc;
function_value2= a-b*root+c*sqr(root);
if((sign(function_value1) ne sign(function_value2)
and abs(function_value1) gt 0
and abs(function_value2) gt tolerance),
maxroot=root;
signswitch=1
else
if(abs(function_value2) gt tolerance,
function_value1=function_value2;
minroot=root;);
);
* display 'inside',minroot,maxroot,function_value1,function_value2;
);

Related

Identifier for nested loops?

right now I am learning about nested loops in PL/SQL.
To distinguish the loops I have to use marks like "outer_loop"and
"inner_loop".
My Question:
What happens when I have more than two loops which are nested? What would be the identifier of the third loop for example? Maybe "inner_inner_loop"?
Thank you in advance!
By "marks" you mean what Oracle calls labels. Labels are free text, and you can use any words you like. It is best to use something which describes the purpose of the loop: outer_loop, innr_loop are a bit meh. Labels are an opportunity to make our code clearer, so make the most of it.
The following is just a demonstration of how to use labels. I am not suggesting this is the best way to implement the code. Indeed, lots of nested loops is often a red flag.
<< departments >>
for d_rec in ( select * from dept ) loop
..
<< employees >>
for e_rec in ( select * from emp where emp.deptno = d_rec.dept_no ) loop
..
<< projects >>
for p_rec in ( select * from proj where proj.empno = e_rec.emp_no ) loop
..
end loop projects;
end loop employees;
end loop departments;

How to display the values of Vector in the data browser of DolphinDB?

I execute the following code in the GUI of DolphinDB.
incomes=table(2016.07.31 - 10..1 as date, rand(100,10) as income);
eventdates = [2016.07.22, 2016.07.25, 2016.07.29];
x = incomes.date.binsrch(eventdates);
incomes.date.cut(x);
Then the GUI show the result
like this.
But I don't know the meaning of com.xxdb.data.BasicDateVector#64e2a63c. Can you tell me how to display the values of Vector simply?
You can try this way:
incomes.date.cut(x)[0]
The number in [] is the index of element you want to access.

Foxpro String Variable combination in Forloop

As in title, there is an error in my first code in FOR loop: Command contains unrecognized phrase. I am thinking if the method string+variable is wrong.
ALTER TABLE table1 ADD COLUMN prod_n c(10)
ALTER TABLE table1 ADD COLUMN prm1 n(19,2)
ALTER TABLE table1 ADD COLUMN rbon1 n(19,2)
ALTER TABLE table1 ADD COLUMN total1 n(19,2)
There are prm2... until total5, in which the numbers represent the month.
FOR i=1 TO 5
REPLACE ALL prm+i WITH amount FOR LEFT(ALLTRIM(a),1)="P" AND
batch_mth = i
REPLACE ALL rbon+i WITH amount FOR LEFT(ALLTRIM(a),1)="R"
AND batch_mth = i
REPLACE ALL total+i WITH sum((prm+i)+(rbon+i)) FOR batch_mth = i
NEXT
ENDFOR
Thanks for the help.
There are a number of things wrong with the code you posted above. Cetin has mentioned a number of them, so I apologize if I duplicate some of them.
PROBLEM 1 - in your ALTER TABLE commands I do not see where you create fields prm2, prm3, prm4, prm5, rbon2, rbon3, etc.
And yet your FOR LOOP would be trying to write to those fields as the FOR LOOP expression i increases from 1 to 5 - if the other parts of your code was correct.
PROBLEM 2 - You cannot concatenate a String to an Integer so as to create a Field Name like you attempt to do with prm+i or rbon+1
Cetin's code suggestions would work (again as long as you had the #2, #3, etc. fields defined). However in Foxpro and Visual Foxpro you can generally do a task in a variety of ways.
Personally, for readability I'd approach your FOR LOOP like this:
FOR i=1 TO 5
* --- Keep in mind that unless fields #2, #3, #4, & #5 are defined ---
* --- The following will Fail ---
cFld1 = "prm" + STR(i,1) && define the 1st field
cFld2 = "rbon" + STR(i,1) && define the 2nd field
cFld3 = "total" + STR(i,1) && define the 3rd field
REPLACE ALL &cFld1 WITH amount ;
FOR LEFT(ALLTRIM(a),1)="P" AND batch_mth = i
REPLACE ALL &cFld2 WITH amount ;
FOR LEFT(ALLTRIM(a),1)="R" AND batch_mth = i
REPLACE ALL &cFld3 WITH sum((prm+i)+(rbon+i)) ;
FOR batch_mth = i
NEXT
NOTE - it might be good if you would learn to use VFP's Debug tools so that you can examine your code execution line-by-line in the VFP Development mode. And you can also use it to examine the variable values.
Breakpoints are good, but you have to already have the TRACE WINDOW open for the Break to work.
SET STEP ON is the Debug command that I generally use so that program execution will stop and AUTOMATICALLY open the TRACE WINDOW for looking at code execution and/or variable values.
Do you mean you have fields named prm1, prm2, prm3 ... prm12 that represent the months and you want to update them in a loop? If so, you need to understand that a "fieldName" is a "name" and thus you need to use a "name expression" to use it as a variable. That is:
prm+i
would NOT work but:
( 'pro'+ ltrim(str(m.i)) )
would.
For example here is your code revised:
For i=1 To 5
Replace All ('prm'+Ltrim(Str(m.i))) With amount For Left(Alltrim(a),1)="P" And batch_mth = m.i
Replace All ('rbon'+Ltrim(Str(m.i))) With amount For Left(Alltrim(a),1)="R" And batch_mth = m.i
* ????????? REPLACE ALL ('total'+Ltrim(Str(m.i))) WITH sum((prm+i)+(rbon+i)) FOR batch_mth = i
Endfor
However, I must admit, your code doesn't make sense to me. Maybe it would be better if you explained what you are trying to do and give some simple data with the result you expect (as code - you can use FAQ 50 on foxite to create code for data).

SUM function in PIG

Starting to learn Pig latin scripting and stuck on below issue. I have gone through similar questions on the same topic without any luck! Want to find SUM of all the age fields.
DUMP X;
(22)(19)
grunt> DESCRIBE X;
X: {age: int}
I tried several options such as :
Y = FOREACH ( group X all ) GENERATE SUM(X.age);
But, getting below exception.
Invalid field projection. Projected field [age] does not exist in schema: group:chararray,X:bag{:tuple(age:int)}.
Thanks for your time and help.
I think the Y projection should work as you wrote it. Here's mi little example code for the same and that's just work fine for me.
X = LOAD 'SO/sum_age.txt' USING PigStorage('\t') AS (age:int);
DESCRIBE X;
Y = FOREACH ( group X all ) GENERATE
SUM(X.age);
DESCRIBE Y;
DUMP Y;
So you your problem looks strange. I used the following input data:
-bash-4.1$ cat sum_age.txt
22
19
Can you make a try on the same data with script I inserted here?

Crystal Reports 8.5 showing multi-values from parameters on report footer

In Crystal Reports 8.5 when I have setup a parameter for multi-value the user enters 90654-90658A. Normally I would use Join() but being that this is not just text but numeric I have tried a few things but with no results.
Local NumberVar i;
Local NumberVar j;
Local StringVar param_values;
if 0 in {?CPT} then
"CPT #s: All CPTs"
else
(
for i := 1 to UBound ({?CPT}) do
for j := Minimum ({?CPT}[ i ]) to Maximum ({?CPT}[ i ]) do
param_values := param_values + "," + CStr (j, "#");
"CPT #s: " + Mid (param_values, 2)
)
This works fine for 90654-90658 but when the user selects 90654-90658A it fails.
Also the selection criteria will not pass to SQL in the query sent to SQL with the correct where clause. Meaning there is not indication that I am even asking for a where. It should show in the select for sql a where table.data >= '90654' and table.data <= '90658A'
I am lost as to where I am going wrong with this. Any help would be great this is my first time seeking an answer on this site but I have not received any help on this request.
Thanks
I tried a similar query with the Xtreme.mdb database, referencing the Customer table. I created a string, range parameter that accepted multiple values (i.e. multiple ranges).
When I supplied it with two ranges, the follow query was generated:
SELECT `Customer`.`Postal Code`
FROM `Customer` `Customer`
WHERE (
(`Customer`.`Postal Code`>='04000' AND `Customer`.`Postal Code`<='04999') OR
(`Customer`.`Postal Code`>='55000' AND `Customer`.`Postal Code`<='55999')
)
As you can see, Crystal Reports will build the necessary BETWEEN or >= <= statements.
In you situation, try:
( "0" IN {?CPT} OR {TABLE.FIELD} IN {?CPT} )
You could adapt your formula field to display the values of the parameter, if you want.
I do appreciate everyones input but I was able to work through the problem. For the record selection I put in the following. {TABLE.FIELD} in CStr({#MinCPT}) to CStr({#MaxCPT}). This pulled the range after I created two formulas. One MinCPT and the other MaxCPT. Here is the formula. Left (ToText (Minimum ({?CPT})),2 ) & Mid (ToText (Minimum ({?CPT})),4 ,3 ) and the same for Max. The report works fine now.
Thanks Again.