-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLMs.txt
2276 lines (1379 loc) · 74.7 KB
/
LLMs.txt
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# OMMX Documentation for AI Assistants
## Introduction
### Introduction
OMMX (Open Mathematical prograMming eXchange) is an open data format and SDK designed to simplify data exchange between software and people when applying mathematical optimization to real-world problems.
## Data Exchange in Mathematical Optimization
When applying mathematical optimization to practical use cases, a large amount of data is often generated, requiring both effective management and sharing. Unlike the research phase of optimization, the application phase is divided into multiple stages, each necessitating specialized tools. Consequently, data must be converted to formats appropriate for each tool, making the overall process increasingly complex. By establishing one common format, it becomes easier to integrate multiple tools through a single conversion path to and from that format.
Moreover, these tasks are typically carried out by separate individuals and teams, requiring data handoffs. Metadata is critical in these handoffs to clarify the data’s meaning and intention. For example, if a solution file for an optimization problem lacks details regarding which problem was solved, which solver was used, or what settings were chosen, the file cannot be reused or validated effectively. Standardized metadata helps streamline collaboration and data handling.
## Components of OMMX
To address these data exchange challenges, OMMX was developed. It consists of four main components:
- OMMX Message
A data format, independent of programming languages and OS, for exchanging information among software
- OMMX Artifact
A package format with metadata that is convenient for exchanging data among people
- OMMX SDK
A framework for efficiently creating and manipulating OMMX Messages and OMMX Artifacts
- OMMX Adapters
Tools for converting between solver-specific formats and OMMX
### OMMX Message
OMMX Message is a data format defined with [Protocol Buffers](https://protobuf.dev/) to ensure language-agnostic and OS-independent data exchange. It encapsulates schemas for optimization problems ([`ommx.v1.Instance`](./user_guide/instance.ipynb)) and solutions ([`ommx.v1.Solution`](./user_guide/solution.ipynb)). Protocol Buffers allow automatic generation of libraries in many languages, which OMMX SDK provides, especially for Python and Rust.
Data structures such as `ommx.v1.Instance` are called Messages, and each Message has multiple fields. For example, `ommx.v1.Instance` has the following fields (some are omitted for simplicity):
```protobuf
message Instance {
// Decision variables
repeated DecisionVariable decision_variables = 2;
// Objective function
Function objective = 3;
// Constraints
repeated Constraint constraints = 4;
// Maximization or minimization
Sense sense = 5;
}
```
Messages such as `ommx.v1.DecisionVariable` representing decision variables and `ommx.v1.Function` representing mathematical functions used as objective functions and constraints are defined under the namespace `ommx.v1`. A list of Messages defined in OMMX is summarized in [OMMX Message Schema](https://jij-inc.github.io/ommx/protobuf.html).
Some solvers can directly read `ommx.v1.Instance`. For those that cannot, OMMX Adapters can be used to convert OMMX Message data into formats the solvers can handle. This makes it simpler to integrate various tools that support OMMX.
### OMMX Artifact
OMMX Artifact is a metadata-rich package format based on the [OCI (Open Container Initiative)](https://opencontainers.org/) standard. An OCI Artifact manages its content as layers and a manifest, assigning a specific [Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml) to each layer. OMMX defines its own Media Types (e.g., `application/org.ommx.v1.instance`), and when these formats are included in OCI Artifacts, they are called OMMX Artifacts.
In OCI Artifact, the contents of the package are managed in units called layers. A single container contains multiple layers and metadata called a Manifest. When reading a container, the Manifest is first checked, and the necessary data is extracted by reading the layers based on that information. Each layer is saved as binary data (BLOB) with metadata called [Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml). For example, when saving a PDF file, the Media Type `application/pdf` is attached, so software reading OCI Artifacts can recognize it as a PDF file by looking at the Media Type.
One major benefit of OCI Artifact compatibility is that standard container registries, such as [DockerHub](https://hub.docker.com/) or [GitHub Container Registry](https://docs.github.com/ja/packages/working-with-a-github-packages-registry/working-with-the-container-registry), can be used to store and distribute data. OMMX uses this mechanism to share large datasets like [MIPLIB 2017](https://miplib.zib.de/), made available at [GitHub Container Registry](https://github.com/Jij-Inc/ommx/pkgs/container/ommx%2Fmiplib2017). For additional details, see [Download MIPLIB Instances](./tutorial/download_miplib_instance.ipynb).
## Tutorial
### Solve With Ommx Adapter
OMMX provides OMMX Adapter software to enable interoperability with existing mathematical optimization tools. By using OMMX Adapter, you can convert optimization problems expressed in OMMX schemas into formats acceptable to other optimization tools, and convert the resulting data from those tools back into OMMX schemas.
Here, we introduce how to solve a 0-1 Knapsack Problem via OMMX PySCIPOpt Adapter.
## Installing the Required Libraries
First, install OMMX PySCIPOpt Adapter with:
```
pip install ommx-pyscipopt-adapter
```
## Two Steps for Running the Optimization
To solve the 0-1 Knapsack Problem through the OMMX PySCIPOpt Adapter, follow these two steps:
1. Prepare the 0-1 Knapsack problem instance.
2. Run the optimization via OMMX Adapter.
In Step 1, we create an `ommx.v1.Instance` object defined in the OMMX Message Instance schema. There are several ways to generate this object, but in this guide, we'll illustrate how to write it directly using the OMMX Python SDK.
```{tip}
There are four ways to prepare an `ommx.v1.Instance`:
1. Write `ommx.v1.Instance` directly with the OMMX Python SDK.
2. Convert an MPS file to `ommx.v1.Instance` using the OMMX Python SDK.
3. Convert a problem instance from a different optimization tool into `ommx.v1.Instance` using an OMMX Adapter.
4. Export `ommx.v1.Instance` from JijModeling.
```
In Step 2, we convert `ommx.v1.Instance` into a PySCIPOpt `Model` object and run optimization with SCIP. The result is obtained as an `ommx.v1.Solution` object defined by the OMMX Message Solution schema.
### Step 1: Preparing a 0-1 Knapsack Problem Instance
The 0-1 Knapsack problem is formulated as:
$$
\begin{align*}
\mathrm{maximize} \quad & \sum_{i=0}^{N-1} v_i x_i \\
\mathrm{s.t.} \quad & \sum_{i=0}^{n-1} w_i x_i - W \leq 0, \\
& x_{i} \in \{ 0, 1\}
\end{align*}
$$
We set the following data as parameters for this model.
```python
# Data for 0-1 Knapsack Problem
v = [10, 13, 18, 31, 7, 15] # Values of each item
w = [11, 25, 20, 35, 10, 33] # Weights of each item
W = 47 # Capacity of the knapsack
N = len(v) # Total number of items
```
Below is an example code using the OMMX Python SDK to describe this problem instance.
```python
from ommx.v1 import Instance, DecisionVariable
# Define decision variables
x = [
# Define binary variable x_i
DecisionVariable.binary(
# Specify the ID of the decision variable
id=i,
# Specify the name of the decision variable
name="x",
# Specify the subscript of the decision variable
subscripts=[i],
)
# Prepare binary variables for the number of items
for i in range(N)
]
# Define the objective function
objective = sum(v[i] * x[i] for i in range(N))
# Define the constraint
constraint = sum(w[i] * x[i] for i in range(N)) - W <= 0
# Specify the name of the constraint
constraint.add_name("Weight limit")
# Create an instance
instance = Instance.from_components(
# Register all decision variables included in the instance
decision_variables=x,
# Register the objective function
objective=objective,
# Register all constraints
constraints=[constraint],
# Specify that it is a maximization problem
sense=Instance.MAXIMIZE,
)
```
### Step 2: Running Optimization with OMMX Adapter
To optimize the instance prepared in Step 1, we convert it to a PySCIPOpt `Model` and run SCIP optimization via the OMMX PySCIPOpt Adapter.
```python
from ommx_pyscipopt_adapter import OMMXPySCIPOptAdapter
# Obtain an ommx.v1.Solution objection through a PySCIPOpt model.
solution = OMMXPySCIPOptAdapter.solve(instance)
```
The variable `solution` is an `ommx.v1.Solution` object that holds the results returned by SCIP.
## Analyzing the Results
From the `solution` in Step 2, we can check:
- The optimal solution (which items to pick to maximize total value)
- The optimal value (maximum total value)
- The status of constraints (how close we are to the knapsack weight limit)
We can do this with various properties in the `ommx.v1.Solution` class.
### Analyzing the Optimal Solution
The `decision_variables` property returns a `pandas.DataFrame` containing information on each variable, such as ID, type, name, and value:
```python
solution.decision_variables
```
Using this `pandas.DataFrame`, for example, you can easily create a table in pandas that shows which items are included in the knapsack.
```python
import pandas as pd
df = solution.decision_variables
pd.DataFrame.from_dict(
{
"Item number": df.index,
"Include in knapsack?": df["value"].apply(lambda x: "Include" if x == 1.0 else "Exclude"),
}
)
```
From this analysis, we see that choosing items 0 and 3 maximizes the total value while satisfying the knapsack’s weight constraint.
### Analyzing the Optimal Value
`objective` stores the best value found. In this case, it should match the sum of items 0 and 3.
```python
import numpy as np
# The expected value is the sum of the values of items 0 and 3
expected = v[0] + v[3]
assert np.isclose(solution.objective, expected)
```
### Analyzing Constraints
The `constraints` property returns a `pandas.DataFrame` that includes details about each constraint’s equality or inequality, its left-hand-side value (`"value"`), name, and more.
```python
solution.constraints
```
Specifically, The `"value"` is helpful for understanding how much slack remains in each constraint. Here, item 0 weighs $11$, item 3 weighs $35$, and the knapsack’s capacity is $47$. Therefore, for the weight constraint
$$
\begin{align*}
\sum_{i=0}^{n-1} w_i x_i - W \leq 0
\end{align*}
$$
the left-hand side "value" is $-1$, indicating there is exactly 1 unit of slack under the capacity.
### Tsp Sampling With Openjij Adapter
Here, we explain how to convert a problem to QUBO and perform sampling using the Traveling Salesman Problem as an example.
The Traveling Salesman Problem (TSP) is about finding a route for a salesman to visit multiple cities in sequence. Given the travel costs between cities, we seek to find the path that minimizes the total cost. Let's consider the following city arrangement:
```python
# From ulysses16.tsp in TSPLIB
ulysses16_points = [
(38.24, 20.42),
(39.57, 26.15),
(40.56, 25.32),
(36.26, 23.12),
(33.48, 10.54),
(37.56, 12.19),
(38.42, 13.11),
(37.52, 20.44),
(41.23, 9.10),
(41.17, 13.05),
(36.08, -5.21),
(38.47, 15.13),
(38.15, 15.35),
(37.51, 15.17),
(35.49, 14.32),
(39.36, 19.56),
]
```
Let's plot the locations of the cities.
```python
%matplotlib inline
from matplotlib import pyplot as plt
x_coords, y_coords = zip(*ulysses16_points)
plt.scatter(x_coords, y_coords)
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.title('Ulysses16 Points')
plt.show()
```
Let's consider distance as the cost. We'll calculate the distance $d(i, j)$ between city $i$ and city $j$.
```python
def distance(x, y):
return ((x[0] - y[0])**2 + (x[1] - y[1])**2)**0.5
# Number of cities
N = len(ulysses16_points)
# Distance between each pair of cities
d = [[distance(ulysses16_points[i], ulysses16_points[j]) for i in range(N)] for j in range(N)]
```
Using this, we can formulate TSP as follows. First, let's represent whether we are at city $i$ at time $t$ with a binary variable $x_{t, i}$. Then, we seek $x_{t, i}$ that satisfies the following constraints. The distance traveled by the salesman is given by:
$$
\sum_{t=0}^{N-1} \sum_{i, j = 0}^{N-1} d(i, j) x_{t, i} x_{(t+1 \% N), j}
$$
However, $x_{t, i}$ cannot be chosen freely and must satisfy two constraints: at each time $t$, the salesman can only be in one city, and each city must be visited exactly once:
$$
\sum_{i=0}^{N-1} x_{t, i} = 1, \quad \sum_{t=0}^{N-1} x_{t, i} = 1
$$
Combining these, TSP can be formulated as a constrained optimization problem:
$$
\begin{align*}
\min \quad & \sum_{t=0}^{N-1} \sum_{i, j = 0}^{N-1} d(i, j) x_{t, i} x_{(t+1 \% N), j} \\
\text{s.t.} \quad & \sum_{i=0}^{N-1} x_{t, i} = 1 \quad (\forall t = 0, \ldots, N-1) \\
\quad & \sum_{t=0}^{N-1} x_{t, i} = 1 \quad (\forall i = 0, \ldots, N-1)
\end{align*}
$$
The corresponding `ommx.v1.Instance` can be created as follows:
```python
from ommx.v1 import DecisionVariable, Instance
x = [[
DecisionVariable.binary(
i + N * t, # Decision variable ID
name="x", # Name of the decision variable, used when extracting solutions
subscripts=[t, i]) # Subscripts of the decision variable, used when extracting solutions
for i in range(N)
]
for t in range(N)
]
objective = sum(
d[i][j] * x[t][i] * x[(t+1) % N][j]
for i in range(N)
for j in range(N)
for t in range(N)
)
place_constraint = [
(sum(x[t][i] for i in range(N)) == 1)
.set_id(t) # type: ignore
.add_name("place")
.add_subscripts([t])
for t in range(N)
]
time_constraint = [
(sum(x[t][i] for t in range(N)) == 1)
.set_id(i + N) # type: ignore
.add_name("time")
.add_subscripts([i])
for i in range(N)
]
instance = Instance.from_components(
decision_variables=[x[t][i] for i in range(N) for t in range(N)],
objective=objective,
constraints=place_constraint + time_constraint,
sense=Instance.MINIMIZE
)
```
The variable names and subscripts added to `DecisionVariable.binary` during creation will be used later when interpreting the obtained samples.
## Converting to QUBO
Many samplers, including OpenJij, operate by generating samples that minimize the objective function described in QUBO (Quadratic Unconstrained Binary Optimization) without constraints. The Traveling Salesman Problem formulated above has all binary variables but includes constraints, making it constrained. Therefore, we convert it to an unconstrained problem by embedding the constraints into the objective function using the penalty method. OMMX's [`Instance.uniform_penalty_method`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.Instance.uniform_penalty_method) converts a problem with equality constraints
$$
\begin{align*}
\min \quad &f(x) \\
\text{s.t.} \quad &g_i(x) = 0 \quad (\forall i)
\end{align*}
$$
into an unconstrained problem with a single parameter $\lambda$:
$$
\min \quad f(x) + \lambda \sum_i g_i(x)^2
$$
If you want to specify weight parameters for each constraint, you can use [`Instance.penalty_method`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.Instance.penalty_method) to convert it into
$$
\min \quad f(x) + \sum_i \lambda_i g_i(x)^2
$$
OMMX's `Instance.penalty_method` allows for specifying individual weight parameters for each constraint.
```python
parametric_qubo = instance.uniform_penalty_method()
```
Since this has parameters that are not decision variables, it becomes a `ommx.v1.ParametricInstance` instead of `ommx.v1.Instance`, which corresponds to the following parameterized QUBO:
$$
\min \quad \sum_{t=0}^{N-1} \sum_{i, j = 0}^{N-1} d(i, j) x_{t, i} x_{(t+1 \% N), j}
+ \lambda \left[ \sum_{t=0}^{N-1} \left(\sum_{i=0}^{N-1} x_{t, i} - 1\right)^2
+ \sum_{i=0}^{N-1} \left(\sum_{t=0}^{N-1} x_{t, i} - 1\right)^2 \right]
$$
You can check the parameters of the `ParametricInstance` using the `parameters` property.
```python
parametric_qubo.parameters
```
As explained above, `uniform_penalty_method` has a single penalty weight parameter, so there is only one parameter. To fix this parameter to $\lambda = 20.0$, use `with_parameters` to specify the parameter. This function takes a dictionary `dict[int, float]` that maps parameter IDs to values.
```python
weight = parametric_qubo.get_parameters()[0]
qubo = parametric_qubo.with_parameters({weight.id: 20.0})
```
The resulting `qubo` now has all parameters substituted, so it is an `ommx.v1.Instance` instead of an `ommx.v1.ParametricInstance`. Additionally, it is an unconstrained optimization problem without any constraints.
```python
assert qubo.get_constraints() == []
```
However, this `qubo` instance retains the information of the original constraints as `removed_constraints`. This information is used to verify whether the samples obtained from QUBO satisfy the original problem's constraints. Converting to QUBO is an extreme example, but it is common for users to preprocess their mathematical models in such a way that the original constraints become unnecessary before passing them to a solver. Even in such cases, users are interested in the original constraints they input, so `ommx.v1.Instance` includes a mechanism to retain this information.
```python
qubo.removed_constraints.head(2)
```
Note that the objective function of this `qubo` instance differs from the original problem's objective function. The `objective` value in subsequent processes refers to the value of this new objective function (commonly known as the energy value).
## Sampling with OpenJij
To sample the QUBO described by `ommx.v1.Instance` using OpenJij, use the `ommx-openjij-adapter`.
```python
import ommx_openjij_adapter as adapter
samples = adapter.sample_qubo_sa(qubo, num_reads=16)
sample_set = qubo.evaluate_samples(samples)
sample_set.summary
```
`ommx_openjij_adapter.sample_qubo_sa` returns `ommx.v1.Samples`, which can be passed to `Instance.evaluate_samples` to calculate the objective function values and constraint violations. The `SampleSet.summary` property is used to display summary information. `feasible` indicates the feasibility to **the original problem** before conversion to QUBO. This is calculated using the information stored in `removed_constraints` of the `qubo` instance.
To view the feasibility for each constraint, use the `summary_with_constraints` property.
```python
sample_set.summary_with_constraints
```
For more detailed information, you can use the `SampleSet.decision_variables` and `SampleSet.constraints` properties.
```python
sample_set.decision_variables.head(2)
```
```python
sample_set.constraints.head(2)
```
To obtain the samples, use the `SampleSet.extract_decision_variables` method. This interprets the samples using the `name` and `subscripts` registered when creating `ommx.v1.DecisionVariables`. For example, to get the value of the decision variable named `x` with `sample_id=1`, use the following to obtain it in the form of `dict[subscripts, value]`.
```python
sample_id = 1
x = sample_set.extract_decision_variables("x", sample_id)
t = 2
i = 3
x[(t, i)]
```
0.0
Since we obtained a sample for $x_{t, i}$, we convert this into a TSP path. This depends on the formulation used, so you need to write the processing yourself.
```python
def sample_to_path(sample: dict[tuple[int, ...], float]) -> list[int]:
path = []
for t in range(N):
for i in range(N):
if sample[(t, i)] == 1:
path.append(i)
return path
```
Let's display this. First, we obtain the IDs of samples that are feasible for the original problem.
```python
feasible_ids = sample_set.summary.query("feasible == True").index
feasible_ids
```
Index([2, 4, 15, 6, 12, 5, 8, 1, 13, 3, 7, 9], dtype='int64', name='sample_id')
Let's display the optimized paths for these samples.
```python
fig, axie = plt.subplots(3, 3, figsize=(12, 12))
for i, ax in enumerate(axie.flatten()):
if i >= len(feasible_ids):
break
s = feasible_ids[i]
x = sample_set.extract_decision_variables("x", s)
path = sample_to_path(x)
xs = [ulysses16_points[i][0] for i in path] + [ulysses16_points[path[0]][0]]
ys = [ulysses16_points[i][1] for i in path] + [ulysses16_points[path[0]][1]]
ax.plot(xs, ys, marker='o')
ax.set_title(f"Sample {s}, objective={sample_set.objectives[s]:.2f}")
plt.tight_layout()
plt.show()
```
### Switching Adapters
Solve with multiple adapters and compare the results
======================================================
Since the OMMX Adapter provides a unified API, you can solve the same problem using multiple solvers and compare the results. Let's consider a simple knapsack problem as an example:
$$
\begin{align*}
\mathrm{maximize} \quad & \sum_{i=0}^{N-1} v_i x_i \\
\mathrm{s.t.} \quad & \sum_{i=0}^{n-1} w_i x_i - W \leq 0, \\
& x_{i} \in \{ 0, 1\}
\end{align*}
$$
```python
from ommx.v1 import Instance, DecisionVariable
v = [10, 13, 18, 31, 7, 15]
w = [11, 25, 20, 35, 10, 33]
W = 47
N = len(v)
x = [
DecisionVariable.binary(
id=i,
name="x",
subscripts=[i],
)
for i in range(N)
]
instance = Instance.from_components(
decision_variables=x,
objective=sum(v[i] * x[i] for i in range(N)),
constraints=[sum(w[i] * x[i] for i in range(N)) - W <= 0],
sense=Instance.MAXIMIZE,
)
```
## Solve with multiple adapters
Here, we will use the following OSS solvers with corresponding adapters, which are developed as a part of OMMX Python SDK:
| Package name | PyPI | Backend |
|:--- |:--- |:--- |
| `ommx-python-mip-adapter` | [](https://pypi.org/project/ommx-python-mip-adapter/) | [CBC](https://github.com/coin-or/Cbc) via [Python-MIP](https://github.com/coin-or/python-mip) |
| `ommx-pyscipopt-adapter` | [](https://pypi.org/project/ommx-pyscipopt-adapter/) | [SCIP](https://github.com/scipopt/scip) via [PySCIPOpt](https://github.com/scipopt/PySCIPOpt) |
| `ommx-highs-adapter` | [](https://pypi.org/project/ommx-highs-adapter/) | [HiGHS](https://github.com/ERGO-Code/HiGHS) |
For non-OSS solvers, the following adapters also developed as separated repositories:
| Package name | PyPI | Backend |
|:--- |:--- |:--- |
| [ommx-gurobipy-adapter](https://github.com/Jij-Inc/ommx-gurobipy-adapter) | [](https://pypi.org/project/ommx-gurobipy-adapter/) | [Gurobi](https://www.gurobi.com/) |
| [ommx-fixstars-amplify-adapter](https://github.com/Jij-Inc/ommx-fixstars-amplify-adapter) | [](https://pypi.org/project/ommx-fixstars-amplify-adapter/) | [Fixstars Amplify](https://amplify.fixstars.com/ja/docs/amplify/v1/index.html#) |
Here, let's solve the knapsack problem with OSS solvers, Highs, Python-MIP (CBC), and SCIP.
```python
from ommx_python_mip_adapter import OMMXPythonMIPAdapter
from ommx_pyscipopt_adapter import OMMXPySCIPOptAdapter
from ommx_highs_adapter import OMMXHighsAdapter
# List of adapters to use
adapters = {
"highs": OMMXHighsAdapter,
"cbc": OMMXPythonMIPAdapter,
"scip": OMMXPySCIPOptAdapter,
}
# Solve the problem using each adapter
solutions = {
name: adapter.solve(instance) for name, adapter in adapters.items()
}
```
Cbc0038I Initial state - 1 integers unsatisfied sum - 0.457143
Cbc0038I Solution found of 28
Cbc0038I Before mini branch and bound, 5 integers at bound fixed and 0 continuous
Cbc0038I Full problem 1 rows 6 columns, reduced to 0 rows 0 columns
Cbc0038I Mini branch and bound did not improve solution (0.00 seconds)
Cbc0038I Round again with cutoff of 30.3171
Cbc0038I Reduced cost fixing fixed 1 variables on major pass 2
Cbc0038I Pass 1: suminf. 0.07474 (1) obj. 30.3171 iterations 1
Cbc0038I Pass 2: suminf. 0.45714 (1) obj. 42.1714 iterations 1
Cbc0038I Pass 3: suminf. 0.07474 (1) obj. 30.3171 iterations 1
Cbc0038I Pass 4: suminf. 0.45714 (1) obj. 42.1714 iterations 1
Cbc0038I Pass 5: suminf. 0.34461 (1) obj. 30.3171 iterations 2
Cbc0038I Solution found of 41
Cbc0038I Before mini branch and bound, 4 integers at bound fixed and 0 continuous
Cbc0038I Full problem 1 rows 6 columns, reduced to 1 rows 2 columns
Cbc0038I Mini branch and bound did not improve solution (0.01 seconds)
Cbc0038I Round again with cutoff of 42.0342
Cbc0038I Reduced cost fixing fixed 5 variables on major pass 3
Cbc0038I After 0.01 seconds - Feasibility pump exiting with objective of 41 - took 0.00 seconds
Cbc0012I Integer solution of 41 found by feasibility pump after 0 iterations and 0 nodes (0.01 seconds)
Cbc0038I Full problem 1 rows 6 columns, reduced to 0 rows 0 columns
Cbc0001I Search completed - best objective 41, took 0 iterations and 0 nodes (0.01 seconds)
Cbc0035I Maximum depth 0, 5 variables fixed on reduced cost
## Compare the results
Since this knapsack problem is simple, all solvers will find the optimal solution.
```python
from matplotlib import pyplot as plt
marks = {
"highs": "o",
"cbc": "x",
"scip": "+",
}
for name, solution in solutions.items():
x = solution.extract_decision_variables("x")
subscripts = [key[0] for key in x.keys()]
plt.plot(subscripts, x.values(), marks[name], label=name)
plt.legend()
```
<matplotlib.legend.Legend at 0x15aa93a40>
It would be convenient to concatenate the `pandas.DataFrame` obtained with `decision_variables` when analyzing the results of multiple solvers.
```python
import pandas
decision_variables = pandas.concat([
solution.decision_variables.assign(solver=solver)
for solver, solution in solutions.items()
])
decision_variables
```
### Share In Ommx Artifact
In mathematical optimization workflows, it is important to generate and manage a variety of data. Properly handling these data ensures reproducible computational results and allows teams to share information efficiently.
OMMX provides a straightforward and efficient way to manage different data types. Specifically, it defines a data format called an OMMX Artifact, which lets you store, organize, and share various optimization data through the OMMX SDK.
## Preparation: Data to Share
First, let's prepare the data we want to share. We will create an `ommx.v1.Instance` representing the 0-1 knapsack problem and solve it using SCIP. We will also share the results of our optimization analysis. Details are omitted for brevity.
```python
from ommx.v1 import Instance, DecisionVariable, Constraint
from ommx_pyscipopt_adapter.adapter import OMMXPySCIPOptAdapter
import pandas as pd
# Prepare data for the 0-1 knapsack problem
data = {
# Values of each item
"v": [10, 13, 18, 31, 7, 15],
# Weights of each item
"w": [11, 15, 20, 35, 10, 33],
# Knapsack capacity
"W": 47,
# Total number of items
"N": 6,
}
# Define decision variables
x = [
# Define binary variable x_i
DecisionVariable.binary(
# Specify the ID of the decision variable
id=i,
# Specify the name of the decision variable
name="x",
# Specify the subscript of the decision variable
subscripts=[i],
)
# Prepare num_items binary variables
for i in range(data["N"])
]
# Define the objective function
objective = sum(data["v"][i] * x[i] for i in range(data["N"]))
# Define constraints
constraint = Constraint(
# Name of the constraint
name = "Weight Limit",
# Specify the left-hand side of the constraint
function=sum(data["w"][i] * x[i] for i in range(data["N"])) - data["W"],
# Specify equality constraint (==0) or inequality constraint (<=0)
equality=Constraint.LESS_THAN_OR_EQUAL_TO_ZERO,
)
# Create an instance
instance = Instance.from_components(
# Register all decision variables included in the instance
decision_variables=x,
# Register the objective function
objective=objective,
# Register all constraints
constraints=[constraint],
# Specify that it is a maximization problem
sense=Instance.MAXIMIZE,
)
# Solve with SCIP
solution = OMMXPySCIPOptAdapter.solve(instance)
# Analyze the optimal solution
df_vars = solution.decision_variables
df = pd.DataFrame.from_dict(
{
"Item Number": df_vars.index,
"Put in Knapsack?": df_vars["value"].apply(lambda x: "Yes" if x == 1.0 else "No"),
}
)
```
```python
from myst_nb import glue
glue("instance", instance, display=False)
glue("solution", solution, display=False)
glue("data", data, display=False)
glue("df", df, display=False)
```
```{list-table}
:header-rows: 1
:widths: 5 30 10
* - Variable Name
- Description
- Value
* - `instance`
- `ommx.v1.Instance` object representing the 0-1 knapsack problem
- ````{toggle}
```{glue:} instance
```
````
* - `solution`
- `ommx.v1.Solution` object containing the results of solving the 0-1 knapsack problem with SCIP
- ````{toggle}
```{glue:} solution
```
````
* - `data`
- Input data for the 0-1 knapsack problem
- ```{glue:} data
```
* - `df`
- `pandas.DataFrame` object representing the optimal solution of the 0-1 knapsack problem
- {glue:}`df`
```
## Creating an OMMX Artifact as a File
OMMX Artifacts can be managed as files or by assigning them container-like names. Here, we'll show how to save the data as a file. Using the OMMX SDK, we'll store the data in a new file called `my_instance.ommx`. First, we need an `ArtifactBuilder`.
```python
import os
from ommx.artifact import ArtifactBuilder
# Specify the name of the OMMX Artifact file
filename = "my_instance.ommx"
# If the file already exists, remove it
if os.path.exists(filename):
os.remove(filename)
# 1. Create a builder to create the OMMX Artifact file
builder = ArtifactBuilder.new_archive_unnamed(filename)
```
[`ArtifactBuilder`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/artifact/index.html#ommx.artifact.ArtifactBuilder) has several constructors, allowing you to choose whether to manage it by name like a container or as an archive file. If you use a container registry to push and pull like a container, a name is required, but if you use an archive file, a name is not necessary. Here, we use `ArtifactBuilder.new_archive_unnamed` to manage it as an archive file.
| Constructor | Description |
| --- | --- |
| [`ArtifactBuilder.new`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/artifact/index.html#ommx.artifact.ArtifactBuilder.new) | Manage by name like a container |
| [`ArtifactBuilder.new_archive`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/artifact/index.html#ommx.artifact.ArtifactBuilder.new_archive) | Manage as both an archive file and a container |
| [`ArtifactBuilder.new_archive_unnamed`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/artifact/index.html#ommx.artifact.ArtifactBuilder.new_archive_unnamed) | Manage as an archive file |
| [`ArtifactBuilder.for_github`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/artifact/index.html#ommx.artifact.ArtifactBuilder.for_github) | Determine the container name according to the GitHub Container Registry |
Regardless of the initialization method, you can save `ommx.v1.Instance` and other data in the same way. Let's add the data prepared above.
```python
# Add ommx.v1.Instance object
desc_instance = builder.add_instance(instance)
# Add ommx.v1.Solution object
desc_solution = builder.add_solution(solution)
# Add pandas.DataFrame object
desc_df = builder.add_dataframe(df, title="Optimal Solution of Knapsack Problem")
# Add an object that can be converted to JSON
desc_json = builder.add_json(data, title="Data of Knapsack Problem")
```
In OMMX Artifacts, data is stored in layers, each with a dedicated media type. Functions like `add_instance` automatically set these media types and add layers. These functions return a `Description` object with information about each created layer.
```python
desc_json.to_dict()
```
{'mediaType': 'application/json',
'digest': 'sha256:6cbfaaa7f97e84d8b46da95b81cf4d5158df3a9bd439f8c60be26adaa16ab3cf',
'size': 78,
'annotations': {'org.ommx.user.title': 'Data of Knapsack Problem'}}
The part added as `title="..."` in `add_json` is saved as an annotation of the layer. OMMX Artifact is a data format for humans, so this is basically information for humans to read. The `ArtifactBuilder.add_*` functions all accept optional keyword arguments and automatically convert them to the `org.ommx.user.` namespace.
Finally, call `build` to save it to a file.
```python
# 3. Create the OMMX Artifact file
artifact = builder.build()
```
This `artifact` is the same as the one that will be explained in the next section, which is the one you just saved. Let's check if the file has been created:
```python
! ls $filename
```
my_instance.ommx
Now you can share this `my_instance.ommx` with others using the usual file sharing methods.