void CTrainNodeCvKNN::train(bool doClean) { #ifdef DEBUG_PRINT_INFO printf("\n"); #endif // Filling the <samples> and <classes> Mat samples, classes; for (byte s = 0; s < m_nStates; s++) { // states int nSamples = m_pSamplesAcc->getNumSamples(s); #ifdef DEBUG_PRINT_INFO printf("State[%d] - %d of %d samples\n", s, nSamples, m_pSamplesAcc->getNumInputSamples(s)); #endif samples.push_back(m_pSamplesAcc->getSamplesContainer(s)); classes.push_back(Mat(nSamples, 1, CV_32FC1, Scalar(s))); if (doClean) m_pSamplesAcc->release(s); // free memory } // s samples.convertTo(samples, CV_32FC1); // Filling <var_type> Mat var_type(getNumFeatures() + 1, 1, CV_8UC1, Scalar(ml::VAR_NUMERICAL)); // all inputs are numerical var_type.at<byte>(getNumFeatures(), 0) = ml::VAR_CATEGORICAL; // Training try { m_pKNN->train(ml::TrainData::create(samples, ml::ROW_SAMPLE, classes, noArray(), noArray(), noArray(), var_type)); } catch (std::exception &e) { printf("EXCEPTION: %s\n", e.what()); getchar(); exit(-1); } }
void writeToFile(const std::string &name) { if (numNonZeros == 0) updateNumNonZeros(); if (getNumZ() == 0 || getNumFeatures() == 0 || getNumY() == 0 || getNumX() == 0 || zeroZero.norm() == 0) { std::cout << "[voxel::FeatureVoxel::writeToFile] Feature Voxel has unset " "members" << std::endl; exit(1); } std::ofstream out(name, std::ios::out | std::ios::binary); int z = getNumZ(); out.write(reinterpret_cast<const char *>(&z), sizeof(z)); for (int k = 0; k < z; ++k) saveMatrixAsSparse(this->voxelGrid[k], out); int numFeatureVectors = this->featureVectors.size(); out.write(reinterpret_cast<const char *>(&numFeatureVectors), sizeof(int)); for (auto &v : this->featureVectors) saveSpareVector(*v, out); out.write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); out.write(reinterpret_cast<const char *>(&numNonZeros), sizeof(numNonZeros)); out.write(reinterpret_cast<const char *>(zeroZero.data()), sizeof(zeroZero)); };
void SarsaAgent::update(double state[], int action, double reward, double discountFactor){ if(lastAction == -1){ for(int i = 0; i < getNumFeatures(); i++){ lastState[i] = state[i]; } lastAction = action; lastReward = reward; } else{ FA->setState(lastState); double oldQ = FA->computeQ(lastAction); FA->updateTraces(lastAction); double delta = lastReward - oldQ; FA->setState(state); //Sarsa update double newQ = FA->computeQ(action); delta += discountFactor * newQ; FA->updateWeights(delta, learningRate); //Assume gamma, lambda are 0. FA->decayTraces(discountFactor*lambda);//replace 0 with gamma*lambda for(int i = 0; i < getNumFeatures(); i++){ lastState[i] = state[i]; } lastAction = action; lastReward = reward; } }
void States::add( const States& a) { if( a.getNumFeatures()!=this->getNumFeatures() ) { std::cerr << "add: feature mismatch" << std::endl; exit(EXIT_FAILURE); } this->X += a.X; this->V += a.V; for(int i = 0; i<getNumFeatures(); i++ ) { this->features[i]->set_body_position( this->features[i]->get_body_position() + a.features[i]->get_body_position() ); this->features[i]->set_world_position( this->features[i]->get_world_position() + a.features[i]->get_world_position() ); } this->b += a.b; }
bool AudioDataSet<T>::save(const std::string & absoluteFilePath, std::string & errorString) { if (getTotalNumInstances() <= 0) { errorString = "No data to save"; return false; } if (!isReady()) { errorString = "Data set not ready. Finish recording instances"; return false; } File file(absoluteFilePath); file.deleteFile(); ValueTree vt("AudioDataSet"); vt.setProperty("NumSounds", var(numSounds), nullptr); vt.setProperty("InstancesPerSound", var(instancesPerSound), nullptr); vt.setProperty("NumFeatures", var(getNumFeatures()), nullptr); vt.setProperty("BufferSize", var(bufferSize), nullptr); vt.setProperty("STFTFramesPerBuffer", var(stftFramesPerBuffer), nullptr); vt.setProperty("NumDelayedBuffers", var(numDelayedBuffers), nullptr); auto dataBlockSize = static_cast<size_t>(data.size() * sizeof(T)); MemoryBlock dataMb(data.mem, dataBlockSize); vt.setProperty("Data", var(dataMb), nullptr); auto soundLabelsBlockSize = static_cast<size_t>(soundLabels.size() * sizeof(int)); MemoryBlock soundLabelsMb(soundLabels.mem, soundLabelsBlockSize); vt.setProperty("SoundLabels", var(soundLabelsMb), nullptr); ValueTree featuresUsedTree("FeaturesUsed"); for (auto featureFramePair : featuresUsed) { ValueTree pair("FeatureFramePair"); pair.setProperty("Frame", var(featureFramePair.first), nullptr); pair.setProperty("Feature", var(static_cast<int>(featureFramePair.second)), nullptr); featuresUsedTree.addChild(pair, -1, nullptr); } vt.addChild(featuresUsedTree, -1, nullptr); FileOutputStream os(file); if (os.getStatus().failed()) { errorString = "Error creating file"; os.flush(); return false; } vt.writeToStream(os); os.flush(); return true; }
int States::getRows ( ) const { return 9+3*getNumFeatures() ; } /* ----- end of method States::getRows ----- */
void CLinearMultiFeatureCalculator::getModifiedState(CStateCollection *stateCol, CState *featState) { assert(equals(featState->getStateProperties())); memset(actualPartition, 0, sizeof(unsigned int) * numDim); unsigned int j = 0; CState *state = stateCol->getState(originalState); /* printf("State : "); for (int i = 0; i < 4; i ++) { printf("%f ", state->getNormalizedContinuousState(i)); } printf("\n");*/ getSingleActiveFeature(state, singleStateFeatures); unsigned int i; int feature = 0; //offset to add to the actual feature int featureAdd = 0; getFeaturePosition(getActiveFeature(state), activePosition); // printf("["); // for (i = 0; i < numDim; i ++) // { // printf("%f ", state->getNormalizedContinuousState(dimensions[i])); // } // printf("]"); // activePosition->saveASCII(stdout); // printf("\n"); for (i = 0; i < numDim; i ++) { int singleFeatureOffset = 0; if (areaSize[i] % 2 == 0) { //double x1 = state->getNormalizedContinuousState(dimensions[i]); //double x2 = activePosition->element(i); if (state->getNormalizedContinuousState(dimensions[i]) < activePosition->element(i)) { singleFeatureOffset ++; } singleFeatureOffset += (areaSize[i] - 1) / 2; } else { singleFeatureOffset += areaSize[i] / 2; } singleStateFeatures[i] -= singleFeatureOffset; activePosition->element(i) = activePosition->element(i) - singleFeatureOffset * 1.0 / partitions[i] * gridScale[i]; } unsigned int featureIndex = 0; for (i = 0; i < areaNumPart; i++) { feature = 0; /*for (j = 0; j < numDim; j++) { int dist = (actualPartition[j] - areaSize[j]); featurePosition->setElement(j, (activePosition->element(j) + 1.0 / partitions[j] * dist)); featureAdd = (singleStateFeatures[j] + (actualPartition[j] - areaSize[j])); if (state->getStateProperties()->getPeriodicity(j)) { featurePosition->setElement(j, featurePosition->element(j) - floor(featurePosition->element(j))); featureAdd = featureAdd - (int) floor((double) featureAdd / (double) partitions[j]) * partitions[j]; } feature = feature + featureAdd * dimensionSize[j]; } */ /*for (j = 0; j < numDim; j++) { int dist = (actualPartition[j] - 1); featurePosition->setElement(j, (activePosition->element(j) + 1.0 / partitions[j] * dist)); featureAdd = (singleStateFeatures[j] + (actualPartition[j] - 1)); if (state->getStateProperties()->getPeriodicity(j)) { featurePosition->setElement(j, featurePosition->element(j) - floor(featurePosition->element(j))); featureAdd = featureAdd - (int) floor((double) featureAdd / (double) partitions[j]) * partitions[j]; } feature = feature + featureAdd * dimensionSize[j]; } for (i = 0; i < numDim; i ++) { int singleFeatureOffset = 0; if (areaSize[i] % 2 == 0) { if (state->getContinuousState(dimensions[i]) < activePosition->element(i)) { singleFeatureOffset ++; } singleFeatureOffset += (singleFeatureOffset - 1) / 2; } else { singleFeatureOffset += areaSize[i] / 2; } singleStateFeatures[i] -= singleFeatureOffset; activePosition->setElement(i, activePosition->element(i) - singleFeatureOffset * 1.0 / partitions[i]); }*/ for (j = 0; j < numDim; j++) { //int dist = (actualPartition[j] - areaSize[j]); featurePosition->element(j) = activePosition->element(j) + (1.0 / partitions[j] * actualPartition[j]) * gridScale[j]; featureAdd = (singleStateFeatures[j] + actualPartition[j]); if (state->getStateProperties()->getPeriodicity(j) && gridScale[j] >= 1.0) { featurePosition->element(j) = featurePosition->element(j) - floor(featurePosition->element(j)); featureAdd = featureAdd - (int) floor((double) featureAdd / (double) partitions[j]) * partitions[j]; } feature = feature + featureAdd * dimensionSize[j]; } if (feature >= 0 && (unsigned int) feature < getNumFeatures()) { featState->setDiscreteState(featureIndex, feature); featState->setContinuousState(featureIndex, getFeatureFactor(state, featurePosition)); featureIndex ++; } j = 0; actualPartition[0] ++; while (j < numDim && actualPartition[j] >= areaSize[j]) { actualPartition[j] = 0; j ++; if (j < numDim) { actualPartition[j]++; } } } featState->setNumActiveContinuousStates(featureIndex); featState->setNumActiveDiscreteStates(featureIndex); for (; featureIndex < areaNumPart; featureIndex ++) { featState->setDiscreteState(featureIndex, 0); featState->setContinuousState(featureIndex, 0.0); } this->normalizeFeatures(featState); }