MCPcopy Create free account
hub / github.com/devosoft/avida / Task_SortInputs

Method Task_SortInputs

avida-core/source/main/cTaskLib.cc:2381–2486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2379}
2380
2381double cTaskLib::Task_SortInputs(cTaskContext& ctx) const
2382{
2383 const cArgContainer& args = ctx.GetTaskEntry()->GetArguments();
2384 const tBuffer<int>& output = ctx.GetOutputBuffer();
2385 const int size = args.GetInt(0);
2386 const int stored = output.GetNumStored();
2387
2388 // if less than half, can't possibly reach threshold
2389 if (stored <= (size / 2)) return 0.0;
2390
2391 Apto::Map<int, int> valmap;
2392 int score = 0;
2393 int maxscore = 0;
2394
2395 // add all valid inputs into the value map
2396 for (int i = 0; i < size; i++) valmap.Set(ctx.GetOrganism()->GetInputAt(i), -1);
2397
2398 int span_start = -1;
2399 int span_end = stored;
2400
2401 if (args.GetInt(2)) { // Contiguous
2402 // scan for the largest contiguous span
2403 // - in the event of a tie, keep the first discovered
2404 for (int i = 0; i < stored; i++) {
2405 if (valmap.Has(output[i])) {
2406 int t_start = i;
2407 while (++i < stored && valmap.Has(output[i])) ;
2408 if (span_start == -1 || (i - t_start) > (span_end - span_start)) {
2409 span_start = t_start;
2410 span_end = i;
2411 }
2412 }
2413 }
2414
2415 // no span was found
2416 if (span_start == -1) return 0.0;
2417 } else { // Scattered
2418 // search for first valid entry
2419 while (++span_start < stored && valmap.Has(output[span_start])) ;
2420
2421 // scanned past the end of the output, nothing to validate
2422 if (span_start >= stored) return 0.0;
2423 }
2424
2425 // again, if span is less than half the size can't possibly reach threshold
2426 if ((span_end - span_start) <= (size / 2)) return 0.0;
2427
2428 // insertion sort span
2429 // - count number of actual entries
2430 // - count moves required
2431 // - update valmap, tracking observed inputs
2432 Apto::Array<int> sorted(size);
2433 const bool ascending = (args.GetInt(1) >= 0);
2434 int count = 1;
2435
2436 // store first value
2437 valmap.Set(output[span_start], span_start);
2438 sorted[0] = output[span_start];

Callers

nothing calls this directly

Calls 10

GetTaskEntryMethod · 0.80
GetIntMethod · 0.80
GetNumStoredMethod · 0.80
GetArgumentsMethod · 0.45
SetMethod · 0.45
GetInputAtMethod · 0.45
GetOrganismMethod · 0.45
HasMethod · 0.45
GetMethod · 0.45
GetDoubleMethod · 0.45

Tested by

no test coverage detected