double minf(PointModel thisModel) { //Now reduce the results double total = 0; Vals results; results.resize(nuclei.size()); for(unsigned long i = 0;i<expVals.size();i++) { results[i] = thisModel.eval(nuclei[i].x,nuclei[i].y,nuclei[i].z); double diff = expVals[i] - results[i]; total += diff*diff; } //Push the results into calcVals = results; //Record the results fout << thisModel.ax << " " << thisModel.rh << " " << thisModel.metal.x << " " << thisModel.metal.y << " " << thisModel.metal.z << " " << thisModel.angle_x << " " << thisModel.angle_y << " " << thisModel.angle_z << " " << endl; return total; }
/** * Function that returns valuation of the formula based on valuation of its variables. * * @param[in] valuation map of variable valuations in the form (name, value) * @param[in] formula formula to resolve * * @return true iff valuation of the formula is true */ static bool resolve (const Vals & valuation, string formula) { // If there is a ! symbol, negate the formula and remove it bool negate = false; bool result; boost::trim(formula); if (formula[0] == '!') { negate = true; formula = formula.substr(1); } boost::trim(formula); // Search for any operator, if not found, assume formula to be an atom and return its valuation if (formula.find("|") == string::npos && formula.find("&") == string::npos) { if (formula.compare("tt") == 0) { result = true; } else if (formula.compare("ff") == 0) { result = false; } else { auto variable = valuation.find(formula); if (variable == valuation.end()) throwException(formula, err_notfound); result = variable->second; } } else { // Find position of the operator and its kind (or/and) bool is_or = false; size_t division_pos = 0; readFormula(formula, is_or, division_pos); // Divide formula by the operator and remove its outer parenthesis, then descend recursivelly string first = formula.substr(1, division_pos - 1); string second = formula.substr(division_pos + 1, formula.size() - division_pos - 2); result = is_or ? resolve(valuation, first) | resolve(valuation, second) : resolve(valuation, first) & resolve(valuation, second); } // Return the value based on valuations of its parts return negate ? !result : result; }
int main() { //Load the data pairNucVals data = loadData("dataset_one.inp"); nuclei = data.first; expVals = data.second; calcVals.resize(expVals.size()); //Set up the model PointModel m; m.ax = 100.0; m.rh = 0.0; m.metal = Vector3((nuclei.xmin+nuclei.xmax)/2, (nuclei.ymin+nuclei.ymax)/2, (nuclei.zmin+nuclei.zmax)/2); m.setEulerAngles(0,0,0); //Start the visualisation thread VisualThread visualThread; visualThread.fw.setNuclei(nuclei); visualThread.fw.setExpVals(expVals); boost::thread boostVisualThread(boost::ref(visualThread)); //Initalise the minimizer Minimiser<PointModel> minimiser(&unpackPointModel, &packPointModel, &minf, bind(onIterate,boost::ref(visualThread),_1)); //Open the log file fout.open("params.log"); if(!fout.is_open()) { cerr << "Could not open params.log for writing" << endl; return 1; } //Clear the results directory system("rm results/*"); double best = numeric_limits<double>::infinity(); PointModel bestModel; //Main loop for(long i = 0;i<10;i++) { for(long j = 0;j<10;j++) { for(long k = 0;k<10;k++) { double fx = i/10.0; double fy = j/10.0; double fz = k/10.0; m.metal.x = nuclei.xmin*(1-fx) + nuclei.xmax*fx; m.metal.y = nuclei.xmin*(1-fy) + nuclei.xmax*fy; m.metal.z = nuclei.xmin*(1-fz) + nuclei.xmax*fz; cout << "Starting: (" << m.metal.x << "," << m.metal.y << "," << m.metal.z << ")" << endl; std::pair<double,PointModel> thePair = minimiser.minimise(m); double found_min = thePair.first; if(best > found_min) { bestModel = thePair.second; best = found_min; cout << "New Best Model:" << endl; cout << "ax = " << bestModel.ax << "rh = " << bestModel.rh << "x = " << bestModel.metal.x << "y = " << bestModel.metal.y << "z = " << bestModel.metal.z << "angle_x = " << bestModel.angle_x << "angle_y = " << bestModel.angle_y << "angle_z = " << bestModel.angle_z << endl << endl; } cout << "Finished, minimum = " << found_min << " best so far " << best <<endl; } } } return 0; }
void Demo::Draw() { Tree::clear_window( Tree::Color::black ); str.SetText( boost::lexical_cast<std::string>( t.GetTime() ) ); str.SetPosition( 100, 5 ); Tree::draw( str ); str.SetText( boost::lexical_cast<std::string>( st.GetTime() ) ); str.SetPosition( 100, 15 ); Tree::draw( str ); str.SetText( boost::lexical_cast<std::string>( cd.GetTime() ) ); str.SetPosition( 200, 5 ); Tree::draw( str ); if( cd.IsDone() ) { str.SetText( "done" ); } else { str.SetText( "not done" ); } str.SetPosition( 200, 15 ); Tree::draw( str ); // Draw shuffle bag's contents int n = 1; const float h = 10; for( Ints::iterator it = bagged.begin(); it != bagged.end(); ++it, ++n ) { str.SetPosition( 10, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } n = 1; for( Ints::iterator it = rest.begin(); it != rest.end(); ++it, ++n ) { str.SetPosition( 30, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } Vec2i mpos = Tree::get_mouse_pos(); Vec2i l1 = point - mpos; Tree::clip( l1, 0, 50 ); Tree::draw( sf::Shape::Line( point, point - l1, 1.0, Tree::Color( 0xff557733))); Vec2i p2( point.x, point.y - 50 ); Vec2i l2 = point - p2; Tree::clip( l2, 0, 50 ); Tree::draw( sf::Shape::Line( point, point - l2, 1.0, Tree::Color( 0xff775533))); float rad = angle( l1, l2 ); str.SetPosition( point.x + 20, point.y - 30 ); str.SetText( boost::lexical_cast<std::string>( rad ) ); Tree::draw( str ); float degree = Tree::rad2deg( rad ); str.SetPosition( point.x + 20, point.y - 20 ); str.SetText( boost::lexical_cast<std::string>( degree ) ); Tree::draw( str ); // Check neighbours str.SetPosition( 250, 350 ); str.SetText( "neighbours" ); Tree::draw( str ); typedef std::vector<Vec2i> Points; Points ps = Tree::generate_neighbours( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 250, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 320, 350 ); str.SetText( "corners" ); Tree::draw( str ); ps = Tree::generate_corners( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 320, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 400, 350 ); str.SetText( "both" ); Tree::draw( str ); ps = Tree::generate_surroundings( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 400, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } // Draw weight bag's selections typedef std::vector<std::string> Vals; typedef std::vector<float> Weights; Vals vals = weight_bag.GetVals(); Weights weights = weight_bag.GetWeights(); std::stringstream ss; ss.precision( 2 ); n = 1; for( Weights::iterator it = weights.begin(); it < weights.end(); ++it, ++n ) { str.SetPosition( 60, 30 + h * n ); ss.str(""); ss << *it; //str.SetText( boost::lexical_cast<std::string>( *it ) ); str.SetText( ss.str() ); Tree::draw( str ); } n = 1; for( Vals::iterator it = vals.begin(); it < vals.end(); ++it, ++n ) { str.SetPosition( 85, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 170, 40 ); ss.str(""); ss << "0.2: " << apples << " " << (float)apples / (float)total_weight << " Apples"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 50 ); ss.str(""); ss << "0.2: " << oranges << " " << (float)oranges / (float)total_weight << " Oranges"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 60 ); ss.str(""); ss << "0.6: " << strawberries << " " << (float)strawberries / (float)total_weight << " Strawberries"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 70 ); str.SetText( "last: " + curr_weight ); Tree::draw( str ); // Draw shapes Tree::draw_line( 500, 200, 520, 220, Tree::Color::white ); Tree::draw_line( 520, 220, 540, 180, Tree::Color::yellow, 2.0 ); Tree::draw_line( Vec2i( 540, 180 ), Vec2i( 560, 200 ), Tree::Color::magenta, 2.0, Tree::Color::cyan, 1.0 ); Tree::draw_rect( 600, 100, 640, 140, Tree::Color::blue ); Tree::draw_rect( 600, 200, 640, 240, Tree::Color::green, Tree::Color::red, 1.0 ); Tree::draw_circle( 700, 250, 40, Tree::Color::white ); Tree::draw_triangle( 610, 300, 600, 320, 620, 320, Tree::Color::white ); Tree::draw_triangle( 610, 360, 600, 340, 620, 340, Tree::Color::white, Tree::Color::red, 2.0 ); // Draw statusbar float perc = st.GetTime() - (int)(st.GetTime() / 2.0) * 2.0; Tree::draw_bar( 700, 580, 790, 590, perc, Tree::Color( 0xff333333 ), Tree::Color( 0xffaaaaaa ), Tree::Color::white, 1.0 ); // Draw indexed sprites for( size_t i = 0; i < sprites.size(); ++i ) { sf::Sprite &spr = sprites[i]; spr.SetPosition( 40 + i * 32, 530 ); Tree::draw( spr ); } // Draw regular sprite aspr.SetPosition( 50, 200 ); if( perc > 1.0 ) perc = 1.0; Tree::set_alpha( aspr, Tree::enbyten( perc ) ); Tree::draw( aspr ); }