Skip to content
Permalink
Browse files

Pick up best goal

  • Loading branch information...
ichyo committed Jun 23, 2019
1 parent f9d5013 commit 1b7790f8370d60715bf4f2430820800df3229680
Showing with 53 additions and 15 deletions.
  1. +1 −1 make_solution.sh
  2. +12 −4 src/main.rs
  3. +40 −10 src/solve.rs
@@ -15,7 +15,7 @@ echo "$(date +"%Y-%m-%d %T"): Creating $OUTPUT" | tee -a $LOG_FILE
mkdir -p $OUTPUT

SECONDS=0
./target/release/icfpc2019 --output $OUTPUT --input ./input
./target/release/icfpc2019 --output $OUTPUT --input ./input --duration 30000
echo "$(date +"%Y-%m-%d %T"): running time is $SECONDS secs" | tee -a $LOG_FILE

echo "$(date +"%Y-%m-%d %T"): Creating zip $OUTPUT/solutions.zip" | tee - a $LOG_FILE
@@ -7,10 +7,10 @@ use std::io::Write;
use icfpc::models::*;
use icfpc::parse::read_all_inputs;
use icfpc::solve::solve_small_while;
use std::time::Duration;

fn solve<W: Write>(task: Task, f: &mut W) {
// 30 seconds -> 10 minutes in total
let cmds = solve_small_while(task, std::time::Duration::from_millis(30000));
fn solve<W: Write>(task: Task, f: &mut W, duration: Duration) {
let cmds = solve_small_while(task, duration);
write!(f, "{}", cmds).unwrap();
}

@@ -29,10 +29,18 @@ fn main() {
.takes_value(true)
.help("output directory"),
)
.arg(
Arg::with_name("duration")
.long("duration")
.takes_value(true)
.help("millis to wait")
)
.get_matches();

let input_root = matches.value_of("input").expect("no input specified");
let output_root = matches.value_of("output");
let millis = matches.value_of("period").unwrap_or("300").parse::<u64>().unwrap();
let duration = Duration::from_millis(millis);

let inputs = read_all_inputs(&input_root);
let progress_bar = ProgressBar::new(inputs.len() as u64);
@@ -45,7 +53,7 @@ fn main() {
}
None => Box::new(std::io::stdout()),
};
solve(input.task, &mut output_file);
solve(input.task, &mut output_file, duration);
progress_bar.inc(1);
});
progress_bar.finish();
@@ -1,8 +1,7 @@
use crate::models::*;
use crate::utils::Matrix;

use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::prelude::*;
use std::collections::{HashMap, VecDeque};
use std::time::Duration;
use std::time::Instant;
@@ -223,6 +222,18 @@ impl<'a> State<'a> {
is_valid && (not_passed || is_booster)
}

fn count_pass(&self, robot_idx: usize, point: Point) -> usize {
self.robots[robot_idx]
.bodies_diff
.iter()
.map(|diff| point + *diff)
.filter(|p| match self.passed.get(*p) {
Some(false) => true,
_ => false,
})
.count()
}

fn find_shortest_path(&self, robot_idx: usize, start: Point) -> Option<Vec<Move>> {
let mut rng = thread_rng();
let mut moves = [
@@ -236,19 +247,26 @@ impl<'a> State<'a> {
let mut queue = VecDeque::new();
queue.push_back(start);
data.insert(start, (Move::Noop, 0));

let mut goal = None;
let mut goal_value = None;

while let Some(c) = queue.pop_front() {
let (_, cost) = data[&c];

if self.is_goal(robot_idx, c) {
let mut res = Vec::new();
let mut iter = c;
while iter != start {
let (mv, _) = &data[&iter];
iter = iter.revert_with(mv);
res.push(mv.clone());
let value = (self.count_pass(robot_idx, c), rng.gen::<usize>());
match goal_value {
Some(goal_value) if goal_value > value => {}
_ => {
goal = Some(c);
goal_value = Some(value);
}
}
res.reverse();
return Some(res);
}

if goal.is_some() {
continue;
}

moves.shuffle(&mut rng);
@@ -263,6 +281,18 @@ impl<'a> State<'a> {
}
}

if let Some(goal) = goal {
let mut res = Vec::new();
let mut iter = goal;
while iter != start {
let (mv, _) = &data[&iter];
iter = iter.revert_with(mv);
res.push(mv.clone());
}
res.reverse();
return Some(res);
}

None
}

0 comments on commit 1b7790f

Please sign in to comment.
You can’t perform that action at this time.