//************************************************************ // Main Implementation of SAT-TPI Game from // Tech. Report ULCS-02-008: //************************************************************ // Projects 2-5 will require modification/interacting with // the implementation given below. //************************************************************ // Paul E. Dunne // Dept. of Computer Science // Univ. of Liverpool //************************************************************ // Version 1: 20-22/05/2002; // Version 2: 23-24//05/2002: Retracted sets held in Stack; // Modification of output GamePosn string. // Version 3: 27/05/2002: Input file argument; // GameRecord comment out. // default holds current and last position; // Version 4: 28/05/2002: Input flags processing. // Version 5: 29/05/2002: Minor changes to command line processing. // Version 6: 30/05/2002: Minor changes to SelectAttack,Defence param. // Version 7: 31/05/2002: Option to print satisfying assignment. //************************************************************ // Summary of command line options: // // java SAT_TPI [-c out_file] [-f n p] [-i in_file] [-n out_file] // [-o out_file] [-p ] [-r n m p] [-s seed] [-S -V] // // All switches are optional. Input/Output defaults to Standard if // none specified. // // -c Save CNF used in out_file (for use with -r and -f options) // -f Generate random 3-CNF formula of n variables; clause // probability p. (Overrides -i and -r) // -i Read CNF formula from file specified by in_file. // -n Save CNF in out_file using ntab format for CNF (for use with -r and -f options). // -o Send output to out_file. // -p Print SAT instantiation if instance satisfiable. // -r Generate random CNF formula of n variables, // m clauses, // probability p. // (Overrides -i) // -s Specify random seed. // // -S Generate statistics only: (Result/Number of Moves/Number of retractions) // (Default: terse output (retractions only and outcome). // -V Generate verbose output (full listing of game progress); // //************************************************************ import java.io.*; import CNF_Formula; import GamePosn; import java.util.*; import BoolString; import PED_NumberIO; //************************************************************ // This line should be included if full Game progress is required // in single data structure. Similalrly all commented lines of // code marked (++++) in main(). //************************************************************ // import GameRecord; (++++) //************************************************************ public class SAT_TPI { public static FileInputStream fname; public static InputStreamReader input; public static BufferedReader istream; public static FileWriter res_file; public static FileWriter cnf_file; public static PrintWriter ostream1; public static PrintWriter ostream2; //************************************************************ static int Ags; // Number of arguments = 2n+m+1 static int n; // Variable count static int m; // Clause count static int PHI; // Argument corresponding to F. static boolean playable=true; // Game halts when playable becomes false; static boolean C_play = false; // Flag to record active player. static int MoveCount=0; // Number of moves made so far; static int LastAttack; // Index of Last Clause (1..m) used. static int lit; static final char Assert='A'; static final char Counter='C'; static final char Retract='R'; //******************************************************************* // Supplementary methods used in main(): //******************************************************************* // a) Find_Clauses( GamePosn G, CNF_Formula F, int n, int m) // Identifies the clauses which can be used to attack F given // given the Game progress so far. // returns boolean[] //******************************************************************* // b) SubSetEq ( boolean[] R, boolean[] S, int y) // returns true if the set indicated by R/{y} is a subset or equal // to the set indicated by S. //******************************************************************* // c) CheckRetraction (Stack R, boolean[] P, int y, int Ag) // returns true if y can be added to set indicated in P without // committing to a set which has been retracted earlier. //******************************************************************* // d) Find_Literals( GamePosn G, CNF_Formula F, Stack X, int k, int n, int m) // Identifies the set of literals which can be used to attack // k'th clause, in the context of Game so far. // returns boolean[] //******************************************************************* // e) Select_Attack( boolean[] Possible, CNF_Formula F, GamePosn G) // Given an indication of the available clauses (identified in (a)) // choose 1 of these to continue attack. // // NOTE: Current implementation is `naive': one projects involves // investigating more sophisticated selection methods, and thence // requires modifying this method. //******************************************************************* // f) Select_Defence( boolean[] Possible, CNF_Formula F, i GamePosn G) // Given an indication of the available literal (identified in (d)) // choose 1 of these to continue defence. // // NOTE: Current implementation is `naive': one projects involves // investigating more sophisticated selection methods, and thence // requires modifying this method. //******************************************************************* // //******************************************************************* // Return the set of clauses that are (currently) available to // Challenger with which to attack. // if res[k]=true (1<=k<=m) then Ck can be used; // if res[0]=false then no avaialble clauses an Challenger has lost //******************************************************************* public static boolean[] Find_Clauses(GamePosn G, CNF_Formula F, int n, int m) { boolean[] res = new boolean[m+1]; boolean[] CA = new boolean[2*n+m+2]; boolean[] CD = new boolean[2*n+m+2]; int[] clause; boolean flag = false; boolean is_ok = true; CA = G.GetAttack(); CD = G.GetDefence(); for (int k=1; k