Skip to content

Instantly share code, notes, and snippets.

@lifedispenser
Last active March 14, 2020 20:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifedispenser/3a9a3433551a324097829e9bedc1132a to your computer and use it in GitHub Desktop.
Save lifedispenser/3a9a3433551a324097829e9bedc1132a to your computer and use it in GitHub Desktop.
class Person extends IdyllModel
belongs_to :working_population
define_attribute :stress, type: integer, min: 0, max: 100, init: random
define_attribute :environment, type: enum, ['high income', 'low income', 'normal']
define_attribute :group, type: classification, |model| => { case model.stress; when 50..100: "high risk"; when 0..49: "low risk" }
def next_step
self.stress += 5 if self.environment == "high income"
self.stress -= 5 if self.environment == "low income"
end
end
class WorkingPopulation extends IdyllPopulation
populate_with :persons, amount: 100
is_step_function step_unit: "years"
def next_step
persons.each do |p|
p.environment = "normal" if chance(.03)
p.environment = "high income" if chance(.01)
end
end
end
@base_model = new WorkingPopulation()
@base_model.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment