set ORIG;
set DEST;
set LINKS within {ORIG,DEST};
printf {i in ORIG, j in DEST} "LINKS[%s,%s] = %g \n",i,j,LINKS[i][j];
data;
set ORIG := GARY CLEV PITT;
set DEST := FRA DET LAN WIN STL FRE LAF;
set LINKS :=
(GARY,*) DET LAN STL LAF
(CLEV,*) FRA DET LAN WIN STL LAF
(PITT,*) FRA WIN STL FRE;
end;
This stops with this error message:
LINKS cannot be subscripted
How can I define LINKS so that it can be accessed with LINKS[i][j]?
With some advice from gnu-help, this seems to be a better syntax
for this problem:
set ORIG;
set DEST;
param LINKS{ORIG, DEST} default 0;
for {i in ORIG, j in DEST} printf "LINKS[%s,%s] = %g \n",i,j,LINKS[i,j];
data;
set ORIG := GARY CLEV PITT;
set DEST := FRA DET LAN WIN STL FRE LAF;
param LINKS : FRA DET LAN WIN STL FRE LAF :=
GARY 1 2 3 4 5 6 .
CLEV . 2 . 4 . 6 .
PITT 1 2 3 4 5 6 7;
end;
Related
I am trying to make a double nested loop in R. The source is stored in routes and looks like this:
Airline AirlineID SourceAirport SourceAirportID DestinationAirport DestinationAirportID Codeshare Stops Equipment
Where every row is a flight. I am concerned with the SourceAirportID and the DestinationAirportID. My double nested loop should have the first index as the SourceAirportID to access a list of DestinationPortIDs. The nested loop needs to be of variable size because airports will not all have the same number of destinations. Here is my attempt:
graph <- list()
for (i in 1:11922) {
graph[i] <- list()
}
it <- 1
sid <- as.numeric(routes[1,4])
for (i in 1:length(routes$SourceAirportID)) {
if (sid != as.numeric((routes[i,4]))) {
sid <- as.numeric(routes[i,4])
it <- 1
}
else {
it <- it + 1
}
graph[sid][it] <- routes[sid,6]
}
Here are the first 4 rows of routes:
Airline AirlineID SourceAirport SourceAirportID DestinationAirport DestinationAirportID Codeshare Stops Equipment
17313 CG 1308 GKA 1 HGU 3 0 DH8 DHT
17314 CG 1308 GKA 1 LAE 4 0 DH8
17315 CG 1308 GKA 1 MAG 2 0 DH8
17316 CG 1308 GKA 1 POM 5 0 DH8
So I'm trying to get the list at graph[1] to contain 3 4 2 5. Instead graph[1] is null, graph[2] contains 4, graph[3] contains 2 and graph[3] contains 5. My code also throws over 50 warnings, so clearly I am doing something very wrong.
Appreciate the help!
I'm not sure if this is what you want:
#reproduciable data:
routes <- data.frame(SourceAirport=c('a','a','b','c','a','b','b','c'),
DestinationAirport=c('Q','W','D','D','Q','E','R','T'))
View(routes)
graph <- list()
o <- unique(routes$SourceAirport)
#DestinationAirports of routes that share the same SourceAirport will be saved within an element in graph
for (i in 1:length(o)) {
graph[[i]] <- routes$DestinationAirport[routes$SourceAirport==o[i]]
}
Sample of File I'm reading in
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
013160,32.8347,-88.1342, 38.1 AL GAINESVILLE LOCK
013511,32.7017,-87.5808, 67.1 AL GREENSBORO
013816,31.8700,-86.2542, 132.0 AL HIGHLAND HOME
015749,34.7442,-87.5997, 164.6 AL MUSCLE SHOALS AP
017157,34.1736,-86.8133, 243.8 AL SAINT BERNARD
017304,34.6736,-86.0536, 187.5 AL SCOTTSBORO
GAWK Code
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print $1,$2,$3,$4
station=""$1 #Forces to be string
#Save latitude
stationInfo[station][lat]=$2
print "lat",stationInfo[station][lat]
#Save longitude
stationInfo[station][lon]=$3
print "lon",stationInfo[station][lon]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station][lat]
print "---------------"
}
Sample output
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
lat,31.0581
lon,-87.0547
lat,-87.0547
---------------
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
lat,30.5467
lon,-87.8808
lat,-87.8808
---------------
For some reason the value stored in stationInfo[station][lat] is being overwritten by the longitude. I'm at a loss for what in the world is going on.
I'm using GAWK 4.1.1 on Fedora 22
Your problem is the fact that lon and lat are variables and evaluate to the empty string so this assignment stationInfo[station][lat]=$2 and stationInfo[station][lon]=$3 are assigning to stationInfo[station]["].
You need to quote the lat and lon in those (and the other) lines to use strings instead of variables.
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print $1,$2,$3,$4
station=""$1 #Forces to be string
#Save latitude
stationInfo[station]["lat"]=$2
print "lat",stationInfo[station]["lat"]
#Save longitude
stationInfo[station]["lon"]=$3
print "lon",stationInfo[station]["lon"]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station]["lat"]
print "---------------"
}
I was implementing adjacency list in Pascal (by first reading edge end points, and then using dynamic arrays to assign required amount of memory to edgelist of each node). The program executes fine, gives correct outputs but gives runtime error 216 just before exiting.
The code is :
type aptr = array of longint;
var edgebuf:array[1..200000,1..2] of longint;
ptrs:array[1..100000] of longint;
i,j,n,m:longint;
elist:array[1..100000] of aptr;
{main}
begin
readln(n,m);
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
readln(edgebuf[i][1],edgebuf[i][2]);
inc(ptrs[edgebuf[i][1]]);
end;
for i:=1 to n do begin
setlength(elist[i],ptrs[i]);
end;
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
inc(ptrs[edgebuf[i][1]]);
elist[edgebuf[i][1]][ptrs[edgebuf[i][1]]]:=edgebuf[i][2];
end;
for i:=1 to n do begin
writeln(i,' begins');
for j:=1 to ptrs[i] do begin
write(j,' ',elist[i][j],' ');
end;
writeln();
writeln(i,' ends');
end;
writeln('bye');
end.
When run on file
4 5
1 2
3 2
4 3
2 1
2 3
gives output:
1 begins
1 2
1 ends
2 begins
1 1 2 3
2 ends
3 begins
1 2
3 ends
4 begins
1 3
4 ends
bye
Runtime error 216 at $0000000000416644
$0000000000416644
$00000000004138FB
$0000000000413740
$0000000000400645
$00000000004145D2
$0000000000400180
Once the program says "bye", what is the program executing that is giving runtime error 216?
RTE 216 is in general fatal exceptions. GPF/SIGSEGV and in some cases SIGILL/SIGBUS, and that probably means that your program corrupts memory somewhere.
Compile with runtime checks on might help you find errors (Free Pascal : -Criot )
Please I am a little new to this field so pardon me if the question sound trivial or basic.
I have a group of dataset(Bag of words to be specific) and I need to generate a proximity matrix by using their edit distance from each other to find and generate the proximity matrix .
I am however quite confused how I will keep track of my data/strings in the matrix. I need the proximity matrix for the purpose of clustering.
Or How generally do you approach this kinds of problem in the field. I am using perl and R to implement this.
Here is a typical code in perl I have written that reads from a text file containing my bag of words
use strict ;
use warnings ;
use Text::Levenshtein qw(distance) ;
main(#ARGV);
sub main
{
my #TokenDistances ;
my $Tokenfile = 'TokenDistinct.txt';
my #Token ;
my $AppendingCount = 0 ;
my #Tokencompare ;
my %Levcount = ();
open (FH ,"< $Tokenfile" ) or die ("Error opening file . $!");
while(<FH>)
{
chomp $_;
$_ =~ s/^(\s+)$//g;
push (#Token , $_ );
}
close(FH);
#Tokencompare = #Token ;
foreach my $tokenWord(#Tokencompare)
{
my $lengthoffile = scalar #Tokencompare;
my $i = 0 ;
chomp $tokenWord ;
##TokenDistances = levDistance($tokenWord , \#Tokencompare );
for($i = 0 ; $i < $lengthoffile ;$i++)
{
if(scalar #TokenDistances == scalar #Tokencompare)
{
print "Yipeeeeeeeeeeeeeeeeeeeee\n";
}
chomp $tokenWord ;
chomp $Tokencompare[$i];
#print $tokenWord. " {$Tokencompare[$i]} " . " $TokenDistances[$i] " . "\n";
#$Levcount{$tokenWord}{$Tokencompare[$i]} = $TokenDistances[$i];
$Levcount{$tokenWord}{$Tokencompare[$i]} = levDistance($tokenWord , $Tokencompare[$i] );
}
StoreSortedValues ( \%Levcount ,\$tokenWord , \$AppendingCount);
$AppendingCount++;
%Levcount = () ;
}
# %Levcount = ();
}
sub levDistance
{
my $string1 = shift ;
#my #StringList = #{(shift)};
my $string2 = shift ;
return distance($string1 , $string2);
}
sub StoreSortedValues {
my $Levcount = shift;
my $tokenWordTopMost = ${(shift)} ;
my $j = ${(shift)};
my #ListToken;
my $Tokenfile = 'LevResult.txt';
if($j == 0 )
{
open (FH ,"> $Tokenfile" ) or die ("Error opening file . $!");
}
else
{
open (FH ,">> $Tokenfile" ) or die ("Error opening file . $!");
}
print $tokenWordTopMost;
my %tokenWordMaster = %{$Levcount->{$tokenWordTopMost}};
#ListToken = sort { $tokenWordMaster{$a} cmp $tokenWordMaster{$b} } keys %tokenWordMaster;
##ListToken = keys %tokenWordMaster;
print FH "-------------------------- " . $tokenWordTopMost . "-------------------------------------\n";
#print FH map {"$_ \t=> $tokenWordMaster{$_} \n "} #ListToken;
foreach my $tokey (#ListToken)
{
print FH "$tokey=>\t" . $tokenWordMaster{$tokey} . "\n"
}
close(FH) or die ("Error Closing File. $!");
}
the problem is how can I represent the proximity matrix from this and still be able to keep track of which comparison represent which in my matrix.
In the RecordLinkage package there is the levenshteinDist function, which is one way of calculating an edit distance between strings.
install.packages("RecordLinkage")
library(RecordLinkage)
Set up some data:
fruit <- c("Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry",
"Blackcurrant", "Blueberry", "Currant", "Cherry")
Now create a matrix consisting of zeros to reserve memory for the distance table. Then use nested for loops to calculate the individual distances. We end with a matrix with a row and a column for each fruit. Thus we can rename the columns and rows to be identical to the original vector.
fdist <- matrix(rep(0, length(fruit)^2), ncol=length(fruit))
for(i in seq_along(fruit)){
for(j in seq_along(fruit)){
fdist[i, j] <- levenshteinDist(fruit[i], fruit[j])
}
}
rownames(fdist) <- colnames(fdist) <- fruit
The results:
fdist
Apple Apricot Avocado Banana Bilberry Blackberry Blackcurrant
Apple 0 5 6 6 7 9 12
Apricot 5 0 6 7 8 10 10
Avocado 6 6 0 6 8 9 10
Banana 6 7 6 0 7 8 8
Bilberry 7 8 8 7 0 4 9
Blackberry 9 10 9 8 4 0 5
Blackcurrant 12 10 10 8 9 5 0
Blueberry 8 9 9 8 3 3 8
Currant 7 5 6 5 8 10 6
Cherry 6 7 7 6 4 6 10
The proximity or similarity (or dissimilarity) matrix is just a table that stores the similarity score for pairs of objects. So, if you have N objects, then the R code can be simMat <- matrix(nrow = N, ncol = N), and then each entry, (i,j), of simMat indicates the similarity between item i and item j.
In R, you can use several packages, including vwr, to calculate the Levenshtein edit distance.
You may also find this Wikibook to be of interest: http://en.wikibooks.org/wiki/R_Programming/Text_Processing
Hi I have products that are made up of a couple of options. Each Option has a SKU Code. You can only select one option from each SKU Group and the options have to be concatenated in the order of the SKUGroup.
So for example i would have a list of options in a table in the DB that looked like
OptID PID SKU Price SKUGroup
156727 93941 C 171.00 1
156728 93941 BN 171.00 1
156729 93941 PN 171.00 1
156718 93940 W 115.20 2
156719 93940 CA 115.20 2
156720 93940 BA 115.20 2
156721 93940 BNA 115.20 2
156722 93940 BN 115.20 2
156723 93940 BS 115.20 2
156716 93939 CHR 121.50 3
156717 93939 NK 138.00 3
And a few finished product SKUs would look something like:
C-W-CHR 407.70
C-W-NK 424.20
C-CA-CHR 407.20
C-CA-NK 424.20
I am trying to make a script that will create a listing of every possible combination of SKU and the price of the combined options.
I need this done in Classic ASP (vbscript) and I'm not that familiar with it. So I'm looking for all the help I can get.
Thanks!
I would start by connecting to the database and creating three recordsets.
Set connection = CreateObject("ADODB.Connection")
connection.Open ConnectionString
Set rsOption1 = CreateObject("ADODB.recordset")
Set rsOption2 = CreateObject("ADODB.recordset")
Set rsOption3 = CreateObject("ADODB.recordset")
rsOption1.Open "SELECT * FROM TableName WHERE SKUGroup = 1", connection, 3,3
rsOption2.Open "SELECT * FROM TableName WHERE SKUGroup = 2", connection, 3,3
rsOption3.Open "SELECT * FROM TableName WHERE SKUGroup = 3", connection, 3,3
Then you can use nested loops to get the combinations. Something like this (Untested, this probably will not work as is, but it gives you an idea of how to do this) (Also this assumes that you have to select at least one option from each group)
for i = 0 to rsOption1.RecordCount
rsOption1.Move i, 1
for j = 0 to rsOption2.RecordCount
rsOption2.Move j, 1
for k = 0 to rsOption3.RecordCount
rsOption3.Move k, 1
'Write rsOption1.Fields(2).Value & "-" & rsOption2.Fields(2).Value & _
'"-" & rsOption3.Fields(2).Value & " " & _
'FormatCurrency((rsOption1.Fields(3).Value + rsOption2.Fields(3).Value + rsOption3.Fields(3).Value))
Next
Next
Next