summaryrefslogtreecommitdiff
path: root/casa/covering/space/GraftSpace.C
blob: daa84e0f89a2b20530e2c7a94bc160841ccac25c (plain)
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
// Copyright 2008, 2009 Brady J. Garvin

// This file is part of Covering Arrays by Simulated Annealing (CASA).

// CASA is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// CASA is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with CASA.  If not, see <http://www.gnu.org/licenses/>.


#include <algorithm>

#include "utility/igreater.H"

#include "covering/space/GraftSpace.H"

using namespace std;

#ifndef MAXIMUM_SUBROW_CHANGE_FAILED_ATTEMPTS
#define MAXIMUM_SUBROW_CHANGE_FAILED_ATTEMPTS 32
#endif

Array<unsigned>GraftSpace::createRandomMatchingRow(InputKnown&known) const {
  Array<unsigned>result(options.getSize());
  for (unsigned i = options.getSize(); i--;) {
    vector<unsigned>candidates;
    for (unsigned
	   j = options.getSymbolCount(i),
	   base = options.getFirstSymbol(i);
	 j--;) {
      candidates.push_back(base + j);
    }
    unsigned index = rand() % candidates.size();
    unsigned symbol = candidates[index];
    known.append(InputTerm(false, symbol));
    while (!solver(known)) {
      known.undoAppend();
      candidates[index] = candidates[candidates.size() - 1];
      candidates.pop_back();
      index = rand() % candidates.size();
      symbol = candidates[index];
      known.append(InputTerm(false, symbol));
    }
    result[i] = symbol;
  }
  return result;
}

CoveringArray GraftSpace::createStartState(unsigned rows) const {
  CoveringArray result(rows, strength, options, solver);
  unsigned i = rows;
  while (i-->resurrectionBuffer.size()) {
    InputKnown known;
    result.setBackingArrayRow(i, createRandomMatchingRow(known));
  }
  if (resurrectionBuffer.size()) {
    for (++i;i--;) {
      Array<unsigned>row = resurrectionBuffer[i];
      // We must deep copy to preserve the resurrection buffer.
      for (unsigned j = options.getSize(); j--;) {
	result.setBackingArrayEntry(i, j, row[j]);
      }
    }
  }
  result.setTrackingCoverage(true);
  result.setTrackingNoncoverage(true);
  return result;
}

CoverageCost GraftSpace::getTraveled(const CoveringArray&start) const {
  return 0;
}

CoverageCost GraftSpace::getTraveled
  (const Node<CoveringArray, CoverageCost>&parent,const CoveringArray&state)
  const {
  return 0;
}

set<CoveringArray>GraftSpace::getChildren
  (const CoveringArray&state, float proportion) const {
  assert(false);  // Unimplemented.
  return getChildren(state, (unsigned)1);
}

set<CoveringArray>GraftSpace::getChildren
  (const CoveringArray&state, unsigned count) const {
  set<CoveringArray>result;
  unsigned rows = state.getRows();
  assert(options.getSize() == state.getOptions());
  assert(state.isTrackingNoncoverage());
  const set<Array<unsigned>, ArrayComparator<unsigned> >&noncoverage =
    state.getNoncoverage();
  if (noncoverage.empty()){
    return result;
  }
  unsigned attempts = 0;
  for (unsigned i = count; i; ++attempts) {
    unsigned row = rand() % rows;
    set<Array<unsigned>, ArrayComparator<unsigned> >::const_iterator
      combinationIterator = noncoverage.begin();
    unsigned combinationIndex = rand() % noncoverage.size();
    while (combinationIndex--) {
      ++combinationIterator;
    }
    Array<unsigned>combination = *combinationIterator;
    Array<unsigned>columns(strength);
    InputKnown known;
    if (attempts < MAXIMUM_SUBROW_CHANGE_FAILED_ATTEMPTS) {
      for (unsigned j = strength; j--;) {
	columns[j] = options.getOption(combination[j]);
      }
      for (unsigned j = options.getSize(), k = strength; j--;) {
	if (k && columns[k-1] == j) {
	  unsigned symbol = combination[--k];
	  known.append(InputTerm(false, symbol));
	} else {
	  assert(!k || columns[k - 1] < j);
	  unsigned symbol = state(row, j);
	  known.append(InputTerm(false, symbol));
	}
      }
      if (solver(known)) {
	CoveringArray child = state;
	child(row, columns) = combination;
	result.insert(child);
	--i;
	attempts = 0;
      }
    } else {
      cout << "Considering a full row change" << endl;
      for (unsigned k = strength; k--;) {
	known.append(InputTerm(false, combination[k]));
      }
      CoveringArray child = state;
      child(row) = createRandomMatchingRow(known);
      result.insert(child);
      --i;
      attempts = 0;
    }
  }
  return result;
}

void GraftSpace::signal(const SearchFinish<CoveringArray, CoverageCost>&finish) {
  const set<const Node<CoveringArray, CoverageCost>*>&best =
    finish.source.getBest();
  if (best.empty()) {
    return;
  }
  const CoveringArray&solution = (*best.begin())->getState();
  unsigned rowCount = solution.getRows();
  unsigned optionCount = solution.getOptions();
  resurrectionBuffer.reserve(rowCount);
  while (resurrectionBuffer.size() < rowCount) {
    resurrectionBuffer.push_back(Array<unsigned>());
  }
  cout << "Sorting rows for distinct coverage" << endl;
  Array<unsigned>distinctCoverage = solution.countDistinctCoverage();
  Array<unsigned>permutation(rowCount);
  for (unsigned i = rowCount; i--;) {
    permutation[i] = i;
  }
  igreater<unsigned>betterDistinctCoverage(distinctCoverage);
  sort(permutation + 0, permutation + rowCount, betterDistinctCoverage);
  cout << "Saving rows in resurrection buffer" << endl;
  for (unsigned i = rowCount; i--;) {
    unsigned p = permutation[i];
    Array<unsigned>row = Array<unsigned>(optionCount);
    for (unsigned j = optionCount; j--;) {
      row[j] = solution(p, j);
    }
    resurrectionBuffer[i] = row;
  }
}

void GraftSpace::clearResurrectionBuffer() {
  resurrectionBuffer.clear();
}