Resource/process scheduling simulator for the 42 krpsim project.
krpsim is a resource/process scheduling simulator.
A configuration describes:
- initial stocks;
- processes with required resources (
needs); - delayed results (
results); - process duration;
- optimization targets.
The engine simulates process execution over cycles. Resources are consumed when a process starts, and results are produced only when the process finishes.
Implemented:
- core data structures (
Config,Process,SimulationState,Event,Trace); - simulator primitives;
- trace verifier;
- config parser for resource files;
- trace parser for verifier input;
- naive solver;
- target-plan solver for simple dependency chains;
- greedy score-based solver;
- beam-search solver;
- solver result comparison with
solveBest; - custom resource files and verifier traces for demonstrations.
Config
|
v
Simulator <--- Solver
| |
| v
| Trace
| |
v v
State Verifier
The simulator is the source of truth:
- a solver generates a
Trace; - the verifier replays a
Trace; - the parser builds a
Configfrom a resource file.
When a process starts:
- all resources from
needsare removed immediately from stocks; - no result is produced immediately;
- an event is created at
current_cycle + duration; - the solver records a trace action:
<cycle>:<process_name>.
When an event completes:
- all resources from
resultsare added to stocks; - the event is removed from the running events.
Resources such as tools or workers are not special cases. If a process needs
four:1 and returns four:1, the resource is simply unavailable during the
process duration.
makeThis builds:
krpsimkrpsim_verif
Object and dependency files are kept in build/.
Useful commands:
make run
make verif
make clean
make rekrpsim accepts a resource file and a maximum cycle:
./krpsim resources/simple 100
./krpsim resources/ikea 100The generated trace is printed on stdout in the subject format:
0:achat_materiel
10:realisation_produit
40:livraison
The solver name, final cycle and final stocks are printed on stderr so stdout can be redirected directly into the verifier.
krpsim_verif accepts a resource file and a trace file:
./krpsim_verif resources/simple trace.txtVerifier fixtures are available in traces/.
Build and run a simple example:
make runThis runs:
./krpsim resources/simple 100Running without arguments now prints usage and exits with an error, matching the subject CLI expectation.
Mock configs still exist for development in src/mocks/MockConfigs.cpp:
krpsim::mock::simpleDemo()
krpsim::mock::ikeaDemo()
krpsim::mock::steakDemo()Available mock configs:
simpleDemo(): linear chain, best first test;ikeaDemo(): branching production and assembly;steakDemo(): reusable blocking tool (poele);recreDemo(): empty results and potentially infinite production;pommeDemo(): larger economy/production loop;inceptionDemo(): complex recursive time-production system.
The recommended test order is:
simple -> ikea -> steak -> recre -> pomme -> inception
The config parser currently supports:
- comments starting with
#; - initial stocks with
name:quantity; - processes with
name:(needs):(results):duration; - empty result groups, for example
manger:(bonbon:1)::10; optimize:(time;resource)targets.
The trace parser supports lines in the form:
cycle:process_name
Current solver functions:
solveNaive: launches the first startable process.solveTargetPlan: starts from the first optimized resource, builds a simple dependency plan, then executes the required processes.solveGreedyByScore: scores every startable process at each cycle using the optimized resources and their dependencies.solveBeamSearch: explores several promising traces and keeps the best beam states.solveBest: runs multiple solver strategies and keeps the best result using the configuredoptimizetargets.
SimulationResult includes the selected solver name, printed on stderr by
krpsim.
Example: for resources/ikea, solveTargetPlan can build the exact component
plan needed for one armoire:
0:do_montant
0:do_montant
0:do_fond
0:do_etagere
0:do_etagere
0:do_etagere
20:do_armoire_ikea
The verifier checks whether a trace is legal for a given config.
Example valid trace for simple:
0:achat_materiel
10:realisation_produit
40:livraison
The verifier:
- starts from the initial state;
- advances events up to each trace action cycle;
- checks that the requested process exists;
- checks that the process can start;
- starts the process;
- completes remaining events at the end.
It returns a VerificationResult with:
ok;lastCycle;finalStocks;errorMessage.
make fclean
make
./krpsim resources/simple 100 > /tmp/simple.trace
./krpsim_verif resources/simple /tmp/simple.trace
./krpsim resources/ikea 100 > /tmp/ikea.trace
./krpsim_verif resources/ikea /tmp/ikea.traceAdditional finite and infinite demonstration configs are in resources/.
Invalid verifier traces are in traces/.