aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ObjectivePriorityQueueImpl.java
blob: 2f3e142ad8a4c0acc82a181441ad7516c4902e17 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.transition.statespace.internal;

import tools.refinery.store.dse.transition.ObjectiveValues;
import tools.refinery.store.dse.transition.VersionWithObjectiveValue;
import tools.refinery.store.dse.transition.objectives.Objective;
import tools.refinery.store.dse.transition.statespace.ObjectivePriorityQueue;

import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;

public class ObjectivePriorityQueueImpl implements ObjectivePriorityQueue {
	public static final Comparator<VersionWithObjectiveValue> c1 = (o1, o2) -> Double.compare(
			((ObjectiveValues.ObjectiveValue1) o1.objectiveValue()).value0(),
			((ObjectiveValues.ObjectiveValue1) o2.objectiveValue()).value0());
	// TODO: support multi objective!
	final PriorityQueue<VersionWithObjectiveValue> priorityQueue;

	public ObjectivePriorityQueueImpl(List<Objective> objectives) {

		if(objectives.size() == 1) {
			this.priorityQueue = new PriorityQueue<>(c1);
		} else {
			throw new UnsupportedOperationException("Only single objective comparator is implemented currently!");
		}
	}
	@Override
	public Comparator<VersionWithObjectiveValue> getComparator() {
		return c1;
	}

	@Override
	public synchronized void submit(VersionWithObjectiveValue versionWithObjectiveValue) {
		priorityQueue.add(versionWithObjectiveValue);
	}

	@Override
	public synchronized void remove(VersionWithObjectiveValue versionWithObjectiveValue) {
		priorityQueue.remove(versionWithObjectiveValue);
	}

	@Override
	public synchronized int getSize() {
		return priorityQueue.size();
	}

	@Override
	public synchronized VersionWithObjectiveValue getBest() {
		return priorityQueue.peek();
	}

	@Override
	public synchronized VersionWithObjectiveValue getRandom(Random random) {
		int randomPosition = random.nextInt(getSize());
		for (VersionWithObjectiveValue entry : this.priorityQueue) {
			if (randomPosition-- == 0) {
				return entry;
			}
		}
		throw new IllegalStateException("The priority queue is inconsistent!");
	}
}