File: Strategy.cc

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (253 lines) | stat: -rw-r--r-- 6,201 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/****   , [ bobocheTree.cc ],
 Copyright (c) 2008 Universite d'Orleans - Jeremie Vautard

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 *************************************************************************/

#include "Strategy.hh"

StrategyImp::StrategyImp() {
	cout<<"Default constructor of StrategyImp should not be called !"<<endl;
	pointers=1;
	zetag=StrategyNode::Dummy();
	todos=0;
	father=NULL;
}

void StrategyImp::todosUpdate(int i) {
	todos += i;
	if (father != NULL) father->todosUpdate(i);
}

StrategyImp::StrategyImp(StrategyNode tag) {
	//    cout<<"Strategy imp constructor"<<endl;
	pointers=1;
	zetag=tag;
	todos=0;
	father=NULL;
	//    cout<<"strategyimp constructor fini"<<endl;
}


StrategyImp::~StrategyImp() {
	for (vector<Strategy>::iterator i = nodes.begin();i != nodes.end();i++) {
		if (((*i).imp->father) == this) {
			(*i).imp->father = NULL;
		}
	}
}


Strategy::Strategy() {
	//    cout<<"strategy default"<<endl;
	StrategyNode tag = StrategyNode::Dummy();
	imp = new StrategyImp(tag);
}

Strategy::Strategy(StrategyNode tag) {
	//    cout<<"Strategy with tag"<<endl;
	imp = new StrategyImp(tag);
	//cout<<"passed imp creation. End of strategy creator"<<endl;
}

Strategy::Strategy(StrategyImp* imp) {
	this->imp = imp;
	this->imp->pointers++;
}

Strategy::Strategy(bool qt,int VMin, int VMax, int scope, vector<int> values) {
	//    cout<<"strategy with values"<<endl;
	StrategyNode tag(2,qt,VMin,VMax,scope);
	tag.valeurs=values;
	imp = new StrategyImp(tag);

}


Strategy::Strategy(const Strategy& tree) {
	//    cout<<"Strategy copy"<<endl;
	imp = tree.imp;
	(imp->pointers)++;
}


Strategy& Strategy::operator = (const Strategy& rvalue) {
	//    cout<<"Strategy = "<<endl;
	if (imp != NULL) {
		(imp->pointers)--;
		if ( (imp->pointers) == 0) {
			//        cout<<"no more references for the imp. Delete"<<endl;
			delete imp;
		}
	}
	imp = rvalue.imp;
	(imp->pointers)++;
	return *this;
}


Strategy::~Strategy() {
	//    cout<<"strategy destructor"<<endl;
	(imp->pointers)--;
	if ( (imp->pointers) == 0) {
		//        cout<<"no more references for the imp. Delete"<<endl;
		delete imp;
	}
}


StrategyNode Strategy::getTag() {
	return imp->zetag;
}

Strategy Strategy::getFather() {
	if (hasFather()) return Strategy(imp->father);
	return Dummy();
}

bool Strategy::hasFather() {
	if (imp->father != NULL) {
		for (int i=0;i< imp->father->nodes.size();i++) {
			if ((imp->father->nodes[i].imp)  == imp)
				return true;
		}
	}
	return false;
}

Strategy Strategy::getChild(int i) {
	if (i<0 || i>=degree() ) {cout<<"Child "<<i<<" does not exist"<<endl;abort();}
	return imp->nodes[i];
}

Strategy Strategy::getSubStrategy(vector<int> position) {
	if (position.empty()) return *this;
	int deg = degree();
	if (deg == 0) {
		cout<<"Did not find substrategy"<<endl;
		return Strategy::Dummy();
	}
	for (int i=0;i<deg;i++) {
		Strategy child=getChild(i);
		bool ok=true;
		if (child.values().size() == 0) {
			ok = false;
		}
		for (int j=0;(j<child.values().size()) && ok;j++) {
			if (child.value(j) != position[j]) ok=false;
		}
		if (ok) {
			position.erase(position.begin(),position.begin() + (child.values().size()));
			return child.getSubStrategy(position);
		}
	}
	cout<<"Did not find substrategy"<<endl;
	return Strategy::Dummy();
}

void Strategy::attach(Strategy child) {
	if (child.isDummy()) {
		int todosToAdd = 0;

		for (int i=0;i<child.degree();i++) {
			this->attach(child.getChild(i));
		}
	}
	else {
		imp->nodes.push_back(child);
		todosUpdate(child.imp->todos);
		(child.imp)->father = this->imp;
	}
}

void Strategy::detach(Strategy son) {

	vector<Strategy>::iterator it = imp->nodes.begin();
	while (it != (imp->nodes.end()) && ( (*it).id() != son.id())) {
	it++;}
	if ( it != imp->nodes.end()) {
		todosUpdate(0-((*it).imp->todos));
		(*it).imp->father=NULL;
		imp->nodes.erase(it);
	}
}


void Strategy::detach(unsigned int i) {
	if (imp->nodes.size() < i) return;

	vector<Strategy>::iterator it = imp->nodes.begin()+i;
	todosUpdate(0-((*it).imp->todos));
	(*it).imp->father=NULL;
	imp->nodes.erase(it);
}

Strategy Strategy::STrue() {
	Strategy ret(StrategyNode::STrue());
	return ret;
}

Strategy Strategy::SFalse() {
	Strategy ret(StrategyNode::SFalse());
	return ret;
}

Strategy Strategy::Dummy() {
	Strategy ret(StrategyNode::Dummy());
	return ret;
}

Strategy Strategy::Stodo() {
	Strategy ret(StrategyNode::Todo());
	ret.imp->todos=1;
	return ret;
}

vector<int> Strategy::getPosition() {
	vector<int> ret;
	Strategy asc = *this;
	while (!asc.isDummy()) {
//		cout<<"GetPosition adding "<<asc.values().size()<<" elements to a vector of size "<<ret.size()<<endl;
		vector<int> ret2;
		ret2.reserve(ret.size() + asc.values().size() +1);
		for (int i=0;i<asc.values().size();i++) {
			ret2.push_back(asc.values()[i]);
		}
		for (int i=0;i<ret.size();i++) {
			ret2.push_back(ret[i]);
		}
		ret=ret2;
		asc = asc.getFather();
	}
	return ret;
}

int Strategy::checkIntegrity() {
	int ret=0;
	for (unsigned int i=0;i < (this->degree());i++) {
		if ( (((imp->nodes[i]).imp)->father) != imp) {
			ret++;
			cout<< (((imp->nodes[i]).imp)->father) << " should be " << imp <<endl;
		}
		ret += (getChild(i).checkIntegrity());
	}
	return ret;
}