aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/test/java/org/eclipse/viatra/solver/data/model/hashTests/HashEfficiencyTest.java
blob: c4d98a435bbb49b0fad228d1ce208456b81f5ba9 (plain) (blame)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.eclipse.viatra.solver.data.model.hashTests;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
import org.eclipse.viatra.solver.data.model.Tuple;
import org.eclipse.viatra.solver.data.model.TupleHashProvider;
import org.eclipse.viatra.solver.data.model.TupleHashProviderBitMagic;
import org.junit.jupiter.api.Test;

class HashEfficiencyTest {

	private static List<Tuple> permutations(int range, int arity) {
		if(arity == 1) {
			List<Tuple> result = new ArrayList<>(range);
			for(int i=0; i<range; i++) {
				result.add(Tuple.of(i));
			}
			return result;
		} else if(arity > 1) {
			List<Tuple> smallers = permutations(range, arity-1);
			List<Tuple> result = new ArrayList<>(range*smallers.size());
			for(Tuple smaller : smallers) {
				for(int i=0; i<range; i++) {
					int[] larger = new int[arity];
					for(int x = 0; x<smaller.getSize(); x++) {
						larger[x] = smaller.get(x);
					}
					larger[arity-1] = i;
					result.add(Tuple.of(larger));
				}
			}
			return result;
		} else throw new IllegalArgumentException();
	}
	
	private static int amountToRange(int arity, int n) {
		int range = 1;
		while(Math.pow(range,arity)<n+0.1) {
			range++;
		}
		return 1024;
	}
	
	public static List<Tuple> nPermutations(int arity, int n) {
		int range = amountToRange(arity, n);
		List<Tuple> permutations = permutations(range, arity);
		return permutations.subList(0, n);
	}

	public static List<Tuple> nRandoms(int arity, int n, int seed) {
		int range = amountToRange(arity, n);
		List<Tuple> permutations = new ArrayList<>(n);
		Random r = new Random(seed);
		for(int i = 0; i<n; i++) {
			int[] tuple = new int[arity];
			for(int j=0; j<arity; j++) {
				tuple[j] = r.nextInt(range);
			}
			permutations.add(Tuple.of(tuple));
		}
		return permutations;
	}
	
	@Test
	void permutationTest() {
		List<Tuple> p = permutations(10, 2);
		assertEquals(p.size(),10*10);
	}
//	private void printTuples(List<Tuple> p) {
//		for(Tuple element : p) {
//			System.out.println(element);
//		}
//	}
	@Test
	void nPermutationTest() {
		final int amount = 500;
		List<Tuple> p = nPermutations(2, amount);
		assertEquals(amount,p.size());
	}
	@Test
	void nRandomTest() {
		final int amount = 500;
		List<Tuple> p = nRandoms(2, amount, 1);;
		assertEquals(amount,p.size());
	}
	private static double calculateHashClashes(List<Tuple> tuples, ContinousHashProvider<Tuple> chp) {
		int sumClashes = 0;
		
		for(int i = 0; i<tuples.size(); i++) {
			int height = 0;
			for(int j=0; j<tuples.size(); j++) {
				int clashes = calculateHashClash(chp, tuples.get(i), tuples.get(j));
				height = Math.max(height, clashes);
			}
			sumClashes += height;
		}
		return (sumClashes+0.0) / tuples.size();
	}
	private static int calculateHashClash(ContinousHashProvider<Tuple> chp, Tuple a, Tuple b) {
		if(a.equals(b)) return 0;
		final int bits = 5;
		final int segments = Integer.SIZE/bits;
		final int mask = (1<<bits)-1;
		for(int i = 0;;i++) {
			int index = i/segments;
			int depth = i%segments;
			int aHash = (chp.getHash(a, index)>>(depth*5))&mask;
			int bHash = (chp.getHash(b, index)>>(depth*5))&mask;
			if(aHash != bHash) {
				return i+1;
			}
			if(i>400) {
				throw new IllegalStateException(a+" vs "+b);
			}
		}
	}
	private static double caclulateOptimalHashClash(int size) {
		return (Math.log(size)/Math.log(32));
	}
	public static void main(String[] args) {
		List<String> hashNames = new LinkedList<>();
		List<ContinousHashProvider<Tuple>> hashes = new LinkedList<>();
		hashNames.add("PrimeGroup");
		hashes.add(new TupleHashProvider());
		hashNames.add("BitMagic");
		hashes.add(new TupleHashProviderBitMagic());
		
		int[] arities = new int[] {2,3,4,5};
		int[] sizes = new int[] {32*32,32*32*8};
		
		System.out.println("Size,Arity,DataSource,Hash,Chashes,Optimal,Badness");
		for(int size : sizes) {
			double optimalClashes = caclulateOptimalHashClash(size);
			for(int arity : arities) {
				List<String> dataSourceNames = new LinkedList<>();
				List<List<Tuple>> dataSources = new LinkedList<>();
				
//				dataSourceNames.add("Permutation");
//				dataSources.add(nPermutations(arity, size));
				dataSourceNames.add("Random");
				dataSources.add(nRandoms(arity, size, 0));
				
				for(int dataSourceIndex = 0; dataSourceIndex<dataSourceNames.size(); dataSourceIndex++) {
					for(int hashIndex = 0; hashIndex<hashNames.size(); hashIndex++) {
						double clashes = calculateHashClashes(dataSources.get(dataSourceIndex),hashes.get(hashIndex));
						System.out.println(
							size+","+arity+","+dataSourceNames.get(dataSourceIndex)+","+hashNames.get(hashIndex)+","+
							clashes+","+optimalClashes+","+(clashes+0.0)/optimalClashes);
					}
				}
			}
		}
	}
}