An Evolver for Core War, written in Python. You don't need to know Redcode (the language used in Core War) to use this tool.
This project uses a genetic algorithm to pit warriors against each other, breed the winners, and introduce mutations to evolve superior strategies over time.
Contributions are welcome! If you have a suggestion or improvement, please submit a pull request.
Before running the evolver, you need:
- Python 3.x: This project requires Python 3 to run.
- nMars: This is the simulator that runs the battles.
- Download: Get the latest version from SourceForge.
- Windows: Download
nmars.exeand put it in the same folder as this project. - Linux/macOS: Download
nmarsand put it in the same folder, or install it so it runs from your terminal.
Settings are in settings.ini. Open this file to change how the evolution works.
- Arenas: You can run multiple arenas with different rules (like size or cycles).
- Important: All list settings (like
CORESIZE_LIST,CYCLES_LIST) must be the same length. - Update
LAST_ARENAto match the index of your last arena. For example, if you have 8 arenas (0 to 7), setLAST_ARENA=7.
- Important: All list settings (like
- Time: Set
CLOCK_TIMEto how many hours you want the script to run. - Starting Fresh vs. Continuing:
- Start New: Set
ALREADYSEEDED = False. This creates random warriors to start. - Resume: Set
ALREADYSEEDED = True. This keeps your current warriors and continues evolving them.
- Start New: Set
- Optimization: Set
FINAL_ERA_ONLY = True(andALREADYSEEDED = True) to skip the chaotic early phases and just fine-tune your warriors.
-
Check
settings.inito make sure it's set up how you want. -
Open your terminal or command prompt in the project folder.
-
Validate your setup: Before starting a long run, check that everything is correct (settings, file paths, simulator).
python evolverstage.py --check
If it says "Configuration and environment are valid," you are good to go.
-
Run the script:
python evolverstage.py
- Tip: To see the exact settings the script is using (including defaults), run:
python evolverstage.py --dump-config
- Tip: To see the exact settings the script is using (including defaults), run:
-
Watch the Progress: You will see output like this:
8.00 hours remaining (0.01% complete) Era: 1 -
Get Results: When it's done (or if you stop it), look in the
arenaXfolders (likearena0) for the.redfiles. These are your evolved warriors. -
Find the Best: Use a benchmark tool (like CoreWin) to test the final warriors against each other to find the champion.
- Arenas (
arena0/,arena1/, ...): The current population. Each file (e.g.,15.red) is a warrior. - Archive (
archive/): A backup of winning warriors. This saves good strategies so they aren't lost.
If you set a filename for BATTLE_LOG_FILE in settings.ini, the script saves every battle result to a CSV file.
- era: Evolution phase (0, 1, or 2).
- arena: Which arena the fight happened in.
- winner/loser: The IDs of the warriors.
- score1/score2: The score from nMars.
- bred_with: The ID of the partner warrior used to make the new warrior.
To ensure the code is working correctly, you can run the included test suite. This is recommended if you plan to modify the code.
- Install pytest:
pip install pytest
- Run the tests:
The tests verify the logic for evolution, parsing, and battle execution (using a mock simulator), so you don't need
pytest
nmarsinstalled just to run them.
- Multi-Arena: Warriors can fight in different environments at the same time. Code that works well in one arena can be "sanitized" (adjusted) and moved to another.
- Smart Selection: The evolver prefers useful instructions (like
MOVandSPL) and efficient numbers. - Eras: Evolution happens in three stages:
- Exploration (Era 1): High mutation. Tries many different things.
- Breeding (Era 2): Winners breed more often. Combines good parts of different warriors.
- Optimization (Era 3): Fine-tuning. Changes small numbers to perfect the code.
- Mutation Strategies: The "Bag of Marbles" system randomly picks how to change a warrior:
- Do Nothing: Keep it the same.
- Major Mutation: Create a totally new random instruction.
- Nab Instruction: Steal code from another arena.
- Mini/Micro Mutation: Change a small part of an instruction.
- Magic Number: Use a specific number known to be good.