AMPL ERROR: I am getting this error, i have mentioned and attached mod and dat files - meta

Below are my dat and mode files for ampl .
I am getting the following error:
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
MODEL FILE:
# AMPL model for the Minimum Cost Network Flow Problem
#
# By default, this model assumes that b[i] = 0, c[i,j] = 0,
# l[i,j] = 0 and u[i,j] = Infinity.
#
# Parameters not specified in the data file will get their default values.
reset;
options solver cplex;
set NODES; # nodes in the network
set ARCS within {NODES, NODES}; # arcs in the network
set english;
set french;
param b {NODES} default 0; # supply/demand for node i
param c {ARCS} default 0; # cost of one of flow on arc(i,j)
param l {ARCS} default 0; # lower bound on flow on arc(i,j)
param u {ARCS} default Infinity; # upper bound on flow on arc(i,j)
var x {ARCS}; # flow on arc (i,j)
maximize cost: sum{(i,j) in ARCS} c[i,j] * x[i,j]; #objective: minimize
#arc flow cost
subject to flow_balance {i in NODES}:
sum{j in NODES: (i,j) in ARCS} x[i,j] - sum{j in NODES: (j,i) in ARCS}
x[j,i] = b[i];A
subject to capacity {(i,j) in ARCS}: l[i,j] <= x[i,j] <= u[i,j];
subject to flow_conservation {i in english}:
sum{j in french} x[i,j] = 1;
subject to flow_bounds {(i,j) in ARCS}:
x[i,j] = 0 || x[i,j] <= 1;
#subject to Number: {(i,j) in ARCS} x[i,j]=0 || x[i,j] = 1;
data hw3.dat
solve;
printf "The optimal pair assignments with compatibility scores are: \n";
for {i in english, j in french} {
printf "English Child %d and French Child %d with compatibility score %d \n", i, j, c[i,j];
}
data;
set NODES :=e1 e2 e3 f1 f2 f3;
set ARCS:= (e1,f1) (e1,f2) (e1,f3) (e2,f1) (e2,f2) (e2,f3) (e3,f1) (e3,f2) (e3,f3);
set english:=e1 e2 e3;
set french:=f1 f2 f3;
param: b:=
1 1
1 1
1 1
1 1
1 1
1 1;
param: c l u:=
[e1,f1] 6 0 10
[e1,f2] 3 0 10
[e1,f3] 2 0 10
[e2,f1] 9 0 10
[e2,f2] 5 0 10
[e2,f3] 1 0 10
[e3,f1] 4 0 10
[e3,f2] 10 0 10
[e3,f3] 8 0 10
;
It keeps saying that b is already defined, but i didnt do it. i tried changing the name from b to some other thing, still shows the same error.
can someone help please.

In your data file you have:
param: b:=
1 1
1 1
1 1
1 1
1 1
1 1;
Each line means b[1] = 1 and that is why you are getting the error "b[1] already defined context".
Since b is indexed over NODES (param b {NODES} default 0;) you should have something like the following instead:
param: b :=
e1 1
e2 1
e3 1
f1 1
f2 1
f3 1;

Related

Ada - how to explicitly pack a bit-field record type?

