Advertisement
Guest User

ex1search

a guest
Nov 27th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.47 KB | None | 0 0
  1. package lecture9.exercises.cp.search
  2.  
  3.  
  4.  
  5.  
  6. import oscar.cp.modeling._
  7. import oscar.cp.core._
  8. import oscar.algo.reversible.ReversibleInt
  9.  
  10. /**
  11.  * @author Pierre Schaus pschaus@gmail.com
  12.  */
  13. object Exercise01 extends App {
  14.  
  15.   implicit val cp = CPSolver()
  16.  
  17.   val a = Array.fill(20)(0)
  18.  
  19.   val i = new ReversibleInt(cp,0)
  20.  
  21.   var nbSol = 0
  22.  
  23.   onSolution {
  24.     //println(a.mkString(","))
  25.     nbSol += 1
  26.   }
  27.  
  28.   search {
  29.     if (i >= a.size) noAlternative
  30.     else branch {
  31.       //left
  32.       if(a.sum >= 3)
  33.           cp.fail()
  34.       else
  35.         a(i) = 1
  36.       i += 1
  37.     } {
  38.       //right
  39.       a(i) = 0
  40.       i += 1
  41.     }
  42.   }
  43.   start()
  44.   print(nbSol)
  45.  
  46. }
  47.  
  48. /*
  49.  
  50. Hint: Doc of most of OscaR constraints are available here:
  51.       https://jenkins.info.ucl.ac.be:8080/job/oscar-dev/javadoc/?#oscar.cp.modeling.Constraints
  52.       A good Scala cheat sheet available here:
  53.       http://docs.scala-lang.org/cheatsheets/
  54.  
  55. 1) run this program
  56.  
  57. 2) modify it to print all possible 0/1 assignments on a
  58.  
  59. 3) modify it to print the solution in this order
  60.  
  61. 1,1,1
  62. 1,1,0
  63. 1,0,1
  64. 1,0,0
  65.  
  66. 4) Increase the size of a to 20 and count the number of solution
  67.  
  68. 5) cutoff branches where there  are at more than three 1's, how many solutions?
  69.    Hint: use cp.fail() in a branch:
  70.  
  71.          cp.branch {
  72.            left branch
  73.            if (condition) cp.fail()
  74.            else ...
  75.          } {
  76.            right branch
  77.          }
  78.  
  79.    How many solutions are there?
  80. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement