summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2024-04-07 07:03:49 +0200
committerache <ache@ache.one>2024-04-07 07:03:49 +0200
commit89b4351a5d598608e2900cdc496cac3fc4603c0b (patch)
treeb6a461d4c39354fea7b4fca6db50f6463b1eca58
Init testmaster
-rw-r--r--Cargo.lock81
-rw-r--r--Cargo.toml9
-rw-r--r--src/lib.rs14
-rw-r--r--src/main.rs131
4 files changed, 235 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..7c112bb
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,81 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "autocfg"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
+
+[[package]]
+name = "matrixmultiply"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "916806ba0031cd542105d916a97c8572e1fa6dd79c9c51e7eb43a09ec2dd84c1"
+dependencies = [
+ "rawpointer",
+]
+
+[[package]]
+name = "ndarray"
+version = "0.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac06db03ec2f46ee0ecdca1a1c34a99c0d188a0d83439b84bf0cb4b386e4ab09"
+dependencies = [
+ "matrixmultiply",
+ "num-complex",
+ "num-integer",
+ "num-traits",
+ "rawpointer",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.46"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "rawpointer"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
+
+[[package]]
+name = "simplex"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77a0a5a52d30f707324c1fcaf35cdbd6c0252712df513a154a9128072a1c693d"
+dependencies = [
+ "ndarray",
+]
+
+[[package]]
+name = "voting"
+version = "0.1.0"
+dependencies = [
+ "simplex",
+]
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..59aef9b
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "voting"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+simplex = "1.0.0"
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..7d12d9a
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,14 @@
+pub fn add(left: usize, right: usize) -> usize {
+ left + right
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn it_works() {
+ let result = add(2, 2);
+ assert_eq!(result, 4);
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..a055909
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,131 @@
+use core::panic;
+use std::fmt::{self, write};
+use std::{cmp::Ordering, vec};
+
+#[derive(Debug)]
+struct Poll<const N: usize> {
+ nb_votes: u64,
+ votes: Vec<Vec<i64>>,
+}
+
+impl<const N: usize> fmt::Display for Poll<N> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ writeln!(f, "{{")?;
+ writeln!(f, "Nb votes: {}", self.nb_votes)?;
+ for row in self.votes.iter() {
+ write!(f, " [{}", row[0])?;
+ for v in row.iter().skip(1) {
+ write!(f, " {}", v)?;
+ }
+ writeln!(f, "]")?;
+ }
+
+ writeln!(f, "}}\n")
+ }
+}
+
+impl<const N: usize> Poll<N> {
+ // Constructs a new instance of [`Second`].
+ // Note this is an associated function - no self.
+ pub fn new() -> Self {
+ Self {
+ nb_votes: 0,
+ votes: vec![vec![0; N]; N],
+ }
+ }
+ fn is_empty(&self) -> bool {
+ self.nb_votes == 0
+ }
+ fn vote(&mut self, preference: [u64; N]) {
+ self.vote_n(preference, 1)
+ }
+ fn vote_n(&mut self, preference: [u64; N], n: u64) {
+ let mut has_alternative = vec![false; N];
+ for i in preference.into_iter() {
+ let i: usize = i.try_into().expect("out of bound value");
+
+ if i >= N {
+ panic!("Out of value ballot")
+ }
+ if has_alternative[i] {
+ panic!("The preference isn't a strict order")
+ }
+
+ has_alternative[i] = true;
+ }
+
+ self.nb_votes += n;
+ for (k, i) in preference.into_iter().enumerate() {
+ let i = i as usize;
+ for j in preference[(k + 1)..].iter() {
+ let j = (*j) as usize;
+
+ self.votes[i][j] += n as i64;
+ self.votes[j][i] -= n as i64;
+ }
+ }
+ }
+ fn elect(&self) -> Vec<f64> {
+ let base_vect = [0.31, 0.27, 0.11, 0.4, 0.2, 0.5, 0.78];
+ print!("max Z = {}*x1 ", base_vect[0]);
+ for (i, zeta_i) in base_vect.iter().enumerate().take(N).skip(1) {
+ print!(" + {}*x{}", zeta_i, i + 1);
+ }
+ println!("\nS.t:");
+
+ for row in self.votes.iter() {
+ let mut has_started = false;
+ for (j, v) in row.iter().enumerate() {
+ let s = match (*v).partial_cmp(&0).unwrap() {
+ Ordering::Greater => 1,
+ Ordering::Less => -1,
+ Ordering::Equal => continue,
+ };
+
+ if s == -1 {
+ if !has_started {
+ print!("-")
+ } else {
+ print!(" - ")
+ }
+ } else if has_started {
+ print!(" + ")
+ }
+
+ print!("x{}", j + 1);
+ has_started = true;
+ }
+ println!(" >= 0");
+ }
+
+ for i in 0..N {
+ println!("x{} >= 0", i + 1);
+ }
+
+ print!("x1");
+ for i in 1..N {
+ print!(" + x{}", i + 1);
+ }
+ println!(" = 1");
+
+ vec![0.]
+ }
+ fn set_distribution(&self) -> bool {
+ self.nb_votes == 0
+ }
+}
+fn main() {
+ let mut a: Poll<3> = Poll::new();
+
+ a.vote_n([0, 1, 2], 27);
+ a.vote_n([0, 2, 1], 8);
+ a.vote_n([1, 2, 0], 21);
+ a.vote_n([1, 0, 2], 14);
+ a.vote_n([2, 0, 1], 20);
+ a.vote_n([2, 1, 0], 10);
+
+ // let program = Simplex::minimize(&vec![1.0, 1.0, 1.0])
+
+ println!("{}", a);
+ println!("{:?}", a.elect());
+}