Ejemplo n.º 1
0
        public void searchIteration(out bool searchCompleted)
        {
            searchCompleted = false;
            searchIterations++;

            // are just the lookup values we still have to lookup
            instructionIndices = programSampler.sampleProgram((int)numberOfInstructionsToEnumerate);

            uint[] sampledProgram = instructionIndices.Select(v => instructionIndexToInstruction[v]).ToArray();

            // copy
            localInterpreterState.program.setLength(sampledProgram.Length);
            for (int i = 0; i < sampledProgram.Length; i++)
            {
                localInterpreterState.program[i] = sampledProgram[i];
            }

            localInterpreterState.program[privateNumberOfInstructions - 1] = InstructionInterpreter.INSTRUCTION_RET; // overwrite last instruction with ret so it terminates always

            // append parent program if there is one
            if (problem.parentProgram != null)
            {
                // commented because the copy is pseudocode   parentProgram.CopyTo(localInterpreterState.program, privateNumberOfInstructions);
            }


            bool trainingSamplesTestedSuccessful = true;

            foreach (TrainingSample currentTrainingSample in problem.trainingSamples)
            {
                localInterpreterState.registers = new int[3];

                // reset interpreter state
                globalInterpreterState.reset();
                localInterpreterState.reset();

                // set question states
                for (int i = 0; i < currentTrainingSample.questionRegisters.Length; i++)
                {
                    if (currentTrainingSample.questionRegisters[i].HasValue)
                    {
                        localInterpreterState.registers[i] = currentTrainingSample.questionRegisters[i].Value;
                    }
                }

                globalInterpreterState.arrayState = new ArrayState();

                globalInterpreterState.arrayState.array.clear();
                for (int i = 0; i < currentTrainingSample.questionArray.Count; i++)
                {
                    globalInterpreterState.arrayState.array.append(currentTrainingSample.questionArray[i]);
                }

                if (currentTrainingSample.questionArrayIndex.HasValue)
                {
                    globalInterpreterState.arrayState.index = currentTrainingSample.questionArrayIndex.Value;
                }

                bool
                    programExecutedSuccessful,
                    hardExecutionError;

                // * interpret

                bool debugExecution = false;

                Interpreter.interpret(
                    globalInterpreterState,
                    localInterpreterState,
                    debugExecution,
                    out programExecutedSuccessful,
                    out hardExecutionError);

                if (!programExecutedSuccessful)
                {
                    trainingSamplesTestedSuccessful = false;
                    break;
                }

                // we are here if the program executed successfully and if it returned something


                // compare result
                if (
                    currentTrainingSample.answerArray != null &&
                    !ListHelpers.isSame(globalInterpreterState.arrayState.array, currentTrainingSample.answerArray)
                    )
                {
                    trainingSamplesTestedSuccessful = false;
                    break;
                }

                if (currentTrainingSample.answerArrayIndex.HasValue && globalInterpreterState.arrayState.index != currentTrainingSample.answerArrayIndex)
                {
                    trainingSamplesTestedSuccessful = false;
                    break;
                }

                if (currentTrainingSample.answerRegisters != null)
                {
                    for (int i = 0; i < currentTrainingSample.answerRegisters.Length; i++)
                    {
                        if (currentTrainingSample.answerRegisters[i].HasValue && currentTrainingSample.answerRegisters[i] != localInterpreterState.registers[i])
                        {
                            trainingSamplesTestedSuccessful = false;
                            break;
                        }
                    }
                }

                if (currentTrainingSample.answerFlag.HasValue && currentTrainingSample.answerFlag != localInterpreterState.comparisionFlag)
                {
                    trainingSamplesTestedSuccessful = false;
                    break;
                }
            }

            if (!trainingSamplesTestedSuccessful)
            {
                return; // try next program
            }

            // ** search was successful
            searchCompleted = true;
            return;
        }