Skip to content

Commit

Permalink
Add bxt_tas_optim_change_pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Jul 24, 2023
1 parent a02a86c commit cde8ec6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/modules/tas_optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Module for TasOptimizer {
static CVARS: &[&CVar] = &[
&BXT_TAS_OPTIM_RANDOM_FRAMES_TO_CHANGE,
&BXT_TAS_OPTIM_CHANGE_SINGLE_FRAMES,
&BXT_TAS_OPTIM_CHANGE_PITCH,
&BXT_TAS_OPTIM_FRAMES,
&BXT_TAS_OPTIM_SIMULATION_ACCURACY,
&BXT_TAS_OPTIM_MULTIPLE_GAMES,
Expand Down Expand Up @@ -125,6 +126,13 @@ Generally `0` gives better results and produces a script ready to be copy-pasted
useful for fine-tuning, e.g. if you're very close but barely not making the jump.",
);

static BXT_TAS_OPTIM_CHANGE_PITCH: CVar = CVar::new(
b"bxt_tas_optim_change_pitch\0",
b"0\0",
"\
When set to `1`, the optimizer will mutate the pitch angle on the frame bulks where it is set.",
);

static BXT_TAS_OPTIM_SIMULATION_ACCURACY: CVar = CVar::new(
b"bxt_tas_optim_simulation_accuracy\0",
b"0\0",
Expand Down Expand Up @@ -831,6 +839,7 @@ pub fn draw(marker: MainThreadMarker, tri: &TriangleApi) {
BXT_TAS_OPTIM_FRAMES.as_u64(marker) as usize,
BXT_TAS_OPTIM_RANDOM_FRAMES_TO_CHANGE.as_u64(marker) as usize,
BXT_TAS_OPTIM_CHANGE_SINGLE_FRAMES.as_bool(marker),
BXT_TAS_OPTIM_CHANGE_PITCH.as_bool(marker),
&OBJECTIVE.borrow(marker),
|value| {
con_print(marker, &format!("Found new best value: {value}\n"));
Expand All @@ -852,6 +861,7 @@ pub fn draw(marker: MainThreadMarker, tri: &TriangleApi) {
BXT_TAS_OPTIM_FRAMES.as_u64(marker) as usize,
BXT_TAS_OPTIM_RANDOM_FRAMES_TO_CHANGE.as_u64(marker) as usize,
BXT_TAS_OPTIM_CHANGE_SINGLE_FRAMES.as_bool(marker),
BXT_TAS_OPTIM_CHANGE_PITCH.as_bool(marker),
&OBJECTIVE.borrow(marker),
) {
let start = Instant::now();
Expand Down
36 changes: 28 additions & 8 deletions src/modules/tas_optimizer/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl Optimizer {
frames: usize,
random_frames_to_change: usize,
change_single_frames: bool,
change_pitch: bool,
objective: &'a Objective,
) -> Option<impl Iterator<Item = AttemptResult> + 'a> {
self.simulate_all(tracer);
Expand All @@ -185,10 +186,10 @@ impl Optimizer {
let frame = if change_single_frames {
// Pick a random frame and mutate it.
let frame = between.sample(&mut rng);
mutate_frame(&mut rng, &mut hltas, frame);
mutate_frame(change_pitch, &mut rng, &mut hltas, frame);
frame
} else {
mutate_single_frame_bulk(&mut hltas, &mut rng)
mutate_single_frame_bulk(change_pitch, &mut hltas, &mut rng)
};

stale_frame = stale_frame.min(frame);
Expand Down Expand Up @@ -287,6 +288,7 @@ impl Optimizer {
frames: usize,
random_frames_to_change: usize,
change_single_frames: bool,
change_pitch: bool,
objective: &Objective,
mut on_improvement: impl FnMut(&str),
) {
Expand Down Expand Up @@ -338,9 +340,9 @@ impl Optimizer {
if change_single_frames {
let frame = between.sample(&mut rng);
let frame_bulk = self.hltas.split_single_at_frame(frame).unwrap();
mutate_frame_bulk(&mut rng, frame_bulk);
mutate_frame_bulk(change_pitch, &mut rng, frame_bulk);
} else {
mutate_single_frame_bulk(&mut self.hltas, &mut rng);
mutate_single_frame_bulk(change_pitch, &mut self.hltas, &mut rng);
}
}

Expand Down Expand Up @@ -479,7 +481,7 @@ impl Optimizer {
}
}

fn mutate_frame<R: Rng>(rng: &mut R, hltas: &mut HLTAS, frame: usize) {
fn mutate_frame<R: Rng>(change_pitch: bool, rng: &mut R, hltas: &mut HLTAS, frame: usize) {
if frame > 0 {
let l = hltas.line_and_repeat_at_frame(frame).unwrap().0;
let frame_bulk = hltas.split_at_frame(frame).unwrap();
Expand All @@ -493,10 +495,10 @@ fn mutate_frame<R: Rng>(rng: &mut R, hltas: &mut HLTAS, frame: usize) {
// Split it into its own frame bulk.
let frame_bulk = hltas.split_single_at_frame(frame).unwrap();

mutate_frame_bulk(rng, frame_bulk);
mutate_frame_bulk(change_pitch, rng, frame_bulk);
}

fn mutate_frame_bulk<R: Rng>(rng: &mut R, frame_bulk: &mut FrameBulk) {
fn mutate_frame_bulk<R: Rng>(change_pitch: bool, rng: &mut R, frame_bulk: &mut FrameBulk) {
let p = rng.gen::<f32>();
let strafe_type = if p < 0.01 {
StrafeType::MaxDeccel
Expand All @@ -518,9 +520,13 @@ fn mutate_frame_bulk<R: Rng>(rng: &mut R, frame_bulk: &mut FrameBulk) {

mutate_action_keys(rng, frame_bulk);
mutate_auto_actions(rng, frame_bulk);

if change_pitch {
mutate_pitch(rng, frame_bulk);
}
}

fn mutate_single_frame_bulk<R: Rng>(hltas: &mut HLTAS, rng: &mut R) -> usize {
fn mutate_single_frame_bulk<R: Rng>(change_pitch: bool, hltas: &mut HLTAS, rng: &mut R) -> usize {
let count = hltas.frame_bulks().count();
let index = rng.gen_range(0..count);
let frame_bulk = hltas.frame_bulks_mut().nth(index).unwrap();
Expand Down Expand Up @@ -595,6 +601,10 @@ fn mutate_single_frame_bulk<R: Rng>(hltas: &mut HLTAS, rng: &mut R) -> usize {
mutate_action_keys(rng, frame_bulk);
mutate_auto_actions(rng, frame_bulk);

if change_pitch {
mutate_pitch(rng, frame_bulk);
}

// Mutate frame count.
if index + 1 < count {
let frame_time = frame_bulk.frame_time.clone();
Expand Down Expand Up @@ -696,4 +706,14 @@ fn mutate_auto_actions<R: Rng>(rng: &mut R, frame_bulk: &mut FrameBulk) {
}
}

fn mutate_pitch<R: Rng>(rng: &mut R, frame_bulk: &mut FrameBulk) {
if let Some(pitch) = frame_bulk.pitch.as_mut() {
if rng.gen::<f32>() < 0.05 {
*pitch = rng.gen_range(-89f32..89f32)
} else {
*pitch += rng.gen_range(-1f32..1f32)
};
}
}

// proptest: after simulating, self.frames.len() = frame count + 1

0 comments on commit cde8ec6

Please sign in to comment.