vector<float> PhraseDictionaryMultiModel::Optimize(OptimizationObjective *ObjectiveFunction, size_t numModels) { dlib::matrix<double,0,1> starting_point; starting_point.set_size(numModels); starting_point = 1.0; try { dlib::find_min_bobyqa(*ObjectiveFunction, starting_point, 2*numModels+1, // number of interpolation points dlib::uniform_matrix<double>(numModels,1, 1e-09), // lower bound constraint dlib::uniform_matrix<double>(numModels,1, 1e100), // upper bound constraint 1.0, // initial trust region radius 1e-5, // stopping trust region radius 10000 // max number of objective function evaluations ); } catch (dlib::bobyqa_failure& e) { cerr << e.what() << endl; } vector<float> weight_vector (numModels); for (int i=0; i < starting_point.nr(); i++) { weight_vector[i] = starting_point(i); } cerr << "Cross-entropy: " << (*ObjectiveFunction)(starting_point) << endl; return weight_vector; }
bool connexify (Solution* sol) { /** 0. Define Data structure */ Composantes composantes; // Vector of set of point that will store the composants Point father, son; /** 1. Find connex composants */ VLOG(3) << "Finding connex composantes"; for (int i = 0; i < sol->data_.m; ++i) for (int j = 0; j < sol->data_.n; ++j) { if (sol->x_[i][j] == 1 && find_in_composantes(std::make_pair(i,j), composantes) == -1) { Point starting_point(i,j); if (not(add_composants(*sol, &composantes, starting_point))) LOG(FATAL) << "We failed at adding composantes"; } } // Debug for (int i = 0; i < composantes.size(); ++i) VLOG(3) << "La composante " << i << " a " << composantes[i].size() << " éléments"; /** 2. Reduce the number of composants */ VLOG(3) << "Reducing the number of composants"; while (composantes.size() > 1) { // We select a composant (here the last) int comp_id = composantes.size()-1; VLOG(3) << "Removing composant number " << comp_id; // We expand the set artificially, and we try to find an intersection with // the others. We first copy the set inside a working set std::set<Point> set_expanded = composantes[comp_id]; std::map<Point,Point> ancestors; std::pair<int,Point> bridge(-1,std::make_pair(-1,-1)); VLOG(3) << "Expanding the solution"; while (bridge.first == -1) { // No intersection expand(*sol, &set_expanded, &ancestors); bridge = intersection(set_expanded, composantes); } VLOG(3) << "Reconstructing the solution"; // We construct a path inside the solution, and we update the composants. son = bridge.second; father = ancestors[son]; while (composantes[comp_id].find(father) == composantes[comp_id].end()) { // We add the father composantes[bridge.first].insert(father); sol->x_[father.first][father.second] = 1; son = father; father = ancestors[son]; } VLOG(3) << "Mergin the two composants"; // We merge the last composant into bridge.first and we remove the last composantes[bridge.first].insert(composantes[comp_id].begin(),composantes[comp_id].end()); composantes.pop_back(); } return true; }
void maxLikelihoodTwoHyperLogLogEstimation(const TwoHyperLogLogStatistic& jointStatistic, double& cardinalityA, double& cardinalityB, double& cardinalityX, std::size_t& numFunctionEvaluations, std::size_t& numGradientEvaluations) { const double eps = 1e-2; const int m = jointStatistic.getNumRegisters(); const double relativeErrorLimit = eps/(sqrt(m)); const double zeroPhi = 2 * std::log(std::numeric_limits<double>::min()); assert(std::exp(zeroPhi) == 0); bool isOptimum; estimateInitialCardinalities(jointStatistic, cardinalityA, cardinalityB, cardinalityX, isOptimum); if (isOptimum) { numFunctionEvaluations = 0; numGradientEvaluations = 0; return; } double phiA = (cardinalityA > 0)?std::log(cardinalityA):zeroPhi; double phiB = (cardinalityB > 0)?std::log(cardinalityB):zeroPhi; double phiX = (cardinalityX > 0)?std::log(cardinalityX):zeroPhi; JointLogLikelihoodFunction logLikelihoodFunction(jointStatistic); LogLikelihoodFunctionForDlib logLikelihoodFunctionForDlib(logLikelihoodFunction); dlib::matrix<double,3,1> starting_point = {phiA, phiB, phiX}; dlib::find_max( dlib::bfgs_search_strategy(), // Use BFGS search algorithm StopStrategy(relativeErrorLimit), std::bind(&LogLikelihoodFunctionForDlib::value, std::ref(logLikelihoodFunctionForDlib), std::placeholders::_1), std::bind(&LogLikelihoodFunctionForDlib::derivative, std::ref(logLikelihoodFunctionForDlib), std::placeholders::_1), starting_point, std::numeric_limits<double>::infinity()); cardinalityA = std::exp(starting_point(0)); cardinalityB = std::exp(starting_point(1)); cardinalityX = std::exp(starting_point(2)); numFunctionEvaluations = logLikelihoodFunction.getNumFunctionEvaluations(); numGradientEvaluations = logLikelihoodFunction.getNumGradientEvaluations(); }