double cal_score(vector <size_t> instance){ //f(x)=(UW)(TV)' + Px double score = 0.0; DblVec UW; DblVec TV; int dimLatent = W.size() / getUserFeaCount(); for (size_t j = 0 ; j < dimLatent; j++){ UW.push_back(0); TV.push_back(0); } for (size_t j = 0; j < instance.size(); j++){ score += P[instance[j]] * 1.0; if(instance[j] < getAdFeaCount()){ for(size_t k = 0; k < dimLatent; k++){ int v_index = instance[j] * dimLatent + k; TV[k]+=V[v_index]; } } else if(instance[j] < getAdFeaCount() + getUserFeaCount()){ for(size_t k = 0; k < dimLatent; k++){ int w_index = (instance[j] - getAdFeaCount())*dimLatent + k; UW[k] += W[w_index]; } } } for (size_t j = 0; j < dimLatent; j++){ score += UW[j]*TV[j]; } return score; }
int load_model(const char* model_path){ string line; char model_file_name[50]; sprintf(model_file_name, "%sP", model_path); double fea_weight; //load P ifstream pmodel(model_file_name); if(!pmodel.good()){ cerr << "error model file " << model_file_name << endl; exit(1); } while (getline(pmodel, line)){ fea_weight = atof(line.c_str()); P.push_back(fea_weight); } pmodel.close(); //load W sprintf(model_file_name, "%sX", model_path); ifstream xmodel(model_file_name); if(!xmodel.good()){ cerr << "error model file " << model_file_name << endl; exit(1); } while (getline(xmodel, line)){ fea_weight = atof(line.c_str()); X.push_back(fea_weight); } xmodel.close(); return 0; }
void SingleTimestepCollisionEvaluator::CalcDists(const DblVec& x, DblVec& dists, DblVec& weights) { DblVec dofvals = getDblVec(x, m_vars); m_rad->SetDOFValues(dofvals); vector<Collision> collisions; GetCollisionsCached(dofvals, collisions); dists.reserve(dists.size() + collisions.size()); weights.reserve(weights.size() + collisions.size()); BOOST_FOREACH(const Collision& col, collisions) { Link2Int::iterator itA = m_link2ind.find(col.linkA), itB = m_link2ind.find(col.linkB); if (itA != m_link2ind.end() || itB != m_link2ind.end()) { dists.push_back(col.distance); weights.push_back(col.weight); } }
double NeedleCollisionClearanceCost::value(const vector<double>& x, Model* model) { DblVec dists; EnvironmentBasePtr env = helper->pis[0]->local_configs[0]->GetEnv(); BOOST_FOREACH(const CollisionEvaluatorPtr& cc, this->ccs) { DblVec tmp_dists; cc->CalcDists(x, tmp_dists); for (int i = 0; i < tmp_dists.size(); ++i) { dists.push_back(-tmp_dists[i]); } } if (dists.size() > 0) { return vecMax(dists) * this->coeff; } else { return - helper->collision_clearance_threshold * this->coeff; } }