Skip to content

Commit

Permalink
Merge pull request #115 from ProvideQ/fix/dwave-annealing-method
Browse files Browse the repository at this point in the history
Fix Dwave Annealing Method
  • Loading branch information
koalamitice authored Jan 13, 2025
2 parents 815c167 + 5b4f0a4 commit 9b93f99
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
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

0 comments on commit 9b93f99

Please sign in to comment.