// constructor / destructor: Processor::Processor() : Trace(0) { Accumulator(0); setInstructionCounter(0); InstructionRegister(0); setOperationCode(0); setOperand(0); // this is an address of cell in the current command }
int main(void) { const int inSize = 384; int *a = malloc(sizeof(int) * inSize); float *b = malloc(sizeof(float) * inSize); int *expected_int = malloc(sizeof(int) * inSize); float *fix_exp = malloc(sizeof(float) * inSize); int *out = malloc(sizeof(int) * inSize); float *fix_out = malloc(sizeof(int) * inSize); memset(out, 0, sizeof(int) * inSize); memset(fix_out, 0, sizeof(int) * inSize); memset(expected_int, 0, sizeof(int) * inSize); for(int i = 0; i < inSize; ++i) { a[i] = i + 1; b[i] = i + 1.5; expected_int[i] = a[i] + (i > 0 ? expected_int[i - 1] : 0); fix_exp[i] = b[i] + (i > 0 ? fix_exp[i - 1] : 0.0); } printf("Running on DFE.\n"); Accumulator(inSize, b, a, fix_out, out); for (int i = 0; i < inSize; i++) if (out[i] != expected_int[i]) { printf("Output from DFE did not match CPU: %d : %d != %d\n", i, out[i], expected_int[i]); return 1; } for (int i = 0; i < inSize; i++) if (fix_out[i] != fix_exp[i]) { printf("Output from DFE did not match CPU: %d : %f != %f\n", i, fix_out[i], fix_exp[i]); return 1; } printf("Test passed!\n"); return 0; }
void AntColonyOptimizationAlgorithm::doIteration() { VertexIterator it, it_end; boost::tie(it, it_end) = boost::vertices(g_); // list of vertices on the map VertexVector vertices(it, it_end); // index stands for ant id // VertexVector places the ant has already visited std::vector<VertexVector> travels; travels.reserve(vertices.size()); // at the very beggining suppose that ant has visited one city - // the one it is located at the moment std::for_each(vertices.begin(), vertices.end(), VertexAdder(travels)); std::vector<VertexVector>::iterator first(travels.begin()), last(travels.end()); for (; first != last; ++first) { //for every ant lets do travel VertexVector& tabu = *first; //we have not visited all the cities yet if (tabu.size() < vertices.size()) { // current city, the ant is located at const VertexDescriptor source = tabu.back(); VertexProbabilityMap probability_map; // investigate all the possible paths, which we have not visited yet // and find which is the best OutEdgeIterator begin, end; boost::tie(begin, end) = boost::out_edges(source, g_); for (; begin != end; ++begin) { const EdgeDescriptor edge = *begin; const VertexDescriptor target = boost::target(edge, g_); // check whether we have already visited the target if (tabu.end() == std::find(tabu.begin(), tabu.end(), target)) { //we have not visited it, so let's do calculation double pheromone_amount = pheromone_amount_map_[edge]; pheromone_amount = pow(pheromone_amount, alpha_); const double distance = edge_weight_map_[edge]; double desirability = 1 / distance; desirability = pow(desirability, beta_); probability_map.insert(std::make_pair(target, pheromone_amount * desirability)); } // we have checked whether we are through we that target } // fine, we have checked through all the pathes // now we need to choose the best according to probability values const double total_probability = std::for_each(probability_map.begin(), probability_map.end(), Accumulator(0.0)).value(); VertexProbabilityMap::const_iterator probability_it(probability_map.begin()), probability_it_end(probability_map.end()); VertexDescriptor target = probability_it->first; double probability = probability_it->second / total_probability; ++probability_it; for (; probability_it != probability_it_end; ++probability_it) { double current_probability = probability_it->second / total_probability; if (current_probability > probability) { target = probability_it->first; probability = current_probability; } } // target is the best vertex to go from source // lets go there tabu.push_back(target); } // we have done one step for one ant } // wow, we have visited all the cities by all the ants // and came to the initial beggining city // update the rate of pheromone on the edges // find total length every ant has gone std::vector<double> distances; distances.reserve(travels.size()); std::transform(travels.begin(), travels.end(), std::back_inserter(distances), DistanceCalculator(g_, edge_weight_map_)); // we have found total distance of every ant std::vector<double>::iterator min_it = std::min_element(distances.begin(), distances.end()); if (minimal_distance_ < 0 || minimal_distance_ > *min_it) { std::iterator_traits<std::vector<double>::iterator>::difference_type location = std::distance(distances.begin(), min_it); solution_ = travels[location]; minimal_distance_ = *min_it; } // find the left pheromone after each of ant PheromoneAmountMap left_pheromone_amount; const int Q = 100; typedef std::vector<VertexVector>::size_type size_type; for (size_type index = 0; index < travels.size(); ++index) { const double distance = distances[index]; const VertexVector& visited = travels[index]; VertexVector::const_iterator begin(visited.begin()), end(visited.end()); --end; EdgeDescriptor edge; bool found = false; for (; begin != end; ++begin) { boost::tie(edge, found) = boost::edge(*it, *boost::next(it), g_); left_pheromone_amount[edge] += (Q / distance); } boost::tie(edge, found) = boost::edge(vertices.back(), vertices.front(), g_); left_pheromone_amount[edge] += (Q / distance); } ++current_iteration_; //left pheromone values have been found std::for_each(pheromone_amount_map_.begin(), pheromone_amount_map_.end(), PheromoneAmountMapUpdater(rho_, left_pheromone_amount)); if (current_iteration_ >= iteration_number_) { is_finished_ = true; } report_data_.algorithmName = "Ant colony optimization"; report_data_.numIterations = current_iteration_; report_data_.optimalDistance = minimal_distance_; report_data_.result = solution_; }