Generated on Fri Oct 6 16:26:42 2006 for Gecode/J by doxygen 1.4.7

Photo.java

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *     Guido Tack, 2006
00009  *
00010  *  Last modified:
00011  *     $Date: 2006-09-26 16:26:14 +0200 (Tue, 26 Sep 2006) $ by $Author: zayenz $
00012  *     $Revision: 3704 $
00013  *
00014  *  This file is part of Gecode, the generic constraint
00015  *  development environment:
00016  *     http://www.gecode.org
00017  *
00018  *  See the file "LICENSE" for information on usage and
00019  *  redistribution of this file, and for a
00020  *     DISCLAIMER OF ALL WARRANTIES.
00021  *
00022  */
00023 
00024 package examples;
00025 
00026 import static org.gecode.Gecode.*;
00027 import static org.gecode.GecodeEnumConstants.*;
00028 
00029 import org.gecode.*;
00030 import org.gecode.explorer.Explorer;
00031 
00041 public class Photo extends Space {
00042 
00043     public VarArray<IntVar> pos;
00044     public IntVar sat;
00045     private PhotoSpec spec;
00046 
00047     public void constrain(Space oldSol) {
00048         rel(this, sat, IRT_GR, ((Photo)oldSol).sat.val(), ICL_DEF);
00049     }
00050 
00051 
00052     public Photo(PhotoSpec s) {
00053         super();
00054         spec = s;
00055         pos  = new VarArray<IntVar>(this, s.n_names(), IntVar.class, 0, spec.n_names()-1);
00056         sat  = new IntVar(this, new IntSet(0, spec.n_prefs()));
00057 
00058         VarArray<BoolVar> ful = new VarArray<BoolVar>(this, spec.n_prefs(), BoolVar.class);
00059 
00060         // Map preferences to fulfilment
00061         for (int i = 0; i<spec.n_prefs(); i++) {
00062             // Preferences
00063             int pa = spec.prefs[2*i+0];
00064             int pb = spec.prefs[2*i+1];
00065             // True iff pos(pb)-pos(pa)==1
00066             BoolVar b0 = new BoolVar(this);
00067             // True iff pos(pa)-pos(pb)==1
00068             BoolVar b1 = new BoolVar(this);            
00069 
00070             int coeffs[] = {1,-1};
00071             VarArray<IntVar> args1 = 
00072                 new VarArray<IntVar>(pos.get(pb),
00073                                      pos.get(pa));
00074 
00075             VarArray<IntVar> args2 = 
00076                 new VarArray<IntVar>(pos.get(pa),
00077                                      pos.get(pb));
00078 
00079             linear(this, coeffs,args1,
00080                    IRT_EQ, 1, b0, ICL_DEF);
00081             linear(this, coeffs,args2,
00082                           IRT_EQ, 1, b1, ICL_DEF);
00083             // ful.get(i) true iff |pos(pa)-pos(pb)|==1
00084             bool_xor(this, b0,b1,ful.get(i));
00085         }
00086         // Sum of fulfilment
00087         {
00088             VarArray<IntVar> eq = new VarArray<IntVar>(ful);
00089             int c[]= new int[spec.n_prefs()+1];
00090             for (int i = 0; i<spec.n_prefs(); i++ ) {
00091                 c[i] = 1;
00092             }
00093             eq.add(spec.n_prefs(), sat); c[spec.n_prefs()] = -1;
00094             linear(this, c, eq, IRT_EQ, 0, ICL_DEF);
00095         }
00096         distinct(this, pos, ICL_DEF);
00097         rel(this, pos.get(0), IRT_LE, pos.get(1), ICL_DEF);
00098         branch(this, pos, BVAR_NONE, BVAL_MIN);
00099     }
00100 
00101     public Photo(Boolean share, Photo photo) {
00102         super(share, photo);
00103 
00104         spec = photo.spec;
00105         
00106         pos = new VarArray<IntVar>(this, share, photo.pos);
00107         sat = photo.sat.copy(this, share);
00108     }
00109 
00110     public static void main(String []args) {
00111         Options opt = new Options("Photo");
00112         opt.size = 0;
00113         opt.bab = true;
00114         opt.gui = true;
00115         opt.solutions = 0;
00116         opt.parse(args, specs.length);
00117 
00118         Photo p = new Photo(specs[opt.size]);   
00119         opt.doSearch(p);
00120     }
00121 
00122 
00123 
00124     // Specifications
00125     private static PhotoSpec[] specs;
00126     static {
00127         specs = new PhotoSpec[2];
00128         specs[0] = new Small();
00129         specs[1] = new Large();
00130     }
00131 
00132     static class PhotoSpec {
00133         public int n_names() {
00134             int n = -1;
00135             for (int i: prefs) n = Math.max(n, i);
00136             return n+1;
00137         }
00138         public int n_prefs() {
00139             return prefs.length / 2;
00140         }
00141         public int prefs[];
00142     }
00143     
00144     static class Small extends PhotoSpec {
00145         public Small() {
00146             int tmp[] = {
00147                 0,2, 1,4, 2,3, 2,4, 3,0, 4,3, 4,0, 4,1
00148             };
00149             prefs = tmp;
00150         }
00151     }
00152     
00153     static class Large extends PhotoSpec {
00154         public Large() {
00155             int tmp[] = {
00156                 0,2, 0,4, 0,7, 1,4, 1,8, 2,3, 2,4, 3,0, 3,4, 
00157                 4,5, 4,0, 5,0, 5,8, 6,2, 6,7, 7,8, 7,6
00158             };
00159             prefs = tmp;
00160         }
00161     }
00162 }