Beginner to AMPL, what may be the reason for the error? - ampl

I am an absolute rookie to AMPL, I would really appreciate if anyone could help me solve this issue!
When I try to run (solve) the following code:
#Part 0 declaration of parameters
param n;
param m;
``set N:=1..n;#number of bondsset M:=0..m;` #number of years
param a{N}; #price of bonds
param L{M}; #liabilities
param d{M,N}; #payment structure
#Part 1 declaration of variables
var x{N} \>=0, integer; #investment on bonds
var z{M} \>=0; #cash on hand
#Part 2 objective function
minimize cost: sum{j in N} a\[j\]\*x\[j\];
#Part 3 constraints
subject to initial: z\[0\]=0;
subject to liability {i in M}: z\[i-1\]-z\[i\]+sum{j in N}d\[i,j\]\*x\[j\]\>=L\[i\];
I ran into the following error:
Error executing "solve" command:
error processing constraint liability[0]:
invalid subscript z[-1]
Do you know what may be the issue?

Related

Error: this expression has type unit but an expression was expected of type int

I am trying to do this simple while loop but it's not working. It is giving me the following error:
this expression has type unit but an expression was expected of type
int
let function () =
let current = ref 0 in
let square = ref !current in
while(((square := !current * !current) mod 1000000) != 269696) do
current := !current +1
done;
!square
;;
First of all, function is a keyword in OCaml, you cannot use it to name your functions.
Also, the condition in the while loop is buggy:
this square := !current * !current is an assignment, which type is unit. It is a type error to use it as an argument of mod which takes two integers as input.
You should probably do the assignment inside the loop and only test !square mod 1000000 <> 269696 in the loop's condition.
Notice that i used the <> structural inequality, instead of the physical one != which by luck does the same thing for integers, but you should not use it unless you know that.

I have some illegal expressions in my code but I don't know how to fix it

This is my code, I have illegal expressions at line 39/45, 40/50, 51/45, 52/36 and fatal: Unexpected end of file at line 64 It would be very helpful if some you will teach me a few things about pascal.
Program PatientRecords;
{Aurthor: Vincent Lopez,
February 16,2015,
Program will read names, age and treatment cost for at least ten patients}
Var {declaration of variables}
Name: array [1..11] of string;
Age: array [1..11] of integer;
T_Cost: array [1..11] of real;
G_Cost: array [1..11] of real;
P_Cost: array [1..11] of real;
Count: integer;
Begin {The program starts here}
{Initialization of variables}
For count := 1 to 10 DO
WriteLn('Welcome to Ministry of Health Cost of Health Care');
WriteLn('Enter the name of the patient');
Readln(name[count]);
Writeln('Enter the age of the patient');
Readln(Age[count]);
Writeln('Enter the treatment cost for patient');
Readln(T_cost[count]);
IF Age>= 65 THEN
Begin
G_cost[count]=T_cost[count]*(100/80);
P_cost[count]=T_cost[count]-G_cost[count];
Writeln('Government will pay = $',G_cost[count]);
Writeln('The patient will pay = $',P_cost[count]);
end
ELSE
Begin
G_cost[count]=T_cost[count]*(100/50);
P_cost=T_cost-G_cost[count];
Writeln('Government will pay = $',G_cost[count]);
Writeln('The patient will pay = $',P_cost[count]);
Readln;
Writeln('Press enter to continue');
Readln;
END; {The program ends here}
One obvious error is the statement
IF Age>= 65 THEN
Age is an array of integers, so this presumably should be
IF Age[count] >= 65 THEN
Another obvious error is that the last line should be 'END.' with a full stop. The 'end;' which you have at the moment is aligned to the 'begin' block following the 'else' branch of the 'if age[count]' statement.
I suggest that first you learn how to run the program for one set of data, then expand it. You will discover that the 'writeln/readln' pair are in the wrong place.

ORACLE SQL Method Produces Warning

I'm having trouble getting the following member method to compile (count_single_buses). Would appreciate any advice on what might be wrong syntactically with my code.
CREATE OR REPLACE TYPE BodyModel2_Type AS OBJECT(
ModelID INTEGER,
ModelName VARCHAR2(45),
FloorType VARCHAR2(45),
Manufacturer VARCHAR2(45),
Length NUMBER(8,2),
Width NUMBER(8,2),
NoOfAxles INTEGER,
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN INTEGER);
/
CREATE OR REPLACE TYPE BODY BodyModel2_Type AS
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN INTEGER IS
N INTEGER;
BEGIN
N := (SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S
WHERE S.BODYMODELREF = ModelID);
RETURN N;
END count_single_buses;
END;
--EDIT--
Thanks to #Ravi, I managed to solve the issue my correcting my SQL syntax and setting the resultset to a NUMBER, instead of INTEGER.
CREATE OR REPLACE TYPE BODY BodyModel_Type AS
MEMBER FUNCTION count_single_buses(thisModelID INTEGER) RETURN NUMBER IS
NUM NUMBER;
BEGIN
SELECT COUNT(S.BODYMODELREF) INTO NUM FROM SINGLEDECKBUS_TABLE S WHERE S.BODYMODELREF.MODELID = thisModelID;
RETURN NUM;
END count_single_buses;
END;
/
Still not sure why #Ravi's exact code still produced the warning, and thought that resultset when returning a count value could go into an integer. At any rate, the code works now. Thanks all.
Your BodyModel2_Type Type definition looks okay. However, the body definition is syntactically incorrect.
You cannot define a SQL statement directly to a variable, thus making this statement wrong.
N := (SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S
WHERE S.BODYMODELREF = ModelID);
You will have to use Select... into statement in order to assign the result set of your SQL query into a variable. So, the right syntax should look like this
SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S INTO N
WHERE S.BODYMODELREF = ModelID
AFAIK you don't have END the Type followed by the Type name like this END count_single_buses. It'll produce an error. So, overall your Type body specification should look like this:
CREATE OR REPLACE TYPE BODY BodyModel2_Type AS
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN NUMBER IS
N NUMBER;
BEGIN
SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S INTO N
WHERE S.BODYMODELREF = ModelID;
RETURN (N);
END;
END;
/
I'm writing this off without any live environment available right now so please let me know if you come across any error in the above code.
Cheers.

Rerun Error Message While Creating a Function

i am fairly new to SQL programming, and I am currently learning to create FUNCTIONS.
The problem that I am having, is creating the following FUNCTION.
create function CreatePI
(
)
returns decimal(10,6)
with returns null on null input
as
begin
declare #P as decimal(10,6)
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)
return #P
end
go
The above function is supposed to replicate the number PI. But the problem that I am having is:
Msg 156, Level 15, State 1, Procedure CreatePI, Line 11
Incorrect syntax near the keyword 'return'.
If anyone could help me out with why I am getting this problem, it would be greatly appriciatead.
You are missing a closing paren on the set line:
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15))
----------------------------------------------------------^

SQL GOTO statement using variable label

i was hoping to be able to do something like this:
declare #label varchar
set #label = 'label_1'
goto #label
label_1:
label_2:
of course, sql server gives me an incorrect syntax error... so i was wondering if i can still do this with a slightly different syntax?
I've cooked up a functional hack that may prove useful to others. On SQL Server 2008, you cannot dynamically build the goto, nor can you supply the label in a variable. The old school hack is to load the variable, jump to a single point that then uses if statements to determine the real destination.
GOTOs are of course considered harmful and are generally a bad idea.
l1:
print '1'
l2:
print '2'
goto l4
l3:
print '3' -- should not print
l4:
print '4'
declare #lbl nvarchar(5)
set #lbl = N'l6'
goto vjump
-- goto #lbl -- doesn't work
-- exec('goto ' + #lbl) -- doesn't work
l5:
print '5'
l6:
print '6'
l7:
print '7'
return
vjump:
if #lbl = 'l1'
goto l1
if #lbl = 'l2'
goto l2
if #lbl = 'l3'
goto l3
if #lbl = 'l6'
goto l6
This produces
1
2
4
6
7
You can only do this if you dynamically build the SQL statement.
GOTOs are best avoided though - mainly for code readability.
Why not simply use an if/else?
As far as I know, you cannot parameterize GOTO. You can do that if you use Dynamic SQL.
Not a timely answer obviously, but when I recently found myself wanting to do this, the reason was because I wanted to execute the same bit of code multiple times and then return to the normal flow, basically "gosub" and not "goto"....
And that you can do. Not with a gosub, but with a temporary stored procedure -- create a temp stored procedure create procedure #DRY and then call it. You can drop it when you are done.
try this:
exec("goto "+#label)