📄 ines2hugin
字号:
#!/usr/bin/perl -w######################################################################### File: ines2hugin.pl ## Author: D. Nauck, BT Labs ##----------------------------------------------------------------------## Description: ## ============ ## This Perl script translates a INES network file ## into a HUGIN network file. ## 2002.07.18 adapted to new INES version by Christian Borgelt #########################################################################use strict;my $script = "Ines2Hugin"; # name of scriptmy $command = "ines2hugin"; # string to invoke scriptmy $author = "Detlef Nauck";my $last_change = "18 Jul 2002, Christian Borgelt";my $description = "Convert a Bayesian network in INES format into HUGIN format.\n\n";my $ines_file;my $hugin_file;my @ines = ();my $lines;my $line;my $size;my $node;my $domain;my @values;my $x_pos = 50;my $y_pos = 70;my $x_inc = 50;my $y_inc = 70;my $indent = " ";my $nodes = 0;my $zero_row_sums = 0;my $distributions = 0;my $nargs = @ARGV;print ("$script by $author (last change: $last_change)\n");print ($description);if ($nargs != 2){ usage(); exit (-1);}$ines_file = $ARGV[0];$hugin_file = $ARGV[1];-r $ines_file or die ("Cannot open $ines_file for reading.\n");$size = -s $ines_file;print ("Reading $ines_file ...\n");open (INES, "$ines_file") or die ("Cannot open $ines_file for reading.\n");;@ines = <INES>;close (INES);$lines = @ines;print ("done ($lines lines, $size bytes).\n");print ("Starting conversion, creating output file $hugin_file ...\n");open (HUGIN, ">$hugin_file") or die ("Cannot open $hugin_file for output.\n");#-----------------------------------------------------------------------# Header of Hugin Network File#----------------------------------------------------------------------- print(HUGIN "net\n", "{\n", " node_size = (80 40);\n", " HRUNTIME_Monitor_InitStates = \"5\";\n", " HRUNTIME_Monitor_OpenGraph = \"0\";\n", " HRUNTIME_Monitor_GraphPrecision = \"100\";\n", " HRUNTIME_Monitor_AutoUpdGraph = \"0\";\n", " HRUNTIME_Compile_ApproxEpsilon = \"0.00001\";\n", " HRUNTIME_Compile_Approximate = \"0\";\n", " HRUNTIME_Compile_Compress = \"0\";\n", " HRUNTIME_Compile_TriangMethod = \"0\";\n", " HRUNTIME_Propagate_AutoNormal = \"1\";\n", " HRUNTIME_Propagate_AutoSum = \"1\";\n", " HRUNTIME_Propagate_Auto = \"0\";\n", " HRUNTIME_Font_Italic = \"0\";\n", " HRUNTIME_Font_Weight = \"400\";\n", " HRUNTIME_Font_Size = \"-12\";\n", " HRUNTIME_Font_Name = \"Arial\";\n", " HRUNTIME_Grid_GridShow = \"0\";\n", " HRUNTIME_Grid_GridSnap = \"1\";\n", " HRUNTIME_Grid_Y = \"10\";\n", " HRUNTIME_Grid_X = \"10\";\n}\n\n\n");#-----------------------------------------------------------------------# Translate domain information into node information#-----------------------------------------------------------------------# skip until first dom line is found$line = shift(@ines);while($line !~ m/^dom/){ $line = shift(@ines);}# convert dom lineswhile($line =~ m/^dom/){ $node = $line; $node =~ s/^dom\((.*)\).*\s*/$1/; print HUGIN ("node $node\n{\n", $indent, "label = \"$node\";\n", $indent, "position = ($x_pos $y_pos);\n", $indent, "states = (\""); $domain = $line; $domain =~ s/.*\{ (.*) \}.*\s*/$1/; $domain =~ s/, /" "/g; print HUGIN ("$domain\");\n}\n\n"); $line = shift (@ines); $x_pos += $x_inc; $y_pos += $y_inc; $y_inc *= -1; $nodes++;}while (@ines > 0){ $line = shift (@ines); if ($line =~ m/^ prob\(/){ $line =~ s/ prob/potential/; $line =~ s/=/\{/; $line =~ s/,/ /g; $line =~ s/\|/ \| /; print HUGIN ("$line","data =\n"); $line = shift (@ines); while (($line !~ m/^$/) && ($line !~ m/^\};/)) { $line =~ s/\([ |0-9]+\)//; $line =~ s/\{/\(/g; $line =~ s/\}/\)/g; $line =~ s/\w+\s*://g; $line =~ s/,//g; if ($line !~ m/[1-9]/){ # if the are only zeros $line =~ s/0/1/g; # then replace them by ones (equal prob.) $zero_row_sums++; } print HUGIN ($line); $line = shift(@ines); $distributions++; } print HUGIN ("}\n\n"); }}close (HUGIN);print ("done.\n");print ("The network has $nodes nodes.\n");print ("There are $distributions conditional probability distributions.\n");if ($zero_row_sums > 0){ print ("$zero_row_sums distributions with zero row sum have been detected\n"); print ("and have been converted to equal distributions.\n");}sub usage{ print ("Usage: $command <ines file> <hugin file>\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -