// // construct and solve various formulations // private static void solve_c_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si, double Cp, double Cn) { int l = prob.l; double[] minus_ones = new double[l]; byte[] y = new byte[l]; int i; for(i=0;i<l;i++) { alpha[i] = 0; minus_ones[i] = -1; if(prob.y[i] > 0) y[i] = +1; else y[i]=-1; } Solver s = new Solver(); s.Solve(l, new SVC_Q(prob,param,y), minus_ones, y, alpha, Cp, Cn, param.eps, si, param.shrinking); double sum_alpha=0; for(i=0;i<l;i++) sum_alpha += alpha[i]; System.out.print("nu = "+sum_alpha/(param.C*prob.l)+"\n"); for(i=0;i<l;i++) alpha[i] *= y[i]; }
@Test public void testNotExistsArrayConjunct() throws SolverException, InterruptedException { BooleanFormula f; // (not exists x . not b[x] = 0) AND (b[123] = 1) is UNSAT f = bfm.and( Lists.newArrayList( bfm.not(qfm.exists(_x, bfm.not(_b_at_x_eq_0))), ifm.equal(afm.select(_b, ifm.makeNumber(123)), ifm.makeNumber(1)))); assertThat(solver.isUnsat(f)).isTrue(); // (not exists x . not b[x] = 0) AND (b[123] = 0) is SAT f = bfm.and( bfm.not(qfm.exists(_x, bfm.not(_b_at_x_eq_0))), ifm.equal(afm.select(_b, ifm.makeNumber(123)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isFalse(); // (not exists x . b[x] = 0) AND (b[123] = 0) is UNSAT f = bfm.and( bfm.not(qfm.exists(_x, _b_at_x_eq_0)), ifm.equal(afm.select(_b, ifm.makeNumber(123)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isTrue(); }
private static void solve_epsilon_svr(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si) { int l = prob.l; double[] alpha2 = new double[2*l]; double[] linear_term = new double[2*l]; byte[] y = new byte[2*l]; int i; for(i=0;i<l;i++) { alpha2[i] = 0; linear_term[i] = param.p - prob.y[i]; y[i] = 1; alpha2[i+l] = 0; linear_term[i+l] = param.p + prob.y[i]; y[i+l] = -1; } Solver s = new Solver(); s.Solve(2*l, new SVR_Q(prob,param), linear_term, y, alpha2, param.C, param.C, param.eps, si, param.shrinking); double sum_alpha = 0; for(i=0;i<l;i++) { alpha[i] = alpha2[i] - alpha2[i+l]; sum_alpha += Math.abs(alpha[i]); } System.out.print("nu = "+sum_alpha/(param.C*l)+"\n"); }
@Test public void testExistsRestrictedRange() throws SolverException, InterruptedException { BooleanFormula f; BooleanFormula _exists_10_20_bx_0 = qfm.exists(_x, ifm.makeNumber(10), ifm.makeNumber(20), _b_at_x_eq_0); BooleanFormula _exists_10_20_bx_1 = qfm.exists(_x, ifm.makeNumber(10), ifm.makeNumber(20), _b_at_x_eq_1); // (exists x in [10..20] . b[x] = 0) AND (forall x . b[x] = 0) is SAT f = bfm.and(_exists_10_20_bx_0, _forall_x_bx_0); assertThat(solver.isUnsat(f)).isFalse(); // (exists x in [10..20] . b[x] = 1) AND (forall x . b[x] = 0) is UNSAT f = bfm.and(_exists_10_20_bx_1, _forall_x_bx_0); assertThat(solver.isUnsat(f)).isTrue(); // (exists x in [10..20] . b[x] = 1) AND (forall x . b[x] = 1) is SAT f = bfm.and(_exists_10_20_bx_1, _forall_x_bx_1); assertThat(solver.isUnsat(f)).isFalse(); // (exists x in [10..20] . b[x] = 1) AND (b[10] = 0) is SAT f = bfm.and( _exists_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(10)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isFalse(); // (exists x in [10..20] . b[x] = 1) AND (b[1000] = 0) is SAT f = bfm.and( _exists_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(1000)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isFalse(); }
public static void main(String[] args) { // create initial board from file In in = new In(args[0]); int N = in.readInt(); int[][] tiles = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { tiles[i][j] = in.readInt(); } Board initial = new Board(tiles); /* System.out.println(initial.isSolvable()); System.out.println(initial.toString()); Iterable<Board> newbrdstack = new Stack<Board>(); newbrdstack = initial.neighbors(); System.out.println(newbrdstack.toString()); */ // check if puzzle is solvable; if so, solve it and output solution if (initial.isSolvable()) { Solver solver = new Solver(initial); StdOut.println("Minimum number of moves = " + solver.moves()); for (Board board : solver.solution()) StdOut.println(board); } // if not, report unsolvable else { StdOut.println("Unsolvable puzzle"); } }
@Test public void testContextCreation() { for (int i = 0; i < 1000; i++) { Solver solver = Solver.createSolver(); solver.isSatisfiable(); } }
private static void solve_one_class(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si) { int l = prob.l; double[] zeros = new double[l]; byte[] ones = new byte[l]; int i; int n = (int)(param.nu*prob.l); // # of alpha's at upper bound if(n>=prob.l) { System.err.print("nu must be in (0,1)\n"); System.exit(1); } for(i=0;i<n;i++) alpha[i] = 1; alpha[n] = param.nu * prob.l - n; for(i=n+1;i<l;i++) alpha[i] = 0; for(i=0;i<l;i++) { zeros[i] = 0; ones[i] = 1; } Solver s = new Solver(); s.Solve(l, new ONE_CLASS_Q(prob,param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking); }
public static void main(String[] args) throws Exception { Solver problem = new Solver(GameState.INITIAL_BOARD); File outFile = new File("output.txt"); PrintWriter output = new PrintWriter(outFile); problem.solve(output); output.close(); }
public Character next() { if (left.hasNext()) { return left.next(); } else if (right.hasNext()) { return right.next(); } throw new NoSuchElementException(); }
public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(1, in, out); out.close(); }
private void doTest() { // 3+5+9=17 assertTrue(solver.solve(new int[] {3, 5, 7, 9, 11}, 17)); // Lukt niet assertFalse(solver.solve(new int[] {2, 4}, 5)); // E�n te weinig assertFalse(solver.solve(new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 31)); // Precies goed assertTrue(solver.solve(new int[] {1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 31)); }
public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); Solver solver = new Solver(in, out); solver.solve(); in.close(); out.flush(); out.close(); }
@Test public void testContradiction() throws SolverException, InterruptedException { // forall x . x = x+1 is UNSAT BooleanFormula f = qfm.forall(_x, ifm.equal(_x, ifm.add(_x, ifm.makeNumber(1)))); assertThat(solver.isUnsat(f)).isTrue(); BooleanFormula g = qfm.exists(_x, ifm.equal(_x, ifm.add(_x, ifm.makeNumber(1)))); assertThat(solver.isUnsat(g)).isTrue(); }
public static void main(String[] args) { try { Solver solver = new Solver(); solver.setup(args); solver.solve(); solver.writeOutput(); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testExistsArrayDisjunct() throws SolverException, InterruptedException { BooleanFormula f; // (exists x . b[x] = 0) OR (forall x . b[x] = 1) is SAT f = bfm.or(qfm.exists(_x, _b_at_x_eq_0), qfm.forall(_x, _b_at_x_eq_1)); assertThat(solver.isUnsat(f)).isFalse(); // (exists x . b[x] = 1) OR (exists x . b[x] = 1) is SAT f = bfm.or(qfm.exists(_x, _b_at_x_eq_1), qfm.exists(_x, _b_at_x_eq_1)); assertThat(solver.isUnsat(f)).isFalse(); }
@Test public void solve3x4() { final Puzzle puzzle = new Puzzle(3, 4).set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0); final Solver solver = new AStarSolver(); final Solution solution = solver.solve(puzzle); assertNotNull(solution); assertEquals(27, solution.steps()); System.out.printf( " A*, 3x4, %2s steps, %9s expansions\n", solution.steps(), solver.expansions()); }
@Override protected void visit(RENode.CharacterClass expr) throws RuntimeException { expr.getExpr().accept(this); for (int i = expr.getMin(); i > 0; i--) { if (solver.hasNext()) { char c = solver.next(); current.resolvingExpression.append(c); current.buffer.append(c); solver.reset(); } else { throw new UnsupportedOperationException("wtf?"); } } }
@Test public void solve3x3Hardest() { final Puzzle puzzle = new Puzzle(3).set(8, 7, 6, 0, 4, 1, 2, 5, 3); final Solver solver = new AStarSolver(); solver.setHeuristic(WalkingDistanceHeuristic.instance()); final Solution solution = solver.solve(puzzle); assertNotNull(solution); assertEquals(31, solution.steps()); System.out.printf( " A*, 3x3, %2s steps, %9s expansions\n", solution.steps(), solver.expansions()); }
@Test public void solve4x4() { final Puzzle puzzle = new Puzzle(4, 4).shuffle(500, 0); final Solver solver = new AStarSolver(); solver.setHeuristic(WalkingDistanceHeuristic.instance()); final Solution solution = solver.solve(puzzle); assertNotNull(solution); assertEquals(46, solution.steps()); System.out.printf( " A*, 4x4, %2s steps, %9s expansions\n", solution.steps(), solver.expansions()); }
@Test public void solve5x3() { final Puzzle puzzle = new Puzzle(5, 3).set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0); final Solver solver = new AStarSolver(); solver.setHeuristic(WalkingDistanceHeuristic.instance()); final Solution solution = solver.solve(puzzle); assertNotNull(solution); assertEquals(42, solution.steps()); System.out.printf( " A*, 5x3, %2s steps, %9s expansions\n", solution.steps(), solver.expansions()); }
// method to load and start a saved game, called by the "Ladda spel" button" private void runSavedGame() { if (!mGameInAction) // can only be done if no game is active { abortGame(); Game game = mLoader.loadGame("savedgames"); // set the folder where the game is to be stored if (game != null) { mGUI.getBoard().setButtons(game.getButtons()); mGUI.getTimePanel() .getDisplay() .setTime(game.getHours(), game.getMinutes(), game.getSeconds()); mSolved = false; mGUI.getBoard().boardUpdate(); extractLockedValues(); // important only to read buttons from original game try { mSolver.run(mCurrentValues); } catch (Exception ex) { // No need to do anything, game is already checked } getSolution(); extractValuesFromBoard(); // now get all values to get current board mGUI.getTimePanel().start(); mGameInAction = true; mLoader.nullGame(); } } }
public TestSolver(String fileName) { File file = new File(fileName); String row; Scanner s; Map map; int cols, rows, rowLength = 0; try { s = new Scanner(file).useDelimiter("\n"); rows = Integer.parseInt(s.next()); cols = Integer.parseInt(s.next()); map = new Map(rows, cols); for (int i = 0; i < rows; i++) { row = s.next(); System.out.println(row); rowLength = row.length(); if (rowLength < cols) { for (int j = 0; j < (cols - rowLength); j++) { row += " "; } } map.insertRow(row, i); } solver = new Solver(map); System.out.println(solver.solve()); } catch (FileNotFoundException e) { System.out.println("File not found."); } }
// helpmethod to startGame // gets the solution for comparison with current board private void getSolution() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { mSolutionValues[i][j] = mSolver.getSolution(i, j); } } }
// save a new unplayed game called by "Lägg till nytt spel button" private void saveNewGame() { if (!mGameInAction) // can only be done if no game is active { extractValuesFromBoard(); if (mSolver.checkNewGame(mCurrentValues)) { mGUI.getBoard().lockShowingNumbers(); // set button showing number to locked String newName = JOptionPane.showInputDialog( "Vad vill du döpa spelet till?"); // get name of the game from user if (!"".equals(newName) && newName != null) // dont do anything unless user names game or if press cancel { // save the game mSaver.saveGame( "newgames/", newName, new Game( mGUI.getBoard().getButtons(), mGUI.getTimePanel().getHours(), mGUI.getTimePanel().getMinutes(), mGUI.getTimePanel().getSeconds())); clearBoard(); // clear the board } else if (newName.equals("")) { JOptionPane.showMessageDialog( null, "Du måste namnge det nya spelet!", "Namnge spelet!", JOptionPane.PLAIN_MESSAGE); } } else { JOptionPane.showMessageDialog( null, "Du har försökt spara ett olösligt sudoku!", "HOPPSAN!!!", JOptionPane.PLAIN_MESSAGE); } } }
void Solve(int l, Kernel Q, double[] b, byte[] y, double[] alpha, double Cp, double Cn, double eps, SolutionInfo si, int shrinking) { this.si = si; super.Solve(l,Q,b,y,alpha,Cp,Cn,eps,si,shrinking); }
@Test public void solve2x2() { final Puzzle puzzle = new Puzzle(2, 2) .set( 2, 0, 3, 1); final Solver solver = new AStarSolver(); final Solution solution = solver.solve(puzzle); assertNotNull(solution); assertEquals(3, solution.steps()); System.out.printf( " A*, 2x2, %2s steps, %9s expansions\n", solution.steps(), solver.expansions()); }
public static void main(String[] args) { try { // Parse input and set up ProjectParams pp = ProjectParams.fromStdIn(); // System.out.println(pp.getTeams()); // System.out.println(pp); Solver solver = new Solver(pp); // System.out.println(solver + "\n"); // Solve and print solver.solve(); solver.printSolution(); } catch (IOException e) { System.err.println("Error reading data from stdin\n"); e.printStackTrace(); } }
/** * solve a slider puzzle (given below) * * @param args */ public static void main(String[] args) { // create initial board from file In in = new In(args[0]); int N = in.readInt(); int[][] blocks = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) blocks[i][j] = in.readInt(); Board initial = new Board(blocks); // solve the puzzle Solver solver = new Solver(initial); // print solution to standard output if (!solver.isSolvable()) StdOut.println("No solution possible"); else { StdOut.println("Minimum number of moves = " + solver.moves()); for (Board board : solver.solution()) StdOut.println(board); } }
@Test public void testForallRestrictedRange() throws SolverException, InterruptedException { BooleanFormula f; BooleanFormula _forall_10_20_bx_0 = qfm.forall(_x, ifm.makeNumber(10), ifm.makeNumber(20), _b_at_x_eq_0); BooleanFormula _forall_10_20_bx_1 = qfm.forall(_x, ifm.makeNumber(10), ifm.makeNumber(20), _b_at_x_eq_1); // (forall x in [10..20] . b[x] = 0) AND (forall x . b[x] = 0) is SAT f = bfm.and(_forall_10_20_bx_0, qfm.forall(_x, _b_at_x_eq_0)); assert_().about(BooleanFormula()).that(f).isSatisfiable(); // (forall x in [10..20] . b[x] = 1) AND (exits x in [15..17] . b[x] = 0) is UNSAT f = bfm.and( _forall_10_20_bx_1, qfm.exists(_x, ifm.makeNumber(15), ifm.makeNumber(17), _b_at_x_eq_0)); assertThat(solver.isUnsat(f)).isTrue(); // (forall x in [10..20] . b[x] = 1) AND b[10] = 0 is UNSAT f = bfm.and( _forall_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(10)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isTrue(); // (forall x in [10..20] . b[x] = 1) AND b[20] = 0 is UNSAT f = bfm.and( _forall_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(20)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isTrue(); // (forall x in [10..20] . b[x] = 1) AND b[9] = 0 is SAT f = bfm.and( _forall_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(9)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isFalse(); // (forall x in [10..20] . b[x] = 1) AND b[21] = 0 is SAT f = bfm.and( _forall_10_20_bx_1, ifm.equal(afm.select(_b, ifm.makeNumber(21)), ifm.makeNumber(0))); assertThat(solver.isUnsat(f)).isFalse(); // (forall x in [10..20] . b[x] = 1) AND (forall x in [0..20] . b[x] = 0) is UNSAT f = bfm.and( _forall_10_20_bx_1, qfm.forall(_x, ifm.makeNumber(0), ifm.makeNumber(20), _b_at_x_eq_0)); assertThat(solver.isUnsat(f)).isTrue(); // (forall x in [10..20] . b[x] = 1) AND (forall x in [0..9] . b[x] = 0) is SAT f = bfm.and( _forall_10_20_bx_1, qfm.forall(_x, ifm.makeNumber(0), ifm.makeNumber(9), _b_at_x_eq_0)); assertThat(solver.isUnsat(f)).isFalse(); }
@Test public void testExistsArrayConjunct() throws SolverException, InterruptedException { BooleanFormula f; // (exists x . b[x] = 0) AND (b[123] = 1) is SAT f = bfm.and( qfm.exists(_x, _b_at_x_eq_0), ifm.equal(afm.select(_b, ifm.makeNumber(123)), ifm.makeNumber(1))); assertThat(solver.isUnsat(f)).isFalse(); // (exists x . b[x] = 1) AND (forall x . b[x] = 0) is UNSAT f = bfm.and(qfm.exists(_x, _b_at_x_eq_1), _forall_x_bx_0); assertThat(solver.isUnsat(f)).isTrue(); // (exists x . b[x] = 0) AND (forall x . b[x] = 0) is SAT f = bfm.and(qfm.exists(_x, _b_at_x_eq_0), _forall_x_bx_0); assertThat(solver.isUnsat(f)).isFalse(); }