Implement job scheduling
This commit is contained in:
43
src/jobs.rs
43
src/jobs.rs
@@ -1,17 +1,48 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Job {
|
pub struct Job {
|
||||||
pub weight: u32,
|
pub weight: i64,
|
||||||
pub length: u32,
|
pub length: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Jobs = Vec<Job>;
|
pub type Jobs = Vec<Job>;
|
||||||
|
|
||||||
|
fn weighted_completion_time(jobs: &Jobs) -> i64 {
|
||||||
|
let mut time = 0;
|
||||||
|
let mut weighted_completion_time = 0;
|
||||||
|
|
||||||
fn weighted_completion_time(jobs: &Jobs) -> usize {
|
for job in jobs {
|
||||||
|
let completion_time = time + job.length;
|
||||||
jobs.len()
|
weighted_completion_time += completion_time * job.weight;
|
||||||
|
time += job.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jobs1(jobs: &Jobs) -> usize {
|
weighted_completion_time
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compare_1(a: &Job, b: &Job) -> Ordering {
|
||||||
|
let da = a.weight - a.length;
|
||||||
|
let db = b.weight - b.length;
|
||||||
|
if da == db {
|
||||||
|
b.weight.cmp(&a.weight)
|
||||||
|
} else {
|
||||||
|
db.cmp(&da)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compare_2(a: &Job, b: &Job) -> Ordering {
|
||||||
|
let da: f64 = a.weight as f64 / a.length as f64;
|
||||||
|
let db: f64 = b.weight as f64 / b.length as f64;
|
||||||
|
db.partial_cmp(&da).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn jobs_1(jobs: &mut Jobs) -> i64 {
|
||||||
|
jobs.sort_by(compare_1);
|
||||||
|
weighted_completion_time(&jobs)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn jobs_2(jobs: &mut Jobs) -> i64 {
|
||||||
|
jobs.sort_by(compare_2);
|
||||||
weighted_completion_time(&jobs)
|
weighted_completion_time(&jobs)
|
||||||
}
|
}
|
||||||
13
src/main.rs
13
src/main.rs
@@ -17,7 +17,7 @@ use crate::min_cut::min_cut;
|
|||||||
use crate::quick_sort::quick_sort;
|
use crate::quick_sort::quick_sort;
|
||||||
use crate::ssc::ssc;
|
use crate::ssc::ssc;
|
||||||
use crate::two_sum::find_two_sums;
|
use crate::two_sum::find_two_sums;
|
||||||
use crate::jobs::jobs1;
|
use crate::jobs::{jobs_1, jobs_2};
|
||||||
use crate::prims::prims;
|
use crate::prims::prims;
|
||||||
|
|
||||||
|
|
||||||
@@ -91,12 +91,15 @@ fn c2a4() {
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn c3a1() {
|
fn c3a1() {
|
||||||
let jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap();
|
let mut jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap();
|
||||||
let r1 = jobs1(&jobs);
|
let r1 = jobs_1(&mut jobs);
|
||||||
let r2 = -1;
|
let r2 = jobs_2(&mut jobs);
|
||||||
let r3 = prims();
|
|
||||||
|
let g = util::read_weighted_graph("data/c3a1_edges.txt").unwrap();
|
||||||
|
let r3 = prims(&g);
|
||||||
|
|
||||||
println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3);
|
println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3);
|
||||||
|
// r1 = 69119377652; r2 != 67311454237;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
12
src/prims.rs
12
src/prims.rs
@@ -1,4 +1,14 @@
|
|||||||
|
#[derive(Debug, Clone)]
|
||||||
|
|
||||||
pub fn prims() -> usize {
|
pub struct Edge {
|
||||||
|
pub node_a: u32,
|
||||||
|
pub node_b: u32,
|
||||||
|
pub weight: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type WeightedGraph = Vec<Edge>;
|
||||||
|
|
||||||
|
pub fn prims(graph: &WeightedGraph) -> usize {
|
||||||
|
println!("{:?}", graph);
|
||||||
42
|
42
|
||||||
}
|
}
|
||||||
32
src/util.rs
32
src/util.rs
@@ -3,6 +3,7 @@ use std::io;
|
|||||||
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use crate::jobs::{Job, Jobs};
|
use crate::jobs::{Job, Jobs};
|
||||||
|
use crate::prims::{WeightedGraph, Edge};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Graph {
|
pub struct Graph {
|
||||||
@@ -149,8 +150,8 @@ pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
|
|||||||
for line in lines {
|
for line in lines {
|
||||||
let line = line?;
|
let line = line?;
|
||||||
let mut nodes = line.split_whitespace();
|
let mut nodes = line.split_whitespace();
|
||||||
let weight: u32 = nodes.next().unwrap().parse().unwrap();
|
let weight = nodes.next().unwrap().parse().unwrap();
|
||||||
let length: u32 = nodes.next().unwrap().parse().unwrap();
|
let length = nodes.next().unwrap().parse().unwrap();
|
||||||
let job = Job {
|
let job = Job {
|
||||||
weight: weight,
|
weight: weight,
|
||||||
length: length,
|
length: length,
|
||||||
@@ -160,3 +161,30 @@ pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
|
|||||||
|
|
||||||
Ok(jobs)
|
Ok(jobs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
|
||||||
|
let g = Vec::new();
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let br = BufReader::new(file);
|
||||||
|
let mut lines = br.lines();
|
||||||
|
let line = lines.next().unwrap().unwrap();
|
||||||
|
let mut nodes = line.split_whitespace();
|
||||||
|
let n_nodes: usize = nodes.next().unwrap().parse().unwrap();
|
||||||
|
let n_edges: usize = nodes.next().unwrap().parse().unwrap();
|
||||||
|
|
||||||
|
println!("{:?} {:?}", n_nodes, n_edges);
|
||||||
|
// lines.next();
|
||||||
|
// for line in lines {
|
||||||
|
// let line = line?;
|
||||||
|
// let mut nodes = line.split_whitespace();
|
||||||
|
// let weight = nodes.next().unwrap().parse().unwrap();
|
||||||
|
// let length = nodes.next().unwrap().parse().unwrap();
|
||||||
|
// let job = Job {
|
||||||
|
// weight: weight,
|
||||||
|
// length: length,
|
||||||
|
// };
|
||||||
|
// jobs.push(job);
|
||||||
|
// }
|
||||||
|
|
||||||
|
Ok(g)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user