Please consider the following experimental Ada program which attempts to create a 32-bit record with well defined bit fields, create one and output it to a file stream...
with System;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Streams.Stream_Io; use Ada.Streams.Stream_Io;
procedure Main is
type Bit is mod (2 ** 1);
type Opcode_Number is mod (2 ** 4);
type Condition_Number is mod (2 ** 4);
type Operand is mod (2 ** 9);
type RAM_Register is
record
Opcode : Opcode_Number;
Z : Bit;
C : Bit;
R : Bit;
I : Bit;
Cond : Condition_Number;
Rsvd_1 : Bit;
Rsvd_2 : Bit;
Dest : Operand;
Src : Operand;
end record;
for RAM_Register use
record
Opcode at 0 range 28 .. 31;
Z at 0 range 27 .. 27;
C at 0 range 26 .. 26;
R at 0 range 25 .. 25;
I at 0 range 24 .. 24;
Cond at 0 range 20 .. 23;
Rsvd_1 at 0 range 19 .. 19;
Rsvd_2 at 0 range 18 .. 18;
Dest at 0 range 9 .. 17;
Src at 0 range 0 .. 8;
end record;
for RAM_Register'Size use 32;
for RAM_Register'Bit_Order use System.High_Order_First;
-- ADA 2012 language reference 'full_type_declaration'
-- (page 758, margin number 8/3) for RAM_Register
pragma Atomic (RAM_Register);
-- 3 2 1 0
-- 10987654321098765432109876543210
-- OOOOzcriCONDrrDDDDDDDDDsssssssss
X : RAM_Register := (2#1000#,
2#1#,
2#1#,
2#1#,
2#1#,
2#1000#,
2#1#,
2#1#,
2#100000001#,
2#100000001#);
The_File : Ada.Streams.Stream_IO.File_Type;
The_Stream : Ada.Streams.Stream_IO.Stream_Access;
begin
begin
Open (The_File, Out_File, "test.dat");
exception
when others =>
Create (The_File, Out_File, "test.dat");
end;
The_Stream := Stream (The_File);
RAM_Register'Write (The_Stream, X);
Close (The_File);
end Main;
I used the info here: https://rosettacode.org/wiki/Object_serialization#Ada and here: https://en.wikibooks.org/wiki/Ada_Programming/Attributes/%27Bit_Order (the very last example) to create the above.
Running the code and examining the output with xxd -g1 test.dat gives the following 12 bytes of output...
00000000: 08 01 01 01 01 08 01 01 01 01 01 01 ............
QUESTION:
How can this 32 bit record be written to, or read from, a stream as 32 bits, observing all bitfield positions? Imagine I was communicating with a microcontroller on an RS-232 port, each bit will be required to be exactly in the right place at the right time. The syntax for RAM_Register use record... seems to have had no effect on how 'Write arranges its output.
If I do provide my own 'Read and 'Write implementations, doesn't that directly contradict the 'for RAM_Register use record...` code?
You probably will have to convert the instance to an unsigned integer (via an unchecked conversion) and then write the unsigned integer to the stream. The default implementation of Write ignores the representation clause (see also RM 13 9/3):
For composite types, the Write or Read attribute for each component is called in canonical order, [...]
So, add
with Interfaces; use Interfaces;
with Ada.Unchecked_Conversion;
and define RAM_Register as
type RAM_Register is
record
Opcode : Opcode_Number;
Z : Bit;
C : Bit;
R : Bit;
I : Bit;
Cond : Condition_Number;
Rsvd_1 : Bit;
Rsvd_2 : Bit;
Dest : Operand;
Src : Operand;
end record with Atomic;
procedure Write
(Stream : not null access Ada.Streams.Root_Stream_Type'Class;
Item : RAM_Register);
for RAM_Register'Write use Write;
for RAM_Register use
record
Opcode at 0 range 28 .. 31;
Z at 0 range 27 .. 27;
C at 0 range 26 .. 26;
R at 0 range 25 .. 25;
I at 0 range 24 .. 24;
Cond at 0 range 20 .. 23;
Rsvd_1 at 0 range 19 .. 19;
Rsvd_2 at 0 range 18 .. 18;
Dest at 0 range 9 .. 17;
Src at 0 range 0 .. 8;
end record;
for RAM_Register'Size use 32;
for RAM_Register'Bit_Order use System.High_Order_First;
-----------
-- Write --
-----------
procedure Write
(Stream : not null access Ada.Streams.Root_Stream_Type'Class;
Item : RAM_Register)
is
function To_Unsigned_32 is
new Ada.Unchecked_Conversion (RAM_Register, Unsigned_32);
U32 : Unsigned_32 := To_Unsigned_32 (Item);
begin
Unsigned_32'Write (Stream, U32);
end Write;
This yields
$ xxd -g1 test.dat
00000000: 01 03 8e 8f ....
Note: the bitorder may have been reversed as I had to comment the aspect specification for RAM_Register'Bit_Order use System.High_Order_First;

Do math on string count (and text parsing with awk)

I have a 4 column file (input.file) with a header:
something1 something2 A B
followed by many 4-column rows with the same format (e.g.):
ID_00001 1 0 0
ID_00002 0 1 0
ID_00003 1 0 0
ID_00004 0 0 1
ID_00005 0 1 0
ID_00006 0 1 0
ID_00007 0 0 0
ID_00008 1 0 0
Where "1 0 0" is representative of "AA", "0 1 0" means "AB", and "0 0 1" means "BB"
First, I would like to create a 5th column to identify these representations:
ID_00001 1 0 0 AA
ID_00002 0 1 0 AB
ID_00003 1 0 0 AA
ID_00004 0 0 1 BB
ID_00005 0 1 0 AB
ID_00006 0 1 0 AB
ID_00007 0 0 0 no data
ID_00008 1 0 0 AA
Note that the A's and B's need to be parsed from columns 3 and 4 of the header row, as they are not always A and B.
Next, I want to "do math" on the counts for (the new) column 5 as follows:
(2BB + AB) / 2(AA + AB + BB)
Using the example, the math would give:
(2(1) + 3) / 2(3 + 3 + 1) = 5/14 = 0.357
which I would like to append to the end of the desired output file (output.file):
ID_00001 1 0 0 AA
ID_00002 0 1 0 AB
ID_00003 1 0 0 AA
ID_00004 0 0 1 BB
ID_00005 0 1 0 AB
ID_00006 0 1 0 AB
ID_00007 0 0 0 no data
ID_00008 1 0 0 AA
B_freq = 0.357
So far I have this:
awk '{ if ($2 = 1) {print $0, $5="AA"} \
else if($3 = 1) {print $0, $5="AB"} \
else if($4 = 1) {print $0, $5="BB"} \
else {print$0, $5="no data"}}' input.file > output.file
Obviously, I was not able to figure out how to parse the info from row 1 (the header row, edited out "column 1"), much less do the math.
Thanks guys!
a more structured approach...
NR==1 {a["100"]=$3$3; a["010"]=$3$4; a["001"]=$4$4; print; next}
{k=$2$3$4;
print $0, (k in a)?a[k]:"no data";
c[k]++}
END {printf "\nB freq = %.3f\n",
(2*c["001"]+c["010"]) / 2 / (c["100"]+c["010"]+c["001"])}
UPDATE
For non binary data you can follow the same logic with some pre-processing. Something like this should work in the main block:
for(i=2;i<5;i++) v[i]=(($i-0.9)^2<=0.1^2)?1:0;
k=v[2] v[3] v[4];
...
here the value is quantized at one for the range [0.8,1] and zero otherwise.
To capture "B" or substitute set h=$4 in the first block and use it as printf "\n%s freq...",h,(2*c...

How does Weka evaluate classifier model

I used random forest algorithm and got this result
=== Summary ===
Correctly Classified Instances 10547 97.0464 %
Incorrectly Classified Instances 321 2.9536 %
Kappa statistic 0.9642
Mean absolute error 0.0333
Root mean squared error 0.0952
Relative absolute error 18.1436 %
Root relative squared error 31.4285 %
Total Number of Instances 10868
=== Confusion Matrix ===
a b c d e f g h i <-- classified as
1518 1 3 1 0 14 0 0 4 | a = a
3 2446 0 0 0 1 1 27 0 | b = b
0 0 2942 0 0 0 0 0 0 | c = c
0 0 0 470 0 1 1 2 1 | d = d
9 0 0 9 2 19 0 3 0 | e = e
23 1 2 19 0 677 1 22 6 | f = f
4 0 2 0 0 13 379 0 0 | g = g
63 2 6 17 0 15 0 1122 3 | h = h
9 0 0 0 0 9 0 4 991 | i = i
I wonder how Weka evaluate errors(mean absolute error, root mean squared error, ...) using non numerical values('a', 'b', ...).
I mapped each classes to numbers from 0 to 8 and evaluated errors manually, but the evaluation was different from Weka.
How to reimplemen the evaluating steps of Weka?

IBM DB2 SQLCODE -1424, SQLSTATE 54040 when trying to create a trigger

I have tried to create the following trigger (DB2/LINUXX8664 9.7.2 running over Ubuntu 10.04.2 LTS) but I always get this:
Error report:
DB2 SQL error: SQLCODE: -1424, SQLSTATE: 54040, SQLERRMC: 2
According IBM DB2 documentation:
Too many references to transition variables and transition table columns or the row length for these references is too long. Reason code=rc.
http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/com.ibm.db2.luw.messages.sql.doc/doc/msql01424n.html
I really do not understand that explanation. Here the stored procedure and trigger:
CREATE PROCEDURE SP_INSERT_UPDATE_REPNUM (
IN RECNUM INTEGER,
IN CUSTOMER_ID INTEGER,
IN Q_CODE VARCHAR(14),
IN S_CODE VARCHAR(14),
IN REP_STATUS INTEGER,
IN P_CODE INTEGER,
IN REPNUMRG_ID INTEGER,
IN VOLG_LET VARCHAR(1),
IN REP_DATUM DATE,
IN REP_INI VARCHAR(2),
IN INGEBOEKT_DATUM DATE,
IN INGEBOEKT_INI VARCHAR(2),
IN WIJZIGING_DATUM DATE,
IN WIJZIGING_INI VARCHAR(2),
IN OMSCR_STORING VARCHAR(5),
IN OMSCR_WERKZ VARCHAR(5),
IN OMSCR_OPMERKING VARCHAR(5),
IN OMSCR_GEBREK VARCHAR(5),
IN OMSCR_MAT VARCHAR(5),
IN REP_TIJD FLOAT,
IN REP_GEDAAN CHAR(1),
IN ACTION CHAR(1)
)
SPECIFIC SP_INSERT_UPDATE_REPNUM
DYNAMIC RESULT SETS 0
DETERMINISTIC
LANGUAGE JAVA
PARAMETER STYLE JAVA
NO DBINFO
NOT FENCED
THREADSAFE
MODIFIES SQL DATA
PROGRAM TYPE SUB
EXTERNAL NAME 'RepairMigration!insertServiceReport'
and here the trigger:
CREATE TRIGGER INSERT_REPNUM
AFTER INSERT ON REPNUM
REFERENCING NEW ROW AS NROW
FOR EACH ROW MODE DB2SQL
BEGIN
CALL SP_INSERT_UPDATE_REPNUM(
NROW.RECNUM,
NROW.CUSTOMER_ID,
NROW.Q_CODE,
NROW.S_CODE,
NROW.REP_STATUS,
NROW.P_CODE,
NROW.REPNUMRG_ID,
NROW.VOLG_LET,
NROW.REP_DATUM,
NROW.REP_INI,
NROW.INGEBOEKT_DATUM,
NROW.INGEBOEKT_INI,
NROW.WIJZIGING_DATUM,
NROW.WIJZIGING_INI,
NROW.OMSCR_STORING,
NROW.OMSCR_WERKZ,
NROW.OMSCR_OPMERKING,
NROW.OMSCR_GEBREK,
NROW.OMSCR_MAT,
NROW.REP_TIJD,
NROW.REP_GEDAAN,
'I'
);
END
the table structure is thsi one:
RECNUM INTEGER 4 0 N
Q_CODE CHARACTER 14 0 N ''
U_Q_CODE CHARACTER 14 0 N
S_CODE CHARACTER 14 0 N ''
U_S_CODE CHARACTER 14 0 N
VOLG_LET CHARACTER 1 0 N ''
U_VOLG_LET CHARACTER 1 0 N
REP_DATUM DATE 4 0 N '0001-01-01'
REP_INI CHARACTER 2 0 N ''
P_CODE INTEGER 4 0 N 0
CUSTOMER_ID INTEGER 4 0 N 0
REPNUMHD_ID INTEGER 4 0 N 0
REPNUMRG_ID INTEGER 4 0 N 0
REP_STATUS SMALLINT 2 0 N 0
KONTAKT_PER CHARACTER 25 0 N ''
KONTAKT_TEL CHARACTER 20 0 N ''
OMSCR_STORING VARCHAR 512 0 N ''
OMSCR_WERKZ VARCHAR 2000 0 N ''
OMSCR_MAT VARCHAR 512 0 N ''
WIJZIGING_DATUM DATE 4 0 N '0001-01-01'
WIJZIGING_INI CHARACTER 2 0 N ''
INGEBOEKT_DATUM DATE 4 0 N '0001-01-01'
INGEBOEKT_INI CHARACTER 2 0 N ''
REP_GEDAAN CHARACTER 1 0 N ''
U_REP_GEDAAN CHARACTER 1 0 N
STATUS SMALLINT 2 0 N 0
UW_OPDRACHT CHARACTER 20 0 N ''
REP_UREN SMALLINT 2 0 N 0
REP_MINUTEN SMALLINT 2 0 N 0
OMSCR_OPMERKING VARCHAR 1008 0 N ''
OMSCR_GEBREK VARCHAR 512 0 N ''
SERVICE_NUMMER INTEGER 4 0 N 0
PRIJS_OPGAVE CHARACTER 1 0 N ''
CURRENCY SMALLINT 2 0 N 0
REP_TIJD INTEGER 4 0 N 0
After trying to execute the trigger, I get the error. How can I solve this?
UPDATE
It looks that I need to create a temporary tablespace. Listing all my tablespaces, I got some with more than 4k, for example this one:
Tablespace ID = 7
Name = TEMPSPACE1234
Type = Database managed space
Contents = All permanent data. Regular table space.
State = 0x0000
Detailed explanation:
Normal
Total pages = 1024
Useable pages = 1008
Used pages = 432
Free pages = 576
High water mark (pages) = 528
Page size (bytes) = 32768
Extent size (pages) = 16
Prefetch size (pages) = 16
Number of containers = 1
1) Create a 32K Buffer Pool
Example:
CREATE BUFFERPOOL "BUFFERPOOLLARGE" IMMEDIATE
ALL DBPARTITIONNUMS SIZE AUTOMATIC
NUMBLOCKPAGES 0
PAGESIZE 32 K;
2) Create a 32K SYSTEM TEMPORARY TABLESPACE
Example:
CREATE SYSTEM TEMPORARY TABLESPACE "TEMPSPACELARGE"
IN DATABASE PARTITION GROUP "IBMTEMPGROUP"
PAGESIZE 32 K
MANAGED BY AUTOMATIC STORAGE
EXTENTSIZE 32
BUFFERPOOL "BUFFERPOOLLARGE"
OVERHEAD INHERIT
TRANSFERRATE INHERIT
USING STOGROUP "IBMSTOGROUP"
FILE SYSTEM CACHING;
3) Run CREATE PROCEDURE Script Again.
OBS:In the examples I used DB2 automatic TABLESPACE.

Runtime of while loop pseudocode

I have a pseudocode which I'm trying to make a detailed analysis, analyze runtime, and asymptotic analysis:
sum = 0
i = 1
while (i ≤ n){
sum = sum + i
i = 2i
}
return sum
My assignment requires that I write the cost/runtime for every line, add these together, and find a Big-Oh notation for the runtime. My analysis looks like this for the moment:
sum = 0 1
long i = 1 1
while (i ≤ n){ log n + 1
sum = sum + i n log n
i = 2i n log n
}
return sum 1
=> 2 n log n + log n + 4 O(n log n)
is this correct ? Also: should I use n^2 on the while loop instead ?
Because of integer arithmetic, the runtime is
O(floor(ln(n))+1) = O(ln(n)).
Let's step through your pseudocode. Consider the case that n = 5.
iteration# i ln(i) n
-------------------------
1 1 0 5
2 2 1 5
3 4 2 5
By inspection we see that
iteration# = ln(i)+1
So in summary:
sum = 0 // O(1)
i = 1 // O(1)
while (i ≤ n) { // O(floor(ln(n))+1)
sum = sum + i // 1 flop + 1 mem op = O(1)
i = 2i // 1 flop + 1 mem op = O(1)
}
return sum // 1 mem op = O(1)

Resources