engioptiqa 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
engioptiqa/__init__.py ADDED
@@ -0,0 +1,9 @@
1
+ from .annealing_solvers import *
2
+ from .problems import *
3
+ __all__ = [
4
+ "AnnealingSolverAmplify",
5
+ "AnnealingSolverDWave",
6
+ "DesignOptimizationProblem",
7
+ "Rod1D",
8
+ "StructuralAnalysisProblem",
9
+ ]
@@ -0,0 +1,7 @@
1
+ from .annealing_solver_amplify import AnnealingSolverAmplify
2
+ from .annealing_solver_dwave import AnnealingSolverDWave
3
+
4
+ __all__ = [
5
+ "AnnealingSolverAmplify",
6
+ "AnnealingSolverDWave"
7
+ ]
@@ -0,0 +1,21 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ class AnnealingSolver(ABC):
4
+
5
+ def __init__(self, token_file=None, proxy=None):
6
+ self.proxy = proxy
7
+ if token_file is not None:
8
+ self.token = open(token_file,"r").read().replace('\n', '')
9
+ self.setup_client()
10
+
11
+ @abstractmethod
12
+ def setup_client(self):
13
+ pass
14
+
15
+ @abstractmethod
16
+ def setup_solver(self):
17
+ pass
18
+
19
+ @abstractmethod
20
+ def solve_qubo_problem(self, problem):
21
+ pass
@@ -0,0 +1,55 @@
1
+ from amplify import Solver
2
+ from amplify.client import FixstarsClient
3
+ from amplify.client.ocean import DWaveSamplerClient, LeapHybridSamplerClient
4
+
5
+ from .annealing_solver import AnnealingSolver
6
+
7
+ class AnnealingSolverAmplify(AnnealingSolver):
8
+ """
9
+ Initialize the Annealing Solver that uses the Amplify SDK.
10
+
11
+ :param client_type: The client type.
12
+ :param token_file: The path of the file that includes the token for the Fixstars Amplify Annealing Engine.
13
+ """
14
+ def __init__(self, client_type, token_file, proxy=None):
15
+ self.client_type = client_type
16
+ super().__init__(token_file, proxy)
17
+
18
+
19
+ def setup_client(self):
20
+
21
+ if self.client_type == 'fixstars':
22
+ self.client = FixstarsClient(token=self.token)
23
+ elif self.client_type == 'dwave':
24
+ self.client = DWaveSamplerClient(token=self.token)
25
+ elif self.client_type == 'dwave_hybrid':
26
+ self.client = LeapHybridSamplerClient(token=self.token)
27
+ else:
28
+ raise Exception('Unknown client type', self.client_type)
29
+ if self.proxy:
30
+ self.client.proxy = self.proxy
31
+ if self.client_type in ['dwave','dwave_hybrid']:
32
+ print('Available solvers:', self.client.solver_names)
33
+
34
+
35
+ def setup_solver(self):
36
+
37
+ if self.client_type == 'fixstars':
38
+ print('Setting default timeout (ms): 800')
39
+ self.client.parameters.timeout = 800
40
+ elif self.client_type == 'dwave':
41
+ print('Choosing default solver: Advantage_system4.1')
42
+ self.client.solver = "Advantage_system4.1"
43
+ print('Setting default num_reads: 200')
44
+ self.client.parameters.num_reads = 200
45
+ elif self.client_type == 'dwave_hybrid':
46
+ print('Choosing default solver: hybrid_binary_quadratic_model_version2')
47
+ self.client.solver = 'hybrid_binary_quadratic_model_version2'
48
+
49
+ self.solver = Solver(self.client)
50
+ print("Created solver")
51
+
52
+ def solve_qubo_problem(self, problem):
53
+
54
+ problem.results = self.solver.solve(problem.binary_quadratic_model)
55
+ print('Number of solutions:', len(problem.results))
@@ -0,0 +1,93 @@
1
+ from dwave.cloud import Client
2
+ from dwave.samplers import SimulatedAnnealingSampler, SteepestDescentSolver
3
+ from dwave.system import DWaveSampler, EmbeddingComposite, LeapHybridSampler
4
+
5
+ from .annealing_solver import AnnealingSolver
6
+
7
+ class AnnealingSolverDWave(AnnealingSolver):
8
+
9
+ def __init__(self, token_file=None, proxy=None):
10
+ self.client_type = 'dwave'
11
+ super().__init__(token_file, proxy)
12
+
13
+ def setup_client(self):
14
+
15
+ self.client = Client(token=self.token, proxy=self.proxy)
16
+ print("Available QPU solvers:")
17
+ for solver in self.client.get_solvers(qpu=True, online=True):
18
+ print("\t", solver)
19
+ print("Available hybrid solvers:")
20
+ for solver in self.client.get_solvers(name__regex='hybrid_binary.*', online=True):
21
+ print("\t", solver)
22
+
23
+ def setup_solver(self, solver_type=None, solver_name=None):
24
+
25
+ if solver_name:
26
+ self.solver_name = solver_name
27
+ solver = self.client.get_solver(name=self.solver_name)
28
+ if solver.qpu:
29
+ self.solver_type = 'qpu'
30
+ self.solver = EmbeddingComposite(
31
+ DWaveSampler(
32
+ token=self.token,
33
+ proxy=self.proxy,
34
+ solver=dict(name=self.solver_name)
35
+ )
36
+ )
37
+ else:
38
+ self.solver_type = 'hybrid'
39
+ self.solver = LeapHybridSampler(token=self.token, proxy=self.proxy)
40
+
41
+ elif solver_type:
42
+ self.solver_type = solver_type
43
+ if self.solver_type == 'qpu':
44
+ self.solver = EmbeddingComposite(
45
+ DWaveSampler(token=self.token, proxy=self.proxy)
46
+ )
47
+ self.solver_name = self.solver.child.properties["chip_id"]
48
+
49
+ elif self.solver_type == 'hybrid':
50
+ self.solver = LeapHybridSampler(token=self.token, proxy=self.proxy)
51
+ self.solver_name = self.solver.properties["category"] + ' ' + self.solver.properties["version"]
52
+
53
+ elif self.solver_type == 'simulated_annealing':
54
+ self.solver = SimulatedAnnealingSampler()
55
+ self.solver_name = 'simulated annealing'
56
+ else:
57
+ raise Exception('Unknown solver type', self.solver_type)
58
+ else:
59
+ raise Exception('Either a solver type or a specific solver name must be specified.')
60
+
61
+ print("Use", self.solver_type, "solver:", self.solver_name)
62
+
63
+ def solve_qubo_problem(self, problem, **kwargs):
64
+
65
+ problem.results_indices = self.solver.sample(
66
+ problem.binary_quadratic_model_indices,
67
+ **kwargs
68
+ )
69
+ print('Number of solutions:', len(problem.results_indices))
70
+ if hasattr(problem, 'mapping_i_to_q'):
71
+ problem.results = problem.results_indices.relabel_variables(
72
+ problem.mapping_i_to_q,
73
+ inplace=False)
74
+ else:
75
+ problem.results = problem.results_indices
76
+
77
+ def perform_local_search(self, problem):
78
+
79
+ if hasattr(problem, 'results_indices'):
80
+ solver_greedy = SteepestDescentSolver()
81
+ sampleset_pp = solver_greedy.sample(
82
+ problem.binary_quadratic_model_indices,
83
+ initial_states=problem.results_indices
84
+ )
85
+ if hasattr(problem, 'mapping_i_to_q'):
86
+ problem.results_pp = sampleset_pp.relabel_variables(
87
+ problem.mapping_i_to_q,
88
+ inplace=False)
89
+ else:
90
+ problem.results_pp = sampleset_pp
91
+
92
+ else:
93
+ raise Exception('Trying to perform local search altough no results exist yet.')
@@ -0,0 +1,7 @@
1
+ from .rod_1d import *
2
+
3
+ __all__ = [
4
+ "DesignOptimizationProblem",
5
+ "Rod1D",
6
+ "StructuralAnalysisProblem"
7
+ ]
@@ -0,0 +1,9 @@
1
+ from .design_optimization_problem import DesignOptimizationProblem
2
+ from .rod_1d import Rod1D
3
+ from .structural_analysis_problem import StructuralAnalysisProblem
4
+
5
+ __all__ = [
6
+ "DesignOptimizationProblem",
7
+ "Rod1D",
8
+ "StructuralAnalysisProblem",
9
+ ]