1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Implementations of different static variable ordering strategies
use rand::{seq::SliceRandom, thread_rng};

use super::dimacs::Instance;

#[allow(dead_code)]
pub fn keep(instance: &Instance) -> Vec<u32> {
    let mut order: Vec<u32> = (1..instance.no_variables + 1).collect();

    order.insert(0, (order.len() + 1) as u32);
    order
}

#[allow(dead_code)]
pub fn rand(instance: &Instance) -> Vec<u32> {
    let mut order: Vec<u32> = (1..instance.no_variables + 1).collect();

    order.shuffle(&mut thread_rng());

    order.insert(0, (order.len() + 1) as u32);
    order
}

#[allow(dead_code)]
pub fn force(instance: &Instance) -> Vec<u32> {
    let mut order: Vec<u32> = (1..instance.no_variables + 1).collect();

    order.shuffle(&mut thread_rng());
    order.insert(0, (order.len() + 1) as u32);

    let mut span: Option<u32> = None;

    let mut converged = false;

    let mut n = 0;

    while !converged && n < 1000 {
        n += 1;

        let mut tpos: Vec<f64> = vec![0.0; (instance.no_variables + 1) as usize];
        let mut degree: Vec<u32> = vec![0; (instance.no_variables + 1) as usize];

        for clause in &instance.clauses {
            let cog: f64 = calc_center_of_gravity(clause, &order);

            for x in clause {
                let y = x.unsigned_abs() as usize;
                tpos[y] += cog;
                degree[y] += 1;
            }
        }

        for x in 1..instance.no_variables + 1 {
            let y = x as usize;
            tpos[y] /= degree[y] as f64;
        }

        // println!("{:?}", tpos);

        order = (1..instance.no_variables + 1).collect::<Vec<u32>>();

        // println!("{:?}", order);

        order.sort_by(|x, y| {
            tpos[*x as usize]
                .partial_cmp(&tpos[*y as usize])
                .unwrap_or(std::cmp::Ordering::Less)
        });
        // println!("{:?}", order);
        order.insert(0, (order.len() + 1) as u32);

        let span_new = calc_span(&instance.clauses, &order);

        if span.is_none() || span_new != span.unwrap() {
            span = Some(span_new);
        } else {
            converged = true;
        }
    }

    order
}

fn calc_center_of_gravity(clause: &[i32], order: &[u32]) -> f64 {
    let mut out = 0;
    for x in clause {
        out += order[x.unsigned_abs() as usize];
    }

    out as f64 / clause.len() as f64
}

fn calc_span(clauses: &[Vec<i32>], order: &[u32]) -> u32 {
    let mut span = 0;

    for clause in clauses {
        let pos = clause
            .iter()
            .map(|x| order[x.unsigned_abs() as usize] as u32)
            .collect::<Vec<u32>>();
        let min = pos.iter().min().unwrap();
        let max = pos.iter().max().unwrap();
        span += max - min;
    }

    span
}

#[allow(dead_code)]
fn order_dist(instance: &Instance) -> Vec<u32> {
    let n = (instance.no_variables + 1) as usize;
    let mut dists: Vec<Vec<u32>> = vec![vec![0; n]; n];
    for clause in &instance.clauses {
        for x_ in clause {
            let x = x_.unsigned_abs() as usize;

            for y_ in clause {
                let y = y_.unsigned_abs() as usize;
                if x >= y {
                    break;
                }

                dists[x][y] += 1;
                dists[y][x] += 1;
            }
        }
    }

    println!(
        "{:?}",
        dists
            .iter()
            .enumerate()
            .map(|(i, x)| (i, x.iter().sum::<u32>()))
            .max_by(|(_, x), (_, y)| x.partial_cmp(y).unwrap())
            .unwrap()
    );

    vec![0]
}