Skip to content

Commit

Permalink
Style
Browse files Browse the repository at this point in the history
Signed-off-by: Arjo Chakravarty <[email protected]>
  • Loading branch information
arjo129 committed Nov 19, 2024
1 parent e3c93b0 commit cab8542
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 84 deletions.
6 changes: 5 additions & 1 deletion examples/discrete_vs_nodiscrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ fn main() {
FixedTimeSATSolver::without_optimality_check(soln.clone());
let optimality_proof_dur = timer.elapsed();

let sat_flexible_time_problem = sat_flexible_time_model::Problem { requests, dependencies: vec![] , one_of_dependencies: vec![]};
let sat_flexible_time_problem = sat_flexible_time_model::Problem {
requests,
dependencies: vec![],
one_of_dependencies: vec![],
};

let stop = Arc::new(AtomicBool::new(false));

Expand Down
113 changes: 65 additions & 48 deletions examples/end_to_end_planner.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use std::{ops::Index, sync::atomic::AtomicBool};
use chrono::Duration;
use std::{ops::Index, sync::atomic::AtomicBool};

use futures::pending;
/// This example show cases how to use a* for accounting for optimal parking spot allocation
/// The core idea is we have a set of robots that need to traverse an area. The goal is to pick up an item
/// and then have it delivered to a certain location.
///
///
/// Along the way we may place a door which serves as a bottle neck.
use pathfinding::prelude::astar;
use rmf_reservations::{algorithms::sat_flexible_time_model::{Problem, SATFlexibleTimeModel}, cost_function::static_cost, database::DefaultUtcClock, ReservationParameters, ReservationRequestAlternative, StartTimeRange};

fn test_scenario() -> String
{
use rmf_reservations::{
algorithms::sat_flexible_time_model::{Problem, SATFlexibleTimeModel},
cost_function::static_cost,
database::DefaultUtcClock,
ReservationParameters, ReservationRequestAlternative, StartTimeRange,
};

fn test_scenario() -> String {
// Goal is for robot to transport i to I and j to J, with parking spots
// "p" using robots 1 and 2.

Expand All @@ -28,57 +32,68 @@ fn test_scenario() -> String
room.to_string()
}

fn find_character_coordinate(map: &Vec<String>, character: &String) -> Option<(usize, usize)>
{
map.iter().enumerate()
.map(|(idx, f)| {(f.find(character), idx)})
.fold(None, |num, res| if res.0 == None { num } else {
if let Some(row) = res.0 {
Some((res.1, row))
}
else {
num
}
})
fn find_character_coordinate(map: &Vec<String>, character: &String) -> Option<(usize, usize)> {
map.iter()
.enumerate()
.map(|(idx, f)| (f.find(character), idx))
.fold(None, |num, res| {
if res.0 == None {
num
} else {
if let Some(row) = res.0 {
Some((res.1, row))
} else {
num
}
}
})
}

fn transform_to_map(map: String) -> Vec<String>
{
map.split("\n").filter(|p| p.len() > 0).map(|f| f.replace(" ", "").to_string()).collect()
fn transform_to_map(map: String) -> Vec<String> {
map.split("\n")
.filter(|p| p.len() > 0)
.map(|f| f.replace(" ", "").to_string())
.collect()
}


fn display_path(path: &Vec<(usize, usize)>, m: &Vec<String>)
{
fn display_path(path: &Vec<(usize, usize)>, m: &Vec<String>) {
let mut m = m.clone();
for p in path {
m[p.0].replace_range(p.1..p.1+1, "*");
m[p.0].replace_range(p.1..p.1 + 1, "*");
}

for line in m{
for line in m {
println!("{:?}", line);
}
}

fn astar_cost_estimate(start_goal: (usize, usize), end_goal: (usize, usize), m: &Vec<String>) -> Option<(Vec<(usize, usize)>, usize)>
{
let opts = vec![(0,1), (1,0), (0,-1), (-1,0)];
astar(&start_goal, |node| {
let v:Vec<_> = opts.iter()
.map(|(dx, dy)| (node.0 as i32 + dx.clone(), node.1 as i32 + dy.clone()))
.filter(|(x,y)| *x >= 0 && *y >= 0 && *x < m.len() as i32 && *y < m[0].len() as i32)
.filter(|(x,y)| m[*x as usize].bytes().nth(*y as usize).unwrap() != b'x')
.collect();
v.into_iter().map(|(x,y)| ((x as usize, y as usize), 1)) },
|p| p.0.abs_diff(end_goal.0) + p.1.abs_diff(end_goal.1),
|f| *f == end_goal)
fn astar_cost_estimate(
start_goal: (usize, usize),
end_goal: (usize, usize),
m: &Vec<String>,
) -> Option<(Vec<(usize, usize)>, usize)> {
let opts = vec![(0, 1), (1, 0), (0, -1), (-1, 0)];
astar(
&start_goal,
|node| {
let v: Vec<_> = opts
.iter()
.map(|(dx, dy)| (node.0 as i32 + dx.clone(), node.1 as i32 + dy.clone()))
.filter(|(x, y)| {
*x >= 0 && *y >= 0 && *x < m.len() as i32 && *y < m[0].len() as i32
})
.filter(|(x, y)| m[*x as usize].bytes().nth(*y as usize).unwrap() != b'x')
.collect();
v.into_iter().map(|(x, y)| ((x as usize, y as usize), 1))
},
|p| p.0.abs_diff(end_goal.0) + p.1.abs_diff(end_goal.1),
|f| *f == end_goal,
)
}

fn ototot()
{
fn ototot() {
use std::sync::Arc;


let current_time = chrono::Utc::now();

let n = 60usize;
Expand All @@ -89,7 +104,7 @@ fn ototot()
parameters: ReservationParameters {
resource_name: "Resource1".to_string(),
duration: Some(task_dur),
start_time:StartTimeRange {
start_time: StartTimeRange {
earliest_start: Some(current_time),
latest_start: Some(current_time + task_dur * (n as i32 + 1)),
},
Expand All @@ -98,25 +113,27 @@ fn ototot()
}]);
}

let problem = Problem { requests, one_of_dependencies: vec![], dependencies: vec![] };
let problem = Problem {
requests,
one_of_dependencies: vec![],
dependencies: vec![],
};

let stop = Arc::new(AtomicBool::new(false));
/*let model = SATFlexibleTimeModel {
clock_source: DefaultUtcClock::default(),
}
.time_optimality_solver(&problem, stop);
let result = model.unwrap();*/

}

fn main()
{
fn main() {
let m = transform_to_map(test_scenario());

let Some(end_goal) = find_character_coordinate(&m, &"i".to_string()) else {
panic!("couldnt find start");
};
let Some(start_goal) = find_character_coordinate(&m, &"I".to_string()) else {
let Some(start_goal) = find_character_coordinate(&m, &"I".to_string()) else {
panic!("could not find end");
};

Expand All @@ -126,4 +143,4 @@ fn main()
//let mut problem = Problem {}

//display_path(&res.unwrap().0, &m);
}
}
Loading

0 comments on commit cab8542

Please sign in to comment.