this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2017
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34
35#include <gecode/flatzinc/branch.hh>
36
37namespace Gecode { namespace FlatZinc {
38
39 void
40 PosIntChoice::archive(Archive& e) const {
41 Choice::archive(e);
42 e << _pos;
43 e << _val;
44 }
45
46
47 bool
48 IntBoolBrancherBase::status(const Space&) const {
49 if (start < x.size()) {
50 for (int i=start; i < x.size(); i++)
51 if (!x[i].assigned()) {
52 start = i;
53 return true;
54 }
55 start = x.size();
56 }
57 for (int i=start-x.size(); i < y.size(); i++)
58 if (!y[i].assigned()) {
59 start = x.size() + i;
60 return true;
61 }
62 return false;
63 }
64
65 ExecStatus
66 IntBoolBrancherBase::commit(Space& home, const Choice& _c,
67 unsigned int b) {
68 const PosIntChoice& c
69 = static_cast<const PosIntChoice&>(_c);
70 int p=c.pos(); int n=c.val();
71 if (p < x.size()) {
72 return me_failed(xvsc->commit(home,b,x[p],p,n)) ?
73 ES_FAILED : ES_OK;
74 } else {
75 p -= x.size();
76 return me_failed(yvsc->commit(home,b,y[p],p,n)) ?
77 ES_FAILED : ES_OK;
78 }
79 }
80
81 NGL*
82 IntBoolBrancherBase::ngl(Space& home, const Choice& _c,
83 unsigned int b) const {
84 const PosIntChoice& c
85 = static_cast<const PosIntChoice&>(_c);
86 int p=c.pos(); int n=c.val();
87 if (p < x.size()) {
88 return xvsc->ngl(home,b,x[p],n);
89 } else {
90 p -= x.size();
91 return yvsc->ngl(home,b,y[p],n);
92 }
93 }
94
95 void
96 IntBoolBrancherBase::print(const Space& home, const Choice& _c,
97 unsigned int b,
98 std::ostream& o) const {
99 const PosIntChoice& c
100 = static_cast<const PosIntChoice&>(_c);
101 int p=c.pos(); int n=c.val();
102 if (p < x.size()) {
103 xvsc->print(home,b,x[p],p,n,o);
104 } else {
105 p -= x.size();
106 yvsc->print(home,b,y[p],p,n,o);
107 }
108 }
109
110 const Choice*
111 IntBoolBrancherBase::choice(const Space& home, Archive& e) {
112 (void) home;
113 int p; e >> p;
114 int v; e >> v;
115 return new PosIntChoice(*this,2,p,v);
116 }
117
118
119 void
120 branch(Home home, const IntVarArgs& x, const BoolVarArgs& y,
121 IntBoolVarBranch vars, IntValBranch vals) {
122 if (home.failed()) return;
123 vars.expand(home,x,y);
124 ViewArray<Int::IntView> xv(home,x);
125 ViewArray<Int::BoolView> yv(home,y);
126 ValSelCommitBase<Int::IntView,int>* xvsc =
127 Int::Branch::valselcommit(home,vals);
128 ValSelCommitBase<Int::BoolView,int>* yvsc =
129 Int::Branch::valselcommit(home,i2b(vals));
130 switch (vars.select()) {
131 case IntBoolVarBranch::SEL_AFC_MAX:
132 {
133 MeritMaxAFC m(home,vars);
134 IntBoolBrancher<MeritMaxAFC>::post(home,xv,yv,m,xvsc,yvsc);
135 }
136 break;
137 case IntBoolVarBranch::SEL_ACTION_MAX:
138 {
139 MeritMaxAction m(home,vars);
140 IntBoolBrancher<MeritMaxAction>::post(home,xv,yv,m,xvsc,yvsc);
141 }
142 break;
143 case IntBoolVarBranch::SEL_CHB_MAX:
144 {
145 MeritMaxCHB m(home,vars);
146 IntBoolBrancher<MeritMaxCHB>::post(home,xv,yv,m,xvsc,yvsc);
147 }
148 break;
149 case IntBoolVarBranch::SEL_AFC_SIZE_MAX:
150 {
151 MeritMaxAFCSize m(home,vars);
152 IntBoolBrancher<MeritMaxAFCSize>::post(home,xv,yv,m,xvsc,yvsc);
153 }
154 break;
155 case IntBoolVarBranch::SEL_ACTION_SIZE_MAX:
156 {
157 MeritMaxActionSize m(home,vars);
158 IntBoolBrancher<MeritMaxActionSize>::post(home,xv,yv,m,xvsc,yvsc);
159 }
160 break;
161 case IntBoolVarBranch::SEL_CHB_SIZE_MAX:
162 {
163 MeritMaxCHBSize m(home,vars);
164 IntBoolBrancher<MeritMaxCHBSize>::post(home,xv,yv,m,xvsc,yvsc);
165 }
166 break;
167 default:
168 GECODE_NEVER;
169 }
170 }
171
172}}
173
174// STATISTICS: flatzinc-branch
175