-- -*- Mode: Sather; -*- -- File: sf.sa -- Author: Chu-Cheow Lim (clim@ICSI.Berkeley.EDU) -- Copyright (C) International Computer Science Institute, 1993 -- -- COPYRIGHT NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY -- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC -- LICENSE contained in the file: "sather/doc/license.txt" of the Sather -- distribution. The license is also available from ICSI, 1947 Center -- St., Suite 600, Berkeley CA 94704, USA. --*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --* FUNCTION: --* --* CLASSES: --* --* REQUIRED FILES: --* --* RELATED FILES: --* --* HISTORY: --* Last edited: Jun 1 17:33 1993 (clim) --* Created: Fri May 21 17:20:11 1993 (clim) --*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class SF is -- A top-level program for sharks-fish problem. -- Decisions made at this level: -- * No shark and fish will ever be in the same position. This is -- maintained by (i) checking initial condition (ii) a fish never -- moves into a position where there is a shark. -- -- Questions: -- * How to parallelize random number generator? -- Represents the grounds where the creatures roam. shared ocean:DIST_VERT_OCEAN{OCEAN{$ABSTRACT_CREATURE},$ABSTRACT_CREATURE}; -- Bags of sharks/fish. shared sharks:DIST_OCEAN_OF_PREDATORS; shared fish:DIST_OCEAN_OF_PREY; -- Sizes of ocean. shared ocean_height:INT := 20; shared ocean_width:INT := 20; -- Set to `true' when the grid is to be displayed at end of each iteration. shared display:BOOL; -- Number of iterations. shared num_iters:INT := 10; -- Density of fish/sharks shared fish_density:DOUBLE := 0.2; shared shark_density:DOUBLE := 0.1; -- Current iteration no. shared curr_iter:INT := 0; main(args:ARRAY{STR}) is setup_options(args); setup_values; compute; end; -- main setup_options(args:ARRAY{STR}) is -- "-disp" : Display grid at end of each iteration. -- "-o-ht" : Height of ocean. -- "-o-wd" : Length of ocean. -- "-iter" : Number of iterations. if (args.asize > 1) then i:INT; n:INT := args.asize; until (i >= n) loop if (args[i].is_equal("-disp")) then display := true; elsif (args[i].is_equal("-f-age")) then i := i+1; if (i >= n) then ERR::s("(Warning): -f-age option ignored").nl; else FISH::sync_breed_age(args[i].to_i) end; elsif (args[i].is_equal("-s-age")) then i := i+1; if (i >= n) then ERR::s("(Warning): -s-age option ignored").nl; else SHARK::sync_breed_age(args[i].to_i) end; elsif (args[i].is_equal("-f-den")) then i := i+1; if (i >= n) then ERR::s("(Warning): -f-den option ignored").nl; else fish_density := args[i].to_d end; elsif (args[i].is_equal("-s-den")) then i := i+1; if (i >= n) then ERR::s("(Warning): -s-den option ignored").nl; else shark_density := args[i].to_d end; elsif (args[i].is_equal("-o-ht")) then i := i+1; if (i >= n) then ERR::s("(Warning): -o-ht option ignored").nl; else ocean_height := args[i].to_i end; elsif (args[i].is_equal("-o-wd")) then i := i+1; if (i >= n) then ERR::s("(Warning): -o-wd option ignored").nl; else ocean_width := args[i].to_i end; elsif (args[i].is_equal("-iter")) then i := i+1; if (i >= n) then ERR::s("(Warning): -iter option ignored").nl; else num_iters := args[i].to_i end; end; -- if i := i+1; end; -- loop end; -- if end; -- setup_options setup_values is ocean := DIST_VERT_OCEAN{OCEAN{$ABSTRACT_CREATURE}, $ABSTRACT_CREATURE}::create(ocean_height, ocean_width); sharks := DIST_OCEAN_OF_PREDATORS::create(ocean_height, ocean_width); fish := DIST_OCEAN_OF_PREY::create(ocean_height, ocean_width); sharks.init_shared_grounds(ocean); fish.init_shared_grounds(ocean); setup_sharks_fish; end; -- setup_values setup_sharks_fish is -- Create some sharks and fish initially. num_sharks,num_fish:INT; random:RANDOM := RANDOM::create; random.init(333); y:INT; until (y >= ocean_height) loop x:INT; until (x >= ocean_width) loop c:$ABSTRACT_CREATURE; if (random.uniform < fish_density) then c := FISH::create(x,y) @ ocean.cluster_id_of(x,y); fish.insert(x,y,c); num_fish := num_fish+1; elsif (random.uniform < shark_density) then c := SHARK::create(x,y) @ ocean.cluster_id_of(x,y); sharks.insert(x,y,c); num_sharks := num_sharks+1; end; -- if if (c /= void) then ocean.insert(x,y,c) end; x := x+1; end; -- loop y := y+1; end; -- loop if (display) then display_grids end; OUT::start; OUT::s("Number of sharks = ").i(num_sharks).nl; OUT::s("Number of fish = ").i(num_fish).nl; OUT::s("Shark breeding age = ").i(SHARK::breed_age).nl; OUT::s("Fish breeding age = ").i(FISH::breed_age).nl; OUT::stop; end; -- setup_sharks_fish compute is -- Evalute over given number of iterations. OUT::start; OUT::s("Ocean size = ").i(ocean_height).s(" X ").i(ocean_width).nl; OUT::s("Number of iterations = ").i(num_iters).nl; OUT::stop; curr_iter:=0; until (curr_iter >= num_iters) loop sharks.eat_and_move(fish); fish.move_from(sharks); if (display) then display_grids end; curr_iter := curr_iter+1; end; -- loop end; -- compute display_grids is -- Display given grid. OUT::start; OUT::s("\nCurrent ocean: ").i(curr_iter).nl; h:INT:=ocean_height; w:INT:=ocean_width; j:INT; until (j >= h) loop i:INT; until (i >= w) loop if (fish.get(i,j) /= void) then if (ocean.get(i,j) /= void) then OUT::s("F "); else OUT::s("1 "); -- Inconsistency. end; -- if elsif (sharks.get(i,j) /= void) then if (ocean.get(i,j) /= void) then OUT::s("S "); else OUT::s("2 "); -- Inconsistency. end; -- if else if (ocean.get(i,j) = void) then OUT::s(". "); else OUT::s("3 "); -- Inconsistency. end; -- if end; -- if i:=i+1; end; -- loop OUT::nl; j:=j+1; end; -- loop OUT::stop; end; -- display_grids end; -- class SF --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~