Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. export class LazyGuy {
  2. constructor(components) {
  3. this.components = components;
  4. this.leftOverComponents = [];
  5.  
  6. this.environment = {
  7. maxPageSize: 1016,
  8. marginOfComfort: 10
  9. };
  10.  
  11. }
  12.  
  13.  
  14. /**
  15. * Creates a generation of possible page options
  16. *
  17. * @param {number} individuals - Number of individuals in generation
  18. */
  19.  
  20. createGeneration(individuals) {
  21. let generation = [];
  22.  
  23. for (let i = 0; i < individuals; i++){
  24. generation.push( this.getPage(this.components.slice(0)) );
  25. }
  26.  
  27. return generation;
  28. }
  29.  
  30.  
  31. /**
  32. * Get the fitness of an entire generation
  33. *
  34. * @param {Object[]} generation - Generation to get the fitness for
  35. */
  36.  
  37. generationFitness(generation) {
  38. let summed = generation.reduce((prev, current) => {
  39. return this.pageFitness(prev) + this.pageFitness(current);
  40. }, 0);
  41.  
  42. return summed / generation.length;
  43. }
  44.  
  45.  
  46. /**
  47. * Creates a page full of components
  48. *
  49. * @param {number[]} components - Components to create a page from
  50. */
  51.  
  52. getPage(components) {
  53. let page = { size: 0, parts: [] },
  54. comfortablePageSize = this.environment.maxPageSize - this.environment.marginOfComfort;
  55.  
  56. while (page.size < comfortablePageSize && components.length > 0){
  57. let randomComponent = this.getRandomComponent(components);
  58. page.parts.push(randomComponent.part);
  59. page.size += randomComponent.part;
  60.  
  61. components.splice(randomComponent.index, 1);
  62. }
  63.  
  64. return page;
  65. }
  66.  
  67.  
  68. /**
  69. * Get the fitness of a page
  70. *
  71. * @param {number} size - Page size to get fitness for
  72. */
  73.  
  74. pageFitness(size) {
  75. return Math.abs(this.environment.maxPageSize - size);
  76. }
  77.  
  78.  
  79. /**
  80. * Get a random component from list
  81. *
  82. * @param {number[]} components - Components to choose from
  83. */
  84.  
  85. getRandomComponent(components) {
  86. let max = components.length,
  87. randomIndex = Math.floor(Math.random() * (max - 1));
  88.  
  89. return { index: randomIndex, part: components[randomIndex] };
  90. }
  91.  
  92.  
  93. /**
  94. * Generates a population from the genetically inferior members
  95. * of the generation based on a small chance
  96. *
  97. * @param {Object[]} inferiorIndividuals - The "losing" end of the current generation
  98. * @param {number} inferiorityRetention - Number of probability to decide inclusion
  99. */
  100.  
  101. inferiorPopulation(inferiorIndividuals, inferiorityRetention) {
  102. let inferiors = [];
  103.  
  104. for (let individual of inferiorIndividuals){
  105. if (inferiorityRetention > Math.random()){
  106. inferiors.push(individual);
  107. }
  108. }
  109.  
  110. return inferiors;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement