Help me learn autonomous simulation
March 3, 2010 9:11 AM   Subscribe

I would like to write some basic software that, without any user input at all, simulates an event such as a crew traveling in space. What do I need to learn in order to get from the "if launch was successful then print 'CONGRATULATIONS'" stage to the "if crew member #3 is depressed and she is co-pilot, then set $mistake_likelihood higher" level of detail?

Extended version: I'm sick of having a bunch of widgets on my desktop that show me the weather, the current time, same old news, etc.

I want to get news from a virtual crew that is headed to another planet.

So the point I'd like to eventually get to is:

0. I have a relatively featureful software simulation written -- featureful enough that running the simulation isn't an exercise in deja vu every time.

1. It runs as a web app that uses cron or similar to update the simulation "mission control" dashboard website once an hour or so.

2. The software creates RSS and Twitter updates from my simulated crew (for aforementioned widgets).

If I go with my current level of expertise, this will be like one huge list of ifs and elses. Is that the way to go? I get the sense that a database would be helpful for storing data, but know only the basics about DBs.

So...advice? Suggested reading? Have you done this before?

My experience in this area is limited to short "choose your own adventure" games, but I'm willing to learn more, and in fact I need to, as becoming good at software development is a long-term goal of mine.

My background is in web design (and limited development) and CG art/illustration. I've worked with about 15-20 programming languages on a rudimentary level, and taken up to mid-200-level CS classes (stopping short of data structures).

Thanks for your input! I'm dying to put this into action...my first plan was to make this into a little gkrellm plugin, if that gives you an idea of how long it's been simmering. ;-)
posted by circular to Computers & Internet (10 answers total) 14 users marked this as a favorite
 
Best answer: Sounds like a fun project! I'm not sure what your level of programming abilities is, so forgive me if this is needlessly simple...

You didn't specify any particular programming language you want to use, but I recommend PHP because it's free, easy to transition to if you're already familiar with (X)HTML, and the documentation is some of the best I've seen anywhere. This tutorial is what I used to get my feet wet with the language and I think it's great. I'm sure others will chime in to dissuade you from using PHP; it has its detractors but I'm personally a big fan of it. If you need a database, PHP plays nicely with MySQL, which is also free and easy to learn.

I can't address all of your questions but I will say that you'll probably want to do a fair amount of random number generation to keep things unpredictable. PHP's mt_rand() will come in handy.

Tutorials for generating RSS with PHP are a dime a dozen. I recommend you pursue that, and then run the feed through TwitterFeed to tweet updated.

You will probably have a lot of if-statements; You may find that using switch() instead makes things more readable. Also, arrays are your friend. Get very familiar with how they work!
posted by The Winsome Parker Lewis at 9:33 AM on March 3, 2010 [1 favorite]


If you don't know much about data structures, you might want to try mucking around with something like Inform. It's free, the documentation is good, and the language is intuitive, almost like typing English sentences. With Inform, you'll be able to time delay things, set up actions to happen randomly and implement that big if-else tree you're thinking of. Inform is set up to be interactive, so you'll have to do some tweaking to get it to behave as you're imagining. Still, it has the potential to be a lot more powerful than just a "Choose Your Own Adventure" type of experience. At the very least, you'll be able to test out your ideas before committing to something requiring a lot more resources.

The tough part will be getting it plugged in as a continually updating web-app, but it wouldn't be impossible.
posted by Alison at 9:39 AM on March 3, 2010


Best answer: Unless the app is going to respond to events that occur in the real world, there's no need to tie the app directly to the web interface or run it in real time. You could just as easily write a program that produces all of the output at once, then write a second program that posts the output to the web.

In addition to letting you cleanly separate the internal logic of the program from the interface, this approach lets you pick the right language for each job. It also lets you review and tweak all of the output before displaying it so you don't get something like "Day 1: Due to an extremely unlikely massive equipment failure, everyone has died" posted.
posted by jedicus at 10:05 AM on March 3, 2010


Best answer: *"updated" = "updates" in the second-to-last paragraph of my earlier post.

Chiming back in with more info regarding databases. Let's brainstorm table structure. Here's how I might lay out the database initially (though it would likely change during development):

"Crew" Table:
  • Crew_ID
  • First_Name
  • Last_Name
  • Nickname
  • Title
  • Rank
  • Gender
  • Age
  • Hometown
  • Emotional_Stability
  • Work_Initiative
"Crew_Fieldofinterest" Table:
  • Crew_Fieldofinterest_ID
  • Crew_ID
  • Fieldofinterest_Name
"Mood" Table:
  • Mood_Name
  • Morale_Effect
  • Avg_Duration
"Relationship" Table:
  • Relationship_ID
  • POV_Crew_ID
  • Regarding_Crew_ID
  • Friendship_Level
  • Cooperation_Level
"Activity" Table:
  • Activity_ID
  • Activity_Name
  • Is_Fieldofinterest_Specific
  • Fieldofinterest_Name
  • Morale_Effect
  • Avg_Duration
  • Is_Work
