00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package examples;
00026
00027 import static org.gecode.Gecode.*;
00028 import static org.gecode.GecodeEnumConstants.*;
00029
00030 import org.gecode.*;
00031
00032 import java.util.ArrayList;
00033
00038 class Nq extends BinaryPropagator<IntVarView> {
00039 public Nq(Space s, IntVarView x0, IntVarView y0) {
00040 super(s, x0, y0, PC_INT_VAL);
00041 }
00042 public Nq(Space s, Boolean share, Nq p) {
00043 super(s, share, p);
00044 }
00045 public PropCost cost() {
00046 return PC_BINARY_HI;
00047 }
00048 public ExecStatus setup(Space home) {
00049 if (x.assigned() && y.assigned() && x.min()==y.min())
00050 return ES_FAILED;
00051 return super.setup(home);
00052 }
00053 public ExecStatus propagate(Space home) {
00054 if (x.assigned()) {
00055 return t(y.nq(home, x.min()), ES_SUBSUMED);
00056 } else {
00057 return t(x.nq(home, y.min()), ES_SUBSUMED);
00058 }
00059 }
00060
00061 static void post(Space home, IntVar x, IntVar y) {
00062 Gecode.addPropagator(home,
00063 new Nq(home,
00064 new IntVarView(home, x),
00065 new IntVarView(home, y)));
00066 }
00067 }
00068
00074 class Distinct<IV extends IntView> extends NaryPropagator<IV> {
00075 private int n;
00076
00077 public Distinct(Space s, ViewArray<IV> iv0) {
00078 super(s, iv0, PC_INT_VAL);
00079 n = iv.size()-1;
00080 }
00081 public Distinct(Space s, Boolean share,
00082 Distinct<IV> p) {
00083 super(s, share, p);
00084 n = p.n;
00085 }
00086 public PropCost cost() {
00087 return cost_hi(n, PC_LINEAR_HI);
00088 }
00089 public ExecStatus propagate(Space home) {
00090 for (int i=0; i<=n; i++) {
00091 if (iv.get(i).assigned()) {
00092 int v = iv.get(i).val();
00093 for (int j=0; j<i; j++) {
00094 if (iv.get(j).nq(home, v).failed())
00095 return ES_FAILED;
00096 }
00097 for (int j=i+1; j<=n; j++) {
00098 if (iv.get(j).nq(home, v).failed())
00099 return ES_FAILED;
00100 }
00101 iv.swap(i, n);
00102 n--;
00103 i--;
00104 }
00105 }
00106 return ES_NOFIX;
00107 }
00108
00111 static void post(Space home, VarArray<IntVar> x) {
00112 ViewArray<IntVarView> xa = new ViewArray<IntVarView>(home, IntVarView.class, x);
00113
00114 Gecode.addPropagator(home,
00115 new Distinct<IntVarView>(home, xa));
00116 }
00117
00120 static void post(Space home, int[] off, VarArray<IntVar> x) {
00121 ViewArray<OffsetView<IntVarView>> xa =
00122 new ViewArray<OffsetView<IntVarView>>(x.size());
00123 for (int i=0; i<x.size(); i++)
00124 xa.add(new OffsetView<IntVarView>
00125 (home, off[i], new IntVarView(home, x.get(i))));
00126
00127 Gecode.addPropagator(home,
00128 new Distinct<OffsetView<IntVarView>>(home,
00129 xa));
00130
00131 }
00132 }
00133
00140 class Naive extends Branching {
00141 private ViewArray<IntVarView> x;
00142
00143 private ExecStatus t(IntModEvent ime) {
00144 return ime.failed()
00145 ? ES_FAILED : ES_OK;
00146 }
00147
00148 public Naive(Space home, VarArray<IntVar> x0) {
00149 x = new ViewArray<IntVarView>(home, IntVarView.class, x0);
00150 }
00151
00152 private Naive(Space home, Boolean share, Naive n) {
00153 x = new ViewArray<IntVarView>(home, share, n.x);
00154 }
00155
00156 public boolean status(Space home) {
00157 return (!x.assigned());
00158 }
00159
00160 public JavaBranchingDesc description(Space home) {
00161 int index = 0;
00162 for (IntVarView v: x) {
00163 if (!v.assigned()) {
00164 return new JavaBranchingDesc(index, v.min());
00165 }
00166 index++;
00167 }
00168 return null;
00169 }
00170
00171 public ExecStatus commit(Space home, JavaBranchingDesc d, long a) {
00172 if (a==0) {
00173 return t(x.get(d.pos()).eq(home, d.val()));
00174 } else {
00175 return t(x.get(d.pos()).nq(home, d.val()));
00176 }
00177 }
00178
00179 public static void post(Space home, VarArray<IntVar> x) {
00180 Gecode.addBranching(home, new Naive(home, x));
00181 }
00182 }
00183
00191 public class QueensJavaPropagator extends Space {
00192 private int n;
00193 private VarArray<IntVar> q;
00194
00195 public QueensJavaPropagator(int size) {
00196 super();
00197 n = size;
00198 q = new VarArray<IntVar>(this, size, IntVar.class, 0, n-1);
00199
00200 int c[] = new int[n];
00201
00202 for (int i=0; i<n; i++)
00203 c[i] = i;
00204 Distinct.post(this, c, q);
00205
00206 for (int i=0; i<n; i++)
00207 c[i] = -i;
00208 Distinct.post(this, c, q);
00209
00210 Distinct.post(this, q);
00211
00212 Naive.post(this, q);
00213 }
00214
00215 public QueensJavaPropagator(Boolean share, QueensJavaPropagator queens) {
00216 super(share, queens);
00217 n = queens.n;
00218 q = new VarArray<IntVar>(this, share, queens.q);
00219 }
00220
00221 public String toString() {
00222 String res = "";
00223
00224 for (int i = 0; i < n; ++i) {
00225 char[] l = new char[n];
00226 for (int j=0;j<n;++j) l[j] = '\u00B7';
00227
00228 if (q.get(i).assigned()) {
00229 l[q.get(i).val()] = 'Q';
00230 } else {
00231 for (Range r : new IntVarRanges(q.get(i)))
00232 for (int j : r)
00233 l[j] = 'q';
00234 }
00235 res += new String(l);
00236 res += "\n";
00237 }
00238 return res;
00239 }
00240
00241 public static void main(String[] args) {
00242 Options opt = new Options("Queens");
00243 opt.size = 6;
00244 opt.gui = true;
00245 opt.parse(args);
00246
00247 QueensJavaPropagator queens =
00248 new QueensJavaPropagator(opt.size);
00249 opt.doSearch(queens);
00250 }
00251 }