Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lecture9.exercises.cp.search
- import oscar.cp.modeling._
- import oscar.cp.core._
- import oscar.algo.reversible.ReversibleInt
- /**
- * @author Pierre Schaus pschaus@gmail.com
- */
- object Exercise01 extends App {
- implicit val cp = CPSolver()
- val a = Array.fill(20)(0)
- val i = new ReversibleInt(cp,0)
- var nbSol = 0
- onSolution {
- //println(a.mkString(","))
- nbSol += 1
- }
- search {
- if (i >= a.size) noAlternative
- else branch {
- //left
- if(a.sum >= 3)
- cp.fail()
- else
- a(i) = 1
- i += 1
- } {
- //right
- a(i) = 0
- i += 1
- }
- }
- start()
- print(nbSol)
- }
- /*
- Hint: Doc of most of OscaR constraints are available here:
- https://jenkins.info.ucl.ac.be:8080/job/oscar-dev/javadoc/?#oscar.cp.modeling.Constraints
- A good Scala cheat sheet available here:
- http://docs.scala-lang.org/cheatsheets/
- 1) run this program
- 2) modify it to print all possible 0/1 assignments on a
- 3) modify it to print the solution in this order
- 1,1,1
- 1,1,0
- 1,0,1
- 1,0,0
- 4) Increase the size of a to 20 and count the number of solution
- 5) cutoff branches where there are at more than three 1's, how many solutions?
- Hint: use cp.fail() in a branch:
- cp.branch {
- left branch
- if (condition) cp.fail()
- else ...
- } {
- right branch
- }
- How many solutions are there?
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement