Modern condensed-matter theory from first principles is highly successful when applied to materials of given structure-type or restricted unit-cell size. But this approach is limited where large cells or searches over millions of structure types become necessary. To treat these with first-principles accuracy, one 'coarse-grains' the many-particle Schrodinger equation into 'model hamiltonians' whose variables are configurational order parameters (atomic positions, spin and so on), connected by a few 'interaction parameters' obtained from a microscopic theory. But to construct a truly quantitative model hamiltonian, one must know just which types of interaction parameters to use, from possibly 10(6)-10(8) alternative selections. Here we show how genetic algorithms, mimicking biological evolution ('survival of the fittest'), can be used to distil reliable model hamiltonian parameters from a database of first-principles calculations. We demonstrate this for a classic dilemma in solid-state physics, structural inorganic chemistry and metallurgy: how to predict the stable crystal structure of a compound given only its composition. The selection of leading parameters based on a genetic algorithm is general and easily applied to construct any other type of complex model hamiltonian from direct quantum-mechanical results.