watchmaker_vm

A virtual machine for use with genetic algorithms.

The virtual machine has an instruction set that is much simpler than real world processors. There are no registers and all operands are memory locations. This has the benefits that instructions are easier to interpret with less need to evolve needless complexity around register usage.

CircleCI

Features

Usage

Example

The following creates an example that executes a factorial function. ```rust let vm = VirtualMachine::new( &ArchitectureBuilder::default() .iinput(1) .istate(2) .ioutput(1) .dinput(1) .dstate(1) .doutput(1) .build() .unwrap(), vec![ Instruction::IIMOV( LeftInteger::Input(0, Mode::Direct), RightInteger::State(0, Mode::Direct), ), Instruction::IIMOV( LeftInteger::Input(0, Mode::Direct), RightInteger::State(1, Mode::Direct), ), Instruction::IJLT( LeftInteger::State(0, Mode::Direct), LeftInteger::Constant(2), CodeOffset { offset: 4 }, ), Instruction::ISUB( LeftInteger::State(0, Mode::Direct), LeftInteger::Constant(1), RightInteger::State(0, Mode::Direct), ), Instruction::IMUL( LeftInteger::State(0, Mode::Direct), LeftInteger::State(1, Mode::Direct), RightInteger::State(1, Mode::Direct), ), Instruction::IJEQ( LeftInteger::Constant(0), LeftInteger::Constant(0), CodeOffset { offset: -3 }, ), Instruction::IIMOV( LeftInteger::State(1, Mode::Direct), RightInteger::Output(0, Mode::Direct), ), Instruction::HLT(), ], ); // Write the input to the first location in the integer typed input memory bank. vm.iinput()[0] = n as i64;

// Execute the VM until the halt instruction is reached.
while vm.next_instruction() != &Instruction::HLT() {
    vm.run(1);
}

// Read the result from the first location of the integer typed output memory bank.
let result = vm.ioutput()[0];
println!("factorial of {:?} is {:?}", vm.iinput()[0], result);

```

Alternatives

The Genetic Algorithm is a very well known technique and as a result there are many alternative ways of representing and executing evolved programs.

Some candidates:

License

MIT permissive license. See LICENSE for full license details.

Source Code Repository

https://github.com/thomasbratt/watchmaker_vm