00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00061 for (int i = 0; i<spec.n_prefs(); i++) {
00062
00063 int pa = spec.prefs[2*i+0];
00064 int pb = spec.prefs[2*i+1];
00065
00066 BoolVar b0 = new BoolVar(this);
00067
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
00084 bool_xor(this, b0,b1,ful.get(i));
00085 }
00086
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
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 }