#!/bin/csh -f
# IND-version2.0
#    IND 2.0 released 9/15/92   contact: ind@kronos.arc.nasa.gov
#    by Wray Buntine (and others, see IND/README)
#    NASA Ames Research Center, MS 269-2, Moffett Field, CA 94035
#
#   make tree(s) using option in "gen"
#	$1  =  file stem
#   OPTIONS
#	-D	set echo
#	-p propts
#		prune with options
#       -R r n  makes n random trees
#		using identical options,
#		trees are numbered ".1", ".2" ... ".n"
#		passes "r" to the R flag in "tgen"
#       -o genopts
#		use options to "gen"
#       -c clopts
#		classify afterwards using options "clopts"
#		if "clopts" == "none", then cancel use if tclass
#	-e 	build from text data file ".bld" rather than ".enc"
#		and classify from complementary ".tst"
#       -H sampleopts
#		use ".enc" file and sample according to "sampleopts"
#		i.e.  first argument to "encsmpl"
#       -O old
#		use versions in directory "old"
#       -m datasize
#		set maximum memory size of tgen, etc., during running
#       -t cputime
#		set maximum time of tgen, etc., during running
#       -T 	switch on basic verbose and time each IND command
#	-i nice
#		nice this shell to this number (default 10)
#	-v 	verbose - show calls to tgen and tprune etc.
#		when used twice switches on verbose in the underlying calls
#	-s style
#		set up the various options ( -o, -p and -c )
#		according to the style given, multiple use simply overlays 
#		the styles and subsequent -o, -p or -c options overlay as well;
#		see also "-c none"
#			cart   for CART-like 1-SE
#			cart0   for CART-like 0-SE
#			id3     for id3 like - with no pruning
#			c4     for c4.5 like
#			mml    for mml with Bayesian smoothing
#			look   for lookahead, quality of test evaluated
#				 by growing a subtree of depth 2
#			tlook  for lookahead, quality of test evaluated
#                                by growing a full subtree greedily
#				 (only done for top 4 tests)
#		WARNING:  if this option quits with "time limit exceeded"
#			  then the tree will be unevenly grown and useless;
#			  rerun "mktree" with an extended time limit ("-t")
#			bayes  for Bayes trees
#			opt    for option trees
#                      	dgraph for decision graphs	
#                       lgraph for lookahead decision graphs
#                       treewan   for wandering in tree space
#		NB.  see below to add or change styles
#
#   NOTES
#	if tprune, tclass or tgen exit abnormally, "mktree" quits
#	and exits with the same exit status
#		status = 14 for alarm timeouts from tgen
#			 15 for out-of-memory from quickfit allocator

set td 			#   set to where you want tree files
set prune bin echoon set trees tname clopts callcl propts sampleopts timer
set genopts = ""
set genoptsc = 0
set datasize = 12m
set cputime = 2000
set nice = 10
set verb textin

#  STYLES
#  
#  these are duplicated in "ttest", so always update them both
#  these arrays define the styles

#    add the style name here
set stylename = ( cart cart0 id3 c4 mml smml look tlook bayes \
		opt opt1 dgraph lgraph dgraphwan treewan )
#    add the tgen command line here
set stylegen = ( "-it -Pnull -p1 -Sfull -s5 -C10 -n0" \
                 "-it -Pnull -p0 -Sfull -s1 -C10 -n0" \
                  "-ii -Pnull" \
                  "-ir -Pnull -s1,2 -Sfull" \
                 "-tPmml -A1 -n0.0001"  \
                 "-tPmml -Pchoices -A1 -n0.0001"  \
                 "-t -B2,4" \
                 "-t -B3,1,0.00001,4 -J1 " \
                 "-tAnonsym,1" \
                 "-t -B2,4 -J3,0.005,0.75,0.1 -K3,.05 -L0.9" \
                 "-t -B1,4 -J3,0.005,0.75,0.1 -K3,.05 -L0.9" \
                  "-tA1 -DD -tPmml" \
                  "-tA1 -DD -tPmml -B2,4" \
                  "-tA1 -DD -tPmml -w" \
                  "-tA1 -D -tPmml -w" )
#    add the tprune command line here
set styleprune = ( "-n" "-n" "-n" "-fn" "-b" "-B" "" "" "-b" "" "" \
			"-b" "-b" "-b" "-b" )
#    add the tclass command line here
set styleclass = ( "-slG" "-slG" "-sl" "-sl" "-slvg" "-slvg" "" "" "-slvg" "" "" \
		"-slvg" "-slvg" "-slvg" "-slvg"  )
#  end STYLES

while ( 1 )
switch ( $1 )
case "-v":
shift
if ( $verb ) then
   set genopts = "$genopts -v"
   set propts = "$propts -v"        
   set verb = 2
else
   set verb = 1
endif
breaksw
case "-e":
shift
set textin = 1
breaksw
case "-D":
shift
set echoon = 1
breaksw
case "-p":
shift
set prune = 1		# prune
set propts = "$propts $1"
shift
breaksw
case "-O":
shift
set bin = "$1"
shift
breaksw
case "-o":
	shift
	set genopts = "$genopts $1"	# options to "gen"
	shift
	breaksw
case "-s":
	shift
	set i = 1
	while ( $i <= $#stylename )
		if ( $stylename[$i] == "$1" ) break
		@ i ++
	end
shift
if ( $i > $#stylename ) then
	echo "mktree: invalid style option, should be $stylename"
	exit 1
endif
set prune = 1           # prune
set propts = ( $propts "$styleprune[$i]" )
set genopts = ( $genopts "$stylegen[$i]" )
if ( "$styleclass[$i]" != "" ) then
	set clopts = ( $clopts "$styleclass[$i]" )
	set callcl = 1
endif
breaksw
case "-T":
shift
set verb=1 timer="time "
breaksw
case "-t":
shift
set cputime = "$1"	# maximum cputime in secs.
shift
breaksw
case "-m":
shift
set datasize = "$1"	# maximum memory size, e.g.  10m, 1000k
shift
breaksw
case "-i":
shift
set nice = "$1"		#value for nice'ing this shell
shift
breaksw
case "-n":
shift
set tname = "$1"	# name for tree
shift
breaksw
case "-H":
shift
set sampleopts = "$1"
shift
breaksw
case "-O":
shift
set path = "( $1 $path )"
shift
breaksw
case "-c":
set callcl = 1
shift
if ( "$1" == "none" ) then
	set callcl
else
	set clopts = "$1"	  	#  options to pass to classify
endif
shift
breaksw
case "-R":
shift
set ran = $1	  	#  arg. for -R
shift
set trees = $1	  	#  no. of trees to do, others with -R
shift
breaksw
default:
break
endsw
end
if ( $#argv != 1 ) then
echo "mktree:  wrong arguments: $*"
echo "mktree:  [-OTeav -H smpl -p propts -c clopts -o genopt -n tname] filestem"
exit 1
endif
set genopts = ( $genopts )
if ( $callcl ) set clopts = ( $clopts )
if ( $textin ) then
set tsf = "$1.tst"
set infile = "$1.bld"
set genopts = ( -e $genopts )
set clopts = ( -e $clopts )
else
set tsf = "$1.enc"
set infile = "$1.enc"
endif
if ( $echoon ) set echo

if ( $verb > 1 ) then
	echo "limit datasize $datasize; limit stacksize $datasize"
	echo "limit cputime $cputime; nice +$nice"
endif
limit datasize $datasize
limit stacksize $datasize
limit cputime $cputime
nice +$nice

if ( "$sampleopts" != "" ) then
    if ( $verb ) then
	set sampleopts = ( -s $sampleopts )
	echo encsmpl $sampleopts $1
    endif
    ${bin}encsmpl $sampleopts $1
endif

if ( $trees ) then
  set i = 1
  #   this just ensures the following "rm" doesn't print "no match"
  rm -f ${1}.tree.[1-9][0-9] ${1}.treec.[1-9][0-9] ggg
  set rran  = "-R $ran"
  while ( $i <= $trees )
    if ( $verb ) echo tgen $genopts $rran  ${1}.attr ${infile} $td${1}.treec.$i
    onintr -
    ${timer}${bin}tgen $genopts $rran  ${1}.attr ${infile} $td${1}.treec.$i
    set stat = $status
    onintr 
    if ( $stat ) then
	echo "problems with tgen ($stat)"
	exit $stat
    endif
    if ( $prune ) then
      if ( $verb ) echo tprune $propts ${1}.attr $td${1}.treec.$i
      ${timer}${bin}tprune $propts ${1}.attr $td${1}.treec.$i
      set stat = $status
      if ( $stat ) then
	echo "problems with tprune ($stat)"
	exit $stat
      endif
    endif
    @ i++
  end
  if ( $callcl ) then
    if ( $verb ) echo tclass $clopts -m ${trees} ${1}.attr \
	 $td${1}.tree.[1-9] $td${1}.tree.[1-9][0-9] ${tsf}
    ${timer}tclass $clopts -m ${trees} ${1}.attr \
	 $td${1}.tree.[1-9] $td${1}.tree.[1-9][0-9] ${tsf}

  endif
else
  if ( $verb ) echo tgen $genopts ${1}.attr ${infile} $td${1}.treec${tname}
  onintr -
  ${timer}${bin}tgen $genopts ${1}.attr ${infile} $td${1}.treec${tname}
  set stat = $status
  onintr 
  if ( $stat ) then
	echo "problems with tgen ($stat)"
	exit $stat
  endif
  if ( $prune ) then
    if ( $verb ) echo tprune $propts ${1}.attr $td${1}.treec${tname}
    ${timer}${bin}tprune $propts ${1}.attr $td${1}.treec${tname}
    set stat = $status
    if ( $stat ) then
	echo "problems with tprune ($stat)"
	exit $stat
    endif
  endif
  if ( $callcl ) then
    if ( $verb ) echo tclass $clopts ${1}.attr $td${1}.tree${tname} ${tsf}
    ${timer}${bin}tclass $clopts ${1}.attr $td${1}.tree${tname} ${tsf}
  endif
endif
