Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dwave Annealing Method #115

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private static SolverSetting getSolverSetting(
name,
description,
options,
codec.treeToValue(node.get("selectedOption"), Object.class).toString());
codec.treeToValue(node.get("selectedOption"), Object.class).toString(),
Object::toString);
}
case TEXT -> {
return new TextSetting(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,90 @@

import edu.kit.provideq.toolbox.meta.setting.SolverSetting;
import edu.kit.provideq.toolbox.meta.setting.SolverSettingType;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Nullable;

public class SelectSetting<T> extends SolverSetting {
private List<T> options;
private List<String> options;
@Nullable
private T selectedOption;
private String selectedOption;

public SelectSetting(String name, String description, List<T> options) {
this(false, name, description, options);
public SelectSetting(
String name,
String description,
List<T> options,
Function<T, String> mapper) {
this(false, name, description, options, mapper);
}

public SelectSetting(boolean required, String name, String description, List<T> options) {
this(required, name, description, options, null);
public SelectSetting(
boolean required,
String name,
String description,
List<T> options,
Function<T, String> mapper) {
this(required, name, description, options, null, mapper);
}

public SelectSetting(String name, String description, List<T> options, T selectedOption) {
this(false, name, description, options, selectedOption);
public SelectSetting(
String name,
String description, List<T> options,
T selectedOption,
Function<T, String> mapper) {
this(false, name, description, options, selectedOption, mapper);
}

public SelectSetting(
boolean required,
String name,
String description,
List<T> options,
T selectedOption) {
T selectedOption,
Function<T, String> mapper) {
super(name, description, SolverSettingType.SELECT, required);

this.setOptions(options);
this.setSelectedOption(selectedOption);
this.setOptions(options, mapper);
this.setSelectedOption(selectedOption, mapper);
}

public List<T> getOptions() {
return options;
public List<String> getOptions() {
return Collections.unmodifiableList(options);
}

public void setOptions(List<T> options) {
this.options = options;
public List<T> getOptionsT(Function<String, T> mapper) {
Objects.requireNonNull(mapper);

return options.stream()
.map(mapper)
.toList();
}

public void setOptions(List<T> options, Function<T, String> mapper) {
Objects.requireNonNull(mapper);

this.options = options.stream()
.map(mapper)
.toList();
}

@Nullable
public T getSelectedOption() {
public String getSelectedOption() {
return selectedOption;
}

public void setSelectedOption(@Nullable T selectedOption) {
this.selectedOption = selectedOption;
@Nullable
public T getSelectedOptionT(Function<String, T> mapper) {
Objects.requireNonNull(mapper);

return mapper.apply(selectedOption);
}

public void setSelectedOption(@Nullable T selectedOption, Function<T, String> mapper) {
Objects.requireNonNull(mapper);

this.selectedOption = mapper.apply(selectedOption);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DwaveQuboSolver extends QuboSolver {
enum AnnealingMethod {
SIMULATED("sim"),
HYBRID("hybrid"),
ABSOLV("absolv"),
QBSOLV("qbsolv"),
DIRECT("direct");

private final String value;
Expand All @@ -41,6 +41,15 @@ enum AnnealingMethod {
public String getValue() {
return value;
}

public static AnnealingMethod fromValue(String value) {
for (AnnealingMethod method : values()) {
if (method.value.equals(value)) {
return method;
}
}
throw new IllegalArgumentException("Unknown value: " + value);
}
}

private final String scriptPath;
Expand Down Expand Up @@ -70,7 +79,8 @@ public List<SolverSetting> getSolverSettings() {
SETTING_ANNNEALING_METHOD,
"The annealing method to use, only relevant when a token is added",
List.of(AnnealingMethod.values()),
AnnealingMethod.SIMULATED
AnnealingMethod.SIMULATED,
AnnealingMethod::getValue
)
);
}
Expand All @@ -90,7 +100,7 @@ public Mono<Solution<String>> solve(
// (a token is needed to access the d-wave hardware)
var dwaveAnnealingMethod = properties
.<SelectSetting<AnnealingMethod>>getSetting(SETTING_ANNNEALING_METHOD)
.map(SelectSetting::getSelectedOption)
.map(s -> s.getSelectedOptionT(AnnealingMethod::fromValue))
.orElse(DEFAULT_ANNEALING_METHOD);

var solution = new Solution<>(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -95,7 +96,8 @@ public Mono<Solution<String>> solve(
.readOutputFile("problem.lp")
.run(getProblemType(), solution.getId());

if (!processResult.success() || processResult.output().isEmpty()) {
Optional<String> output = processResult.output();
if (!processResult.success() || output.isEmpty()) {
solution.setDebugData(processResult.errorOutput().orElse("Unknown error occurred."));
solution.abort();
return Mono.just(solution);
Expand All @@ -110,7 +112,7 @@ public Mono<Solution<String>> solve(
Path quboSolutionFilePath = Path.of(problemDirectoryPath, "problem.bin");

String finalInput = input;
return resolver.runSubRoutine(QUBO_SUBROUTINE, processResult.output().get())
return resolver.runSubRoutine(QUBO_SUBROUTINE, output.get())
.publishOn(Schedulers.boundedElastic()) //avoids block from Files.writeString() in try/catch
.map(subRoutineSolution -> {
if (subRoutineSolution.getSolutionData() == null
Expand Down
Loading