Changeset c62f96
- Timestamp:
- Dec 19, 2012, 3:25:30 PM (12 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- 65c42c
- Parents:
- 4f82f8
- git-author:
- Frederik Heber <heber@…> (10/03/12 10:47:16)
- git-committer:
- Frederik Heber <heber@…> (12/19/12 15:25:30)
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/FunctionApproximation/FunctionApproximation.cpp
r4f82f8 rc62f96 145 145 146 146 m = model.getParameterDimension(); 147 n = 1;147 n = output_data.size(); 148 148 const FunctionModel::parameters_t params = model.getParameters(); 149 p = new double(m); 150 x = new double(n); 151 size_t index = 0; 152 for(FunctionModel::parameters_t::const_iterator paramiter = params.begin(); 153 paramiter != params.end(); 154 ++paramiter, ++index) { 155 p[index] = params[index]; 156 } 157 x[0] = 0.; 149 { 150 p = new double[m]; 151 size_t index = 0; 152 for(FunctionModel::parameters_t::const_iterator paramiter = params.begin(); 153 paramiter != params.end(); 154 ++paramiter, ++index) { 155 p[index] = *paramiter; 156 } 157 } 158 { 159 x = new double[n]; 160 size_t index = 0; 161 for(outputs_t::const_iterator outiter = output_data.begin(); 162 outiter != output_data.end(); 163 ++outiter, ++index) { 164 x[index] = (*outiter)[0]; 165 } 166 } 158 167 159 168 { … … 214 223 } 215 224 216 delete p;217 delete x;225 delete[] p; 226 delete[] x; 218 227 } 219 228 … … 229 238 "FunctionApproximation::evaluate() - LevMar expects "+toString(m) 230 239 +" parameters but the model function expects "+toString(model.getParameterDimension())+"."); 231 ASSERT( n == 1, 232 "FunctionApproximation::evaluate() - we return precisely a single value, LevMar expects something else."); 240 ASSERT( (size_t)n == output_data.size(), 241 "FunctionApproximation::evaluate() - LevMar expects "+toString(n) 242 +" outputs but we provide "+toString(output_data.size())+"."); 233 243 FunctionModel::parameters_t params(m, 0.); 234 244 std::copy(p, p+m, params.begin()); … … 236 246 237 247 // then evaluate 238 if (! input_data.empty()) {248 if (!output_data.empty()) { 239 249 inputs_t::const_iterator initer = input_data.begin(); 240 250 outputs_t::const_iterator outiter = output_data.begin(); … … 251 261 x[index] = std::accumulate(differences.begin(), differences.end(), 0.); 252 262 } 253 } else { 254 for (register int i=0;i<n;++i) 255 x[i] = 0.; 256 } 257 } 263 } 264 } -
src/LevMartester.cpp
r4f82f8 rc62f96 55 55 #include "Fragmentation/Homology/HomologyContainer.hpp" 56 56 #include "Fragmentation/SetValues/Fragment.hpp" 57 #include "FunctionApproximation/FunctionApproximation.hpp" 58 #include "FunctionApproximation/FunctionModel.hpp" 59 #include "Potentials/Specifics/PairPotential_Harmonic.hpp" 57 60 58 61 namespace po = boost::program_options; … … 67 70 } 68 71 return HomologyGraph(); 69 }70 71 /* Meyer's (reformulated) problem, minimum at (2.48, 6.18, 3.45) */72 void meyer(double *p, double *x, int m, int n, void *data)73 {74 register int i;75 double ui;76 77 for(i=0; i<n; ++i){78 ui=0.45+0.05*i;79 x[i]=p[0]*exp(10.0*p[1]/(ui+p[2]) - 13.0);80 }81 }82 83 void jacmeyer(double *p, double *jac, int m, int n, void *data)84 {85 register int i, j;86 double ui, tmp;87 88 for(i=j=0; i<n; ++i){89 ui=0.45+0.05*i;90 tmp=exp(10.0*p[1]/(ui+p[2]) - 13.0);91 92 jac[j++]=tmp;93 jac[j++]=10.0*p[0]*tmp/(ui+p[2]);94 jac[j++]=-10.0*p[0]*p[1]*tmp/((ui+p[2])*(ui+p[2]));95 }96 72 } 97 73 … … 151 127 152 128 // Afterwards we go through all of this type and gather the distance and the energy value 153 typedef std::vector< std::pair<double, double> > DistanceEnergyVector_t; 154 DistanceEnergyVector_t DistanceEnergyVector; 129 typedef std::pair< 130 FunctionApproximation::inputs_t, 131 FunctionApproximation::outputs_t> InputOutputVector_t; 132 InputOutputVector_t DistanceEnergyVector; 155 133 std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range = 156 134 homologies.getHomologousGraphs(graph); … … 160 138 const Fragment::charges_t charges = fragment.getCharges(); 161 139 const Fragment::positions_t positions = fragment.getPositions(); 162 std::vector< Vector> DistanceVectors;140 std::vector< std::pair<Vector, size_t> > DistanceVectors; 163 141 for (Fragment::charges_t::const_iterator chargeiter = charges.begin(); 164 142 chargeiter != charges.end(); ++chargeiter) { … … 167 145 const size_t steps = std::distance(charges.begin(), chargeiter); 168 146 std::advance(positer, steps); 169 DistanceVectors.push_back(Vector((*positer)[0], (*positer)[1], (*positer)[2])); 147 DistanceVectors.push_back( 148 std::make_pair(Vector((*positer)[0], (*positer)[1], (*positer)[2]), 149 steps)); 170 150 } 171 151 } 172 ASSERT( DistanceVectors.size() == (size_t)2, 173 "main() - found not exactly two carbon atoms in fragment."); 174 const double distance = DistanceVectors[0].distance(DistanceVectors[1]); 175 const double energy = iter->second.second; 176 DistanceEnergyVector.push_back( std::make_pair(distance, energy) ); 152 if (DistanceVectors.size() == (size_t)2) { 153 argument_t arg; 154 arg.indices.first = DistanceVectors[0].second; 155 arg.indices.second = DistanceVectors[1].second; 156 arg.distance = DistanceVectors[0].first.distance(DistanceVectors[1].first); 157 const double energy = iter->second.second; 158 DistanceEnergyVector.first.push_back( FunctionModel::arguments_t(1,arg) ); 159 DistanceEnergyVector.second.push_back( FunctionModel::results_t(1,energy) ); 160 } else { 161 ELOG(2, "main() - found not exactly two carbon atoms in fragment " 162 << fragment << "."); 163 } 177 164 } 178 LOG(1, "INFO: I gathered the following " << DistanceEnergyVector.size() << " data pairs: "); 179 for (DistanceEnergyVector_t::const_iterator dataiter = DistanceEnergyVector.begin(); 180 dataiter != DistanceEnergyVector.end(); ++dataiter) { 181 LOG(1, "INFO: " << dataiter->first << " with energy " << dataiter->second); 165 // print training data for debugging 166 { 167 LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size() 168 << "," << DistanceEnergyVector.second.size() << ") data pairs: "); 169 FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin(); 170 FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin(); 171 for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) { 172 LOG(1, "INFO: (" << (*initer)[0].indices.first << "," << (*initer)[0].indices.second 173 << ") " << (*initer)[0].distance << " with energy " << *outiter); 174 } 182 175 } 183 176 // NOTICE that distance are in bohrradi as they come from MPQC! 184 177 185 // let levmar optimize 186 register int i, j; 187 int ret; 188 double p[5], // 5 is max(2, 3, 5) 189 x[16]; // 16 is max(2, 3, 5, 6, 16) 190 int m, n; 191 double opts[LM_OPTS_SZ], info[LM_INFO_SZ]; 178 PairPotential_Harmonic harmonic(1., 1.); 179 FunctionApproximation approximator(1, 1, harmonic); 180 approximator(); 181 const FunctionModel::parameters_t params = harmonic.getParameters(); 192 182 193 opts[0]=LM_INIT_MU; opts[1]=1E-15; opts[2]=1E-15; opts[3]=1E-20; 194 opts[4]= LM_DIFF_DELTA; // relevant only if the Jacobian is approximated using finite differences; specifies forward differencing 195 //opts[4]=-LM_DIFF_DELTA; // specifies central differencing to approximate Jacobian; more accurate but more expensive to compute! 196 197 m=3; n=16; 198 p[0]=8.85; p[1]=4.0; p[2]=2.5; 199 x[0]=34.780; x[1]=28.610; x[2]=23.650; x[3]=19.630; 200 x[4]=16.370; x[5]=13.720; x[6]=11.540; x[7]=9.744; 201 x[8]=8.261; x[9]=7.030; x[10]=6.005; x[11]=5.147; 202 x[12]=4.427; x[13]=3.820; x[14]=3.307; x[15]=2.872; 203 ret=dlevmar_der(meyer, jacmeyer, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian 204 205 { double *work, *covar; 206 work=(double *)malloc((LM_DIF_WORKSZ(m, n)+m*m)*sizeof(double)); 207 if(!work){ 208 fprintf(stderr, "memory allocation request failed in main()\n"); 209 exit(1); 210 } 211 covar=work+LM_DIF_WORKSZ(m, n); 212 213 ret=dlevmar_dif(meyer, p, x, m, n, 1000, opts, info, work, covar, NULL); // no Jacobian, caller allocates work memory, covariance estimated 214 215 printf("Covariance of the fit:\n"); 216 for(i=0; i<m; ++i){ 217 for(j=0; j<m; ++j) 218 printf("%g ", covar[i*m+j]); 219 printf("\n"); 220 } 221 printf("\n"); 222 223 free(work); 224 } 225 226 // { 227 // double err[16]; 228 // dlevmar_chkjac(meyer, jacmeyer, p, m, n, NULL, err); 229 // for(i=0; i<n; ++i) printf("gradient %d, err %g\n", i, err[i]); 230 // } 231 232 printf("Levenberg-Marquardt returned %d in %g iter, reason %g\nSolution: ", ret, info[5], info[6]); 233 for(i=0; i<m; ++i) 234 printf("%.7g ", p[i]); 235 printf("\n\nMinimization info:\n"); 236 for(i=0; i<LM_INFO_SZ; ++i) 237 printf("%g ", info[i]); 238 printf("\n"); 183 LOG(0, "RESULT: Best parameters are " << params[0] << " and " << params[1] << "."); 239 184 240 185 return 0; -
src/Makefile.am
r4f82f8 rc62f96 482 482 LevMartester_LDFLAGS = $(AM_LDFLAGS) $(BOOST_SERIALIZATION_LDFLAGS) $(BOOST_PROGRAM_OPTIONS_LDFLAGS) $(BOOST_FILESYSTEM_LDFLAGS) $(BOOST_SYSTEM_LDFLAGS) ${CodePatterns_LDFLAGS} $(LEVMAR_LDFLAGS) 483 483 LevMartester_LDADD = \ 484 libMolecuilderPotentials.la \ 485 libMolecuilderFunctionApproximation.la \ 484 486 libMolecuilderFragmentation.la \ 485 487 libMolecuilderFragmentation_getFromKeysetStub.la \
Note:
See TracChangeset
for help on using the changeset viewer.