"Mission_Plan" Table:
  • Sequence_ID
  • Location_ID
  • Expected_Duration
  • Morale_Effect
"MissionPlan_Activity" Table:
  • MissionPlan_Activity_ID
  • MissionPlan_Sequence_ID
  • Activity_ID
  • Time_Required_To_Complete
"Crew_Status" Table
  • Crew_Status_ID
  • Crew_ID
  • Mood
  • Activity
  • Morale
  • Time_Begun
  • Time_Ended
I would also have vocab tables for generating text, one table for each part of speech (noun, adjective, verb, adverb). Your code can pull from these as needed like Mad Libs. Associate certain words with moods, morale levels, locations, or tasks to generate context-appropriate output.

Actually this is pretty incomplete but may give you a good starting place. :-)
posted by The Winsome Parker Lewis at 10:18 AM on March 3, 2010 [1 favorite]


Thinking some more on your original question, it could be fun if you added "Skill" and "Resourcefulness" columns to the Crew table, for calculating likelihood of mistakes and ability to fix them quickly when crap does happen.
posted by The Winsome Parker Lewis at 10:25 AM on March 3, 2010


Best answer: You say your goal is to become a good software developer, rather than to get something simple working ASAP. In this case, I recommend learning object-oriented programming. There was a while where OOP was marketed as a solution to all programming problems (it's not), and there was backlash since then, but it is a great fit for this kind of simulation.

You already have the sense that a big list of if statements isn't a good idea. The OOP answer is polymorphism. The basic idea is that instead of using an explicit if statement to check for some condition, you write a single statement, and the runtime system knows what to do based on the type of objects involved. It's more involved than that, and it will take some reading and time to understand, but you're smart.

There isn't much OOP in the stuff I work on, so I'll leave specific book/language recommendations for others to make.
posted by domnit at 10:33 AM on March 3, 2010


This sounds pretty neat. If you can get it to the stage where a user can tweak the attributes of a virtual crew, and then receive news of the mission online/on the desktop over the course of the mission throughout days and weeks, then I think a lot of people would be interested in this as a neat little distraction.

I don't have much to contribute in terms of accomplishing this, but if you finish it and you remember, me-mail me with where I can get a copy!
posted by Diplodocus at 12:08 PM on March 3, 2010


Best answer: I think the Mars Simulation, a simulation of society settling on Mars, has covered most of the same ground (characters have fatigue, hunger, morale, etc). Perhaps you could focus on creating a widget for this, and then extend/adapt the underlying program to cover life in a spaceship instead of on Mars.
posted by jacalata at 12:29 PM on March 3, 2010 [3 favorites]


Best answer: I agree completely with jedicus's point that the web app should almost be an afterthought here.

Let's think about persistent data first- you've got:

The Trip: How far along am I?
The Ship: How much fuel/supplies/oxygen/whatever am I carrying, and what is my state of repair?
The Crew: How healthy/happy is everyone?

At a basic level, your app needs to incorporate a daemon/service that does roughly the following at regular intervals:

- Move the ship (decrease fuel, randomly decrease state of repair). Update the Trip, check if you're there yet.
- Check the health of the ship. Oxygen, for example, may run out more quickly if repair falls below X%. If some crew member has the ability, and is above some health/happiness threshold, increase state of repair.
- Reduce supplies to simulate use. If supplies run out, affect the crew's health/happiness accordingly.
- Publish updates (feeds, web pages, etc.)

From there, you can bolt on "events" that can happen either randomly (food spoils, people get sick) or on some kind of schedule (passing through an asteroid belt at point X requires more fuel).

Don't get too hung up on making some kind of highly abstracted data model to handle rules and events. It's extensibility you don't need- leave that kind of logic in code. Use the DB to store your updated data after each interval.
posted by mkultra at 1:45 PM on March 3, 2010


Response by poster: jacalata: HOLY cow. That simulation is awesome! Even breaks down people into MBTI personality types...! After using it for a while though, I've realized how much importance I do (and they don't) place on narrative and even plot development. Hmmm.

I'm going to have to think about some simple things I could do to make my projects run a bit more like a story. Maybe focusing on larger-scale events could help that.

I have a few more ideas in this area (like a screensaver that simulates C3I infrastructure during a natural disaster by expanding to use any number of available monitors across a network and assigning them roles like "community calendar TV channel," "NORAD desktop," "NOAA network administrator," etc.) and so I need to move forward in learning.

diplodocus: I will try to remember to keep you posted :-)

mkultra: Thanks for your warning about the highly-abstracted data model. I'm just the type to obsess over that.

domnit: Ah, I was afraid OOP might provide an answer to this. :-( I have a terrible history with it.
Love the concept.

Alison: I like Inform, but I don't think it's in the cards for this project. I will be using it for other projects, though.

The Winsome Parker Lewis: Your help with potential DB tables is appreciated.
posted by circular at 5:10 PM on March 3, 2010


« Older calling under a moonlit sky.. oh brazil   |   Fame and fortune, here I come! Newer »
This thread is closed to new comments.