VOCS data structure¶
Variables, Objectives, Constraints, and other Settings (VOCS) helps define our optimization problems.
In [1]:
Copied!
from xopt.vocs import VOCS
from xopt.vocs import form_objective_data
import pandas as pd
import numpy as np
from xopt.vocs import VOCS
from xopt.vocs import form_objective_data
import pandas as pd
import numpy as np
In [2]:
Copied!
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS.from_yaml(Y)
vocs
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS.from_yaml(Y)
vocs
Out[2]:
VOCS(variables={'a': (0.0, 1000.0), 'b': (-1.0, 1.0)}, constraints={'e': ('LESS_THAN', 2.0), 'f': ('GREATER_THAN', 0.0)}, objectives={'c': 'MAXIMIZE', 'd': 'MINIMIZE'}, constants={'g': 1234}, observables=[])
In [3]:
Copied!
# as dict
dict(vocs)
# as dict
dict(vocs)
Out[3]:
{'variables': {'a': (0.0, 1000.0), 'b': (-1.0, 1.0)},
'constraints': {'e': ('LESS_THAN', 2.0), 'f': ('GREATER_THAN', 0.0)},
'objectives': {'c': 'MAXIMIZE', 'd': 'MINIMIZE'},
'constants': {'g': 1234},
'observables': []}
In [4]:
Copied!
# re-parse dict
vocs2 = VOCS.from_dict(dict(vocs))
# re-parse dict
vocs2 = VOCS.from_dict(dict(vocs))
In [5]:
Copied!
# Check that these are the same
vocs2 == vocs
# Check that these are the same
vocs2 == vocs
Out[5]:
True
In [6]:
Copied!
# This replaces the old vocs["variables"]
getattr(vocs, "variables")
# This replaces the old vocs["variables"]
getattr(vocs, "variables")
Out[6]:
{'a': (0.0, 1000.0), 'b': (-1.0, 1.0)}
In [7]:
Copied!
vocs.objectives["c"] == "MAXIMIZE"
vocs.objectives["c"] == "MAXIMIZE"
Out[7]:
True
In [8]:
Copied!
# json
vocs.to_json()
# json
vocs.to_json()
Out[8]:
'{"variables":{"a":[0.0,1000.0],"b":[-1.0,1.0]},"constraints":{"e":["LESS_THAN",2.0],"f":["GREATER_THAN",0.0]},"objectives":{"c":"MAXIMIZE","d":"MINIMIZE"},"constants":{"g":1234},"observables":[]}'
Objective Evaluation¶
In [9]:
Copied!
data = pd.DataFrame(vocs.random_inputs(10))
# Add some outputs
data["c"] = data["a"] + data["b"]
data["d"] = data["a"] - data["b"]
data["e"] = data["a"] * 2 + data["b"] * 2
data["f"] = data["a"] * 2 - data["b"] * 2
data.index = np.arange(len(data)) + 5 # custom index
data
data = pd.DataFrame(vocs.random_inputs(10))
# Add some outputs
data["c"] = data["a"] + data["b"]
data["d"] = data["a"] - data["b"]
data["e"] = data["a"] * 2 + data["b"] * 2
data["f"] = data["a"] * 2 - data["b"] * 2
data.index = np.arange(len(data)) + 5 # custom index
data
Out[9]:
| a | b | g | c | d | e | f | |
|---|---|---|---|---|---|---|---|
| 5 | 149.301121 | 0.789104 | 1234 | 150.090224 | 148.512017 | 300.180448 | 297.024034 |
| 6 | 161.256076 | -0.589203 | 1234 | 160.666873 | 161.845279 | 321.333745 | 323.690557 |
| 7 | 938.235497 | -0.317351 | 1234 | 937.918147 | 938.552848 | 1875.836294 | 1877.105696 |
| 8 | 435.604227 | 0.200648 | 1234 | 435.804875 | 435.403579 | 871.609749 | 870.807158 |
| 9 | 173.347787 | -0.126612 | 1234 | 173.221175 | 173.474398 | 346.442350 | 346.948797 |
| 10 | 747.584289 | -0.471892 | 1234 | 747.112397 | 748.056181 | 1494.224794 | 1496.112362 |
| 11 | 296.482495 | -0.115261 | 1234 | 296.367234 | 296.597757 | 592.734468 | 593.195514 |
| 12 | 192.259903 | 0.040091 | 1234 | 192.299994 | 192.219812 | 384.599988 | 384.439624 |
| 13 | 69.311679 | 0.011982 | 1234 | 69.323661 | 69.299697 | 138.647322 | 138.599393 |
| 14 | 384.344417 | -0.261956 | 1234 | 384.082461 | 384.606373 | 768.164923 | 769.212746 |
In [10]:
Copied!
vocs.objectives
vocs.objectives
Out[10]:
{'c': 'MAXIMIZE', 'd': 'MINIMIZE'}
In [11]:
Copied!
# These are in standard form for minimization
form_objective_data(vocs.objectives, data)
# These are in standard form for minimization
form_objective_data(vocs.objectives, data)
Out[11]:
| objective_c | objective_d | |
|---|---|---|
| 5 | -150.090224 | 148.512017 |
| 6 | -160.666873 | 161.845279 |
| 7 | -937.918147 | 938.552848 |
| 8 | -435.804875 | 435.403579 |
| 9 | -173.221175 | 173.474398 |
| 10 | -747.112397 | 748.056181 |
| 11 | -296.367234 | 296.597757 |
| 12 | -192.299994 | 192.219812 |
| 13 | -69.323661 | 69.299697 |
| 14 | -384.082461 | 384.606373 |
In [12]:
Copied!
# This is also available as a method
vocs.objective_data(data)
# This is also available as a method
vocs.objective_data(data)
Out[12]:
| objective_c | objective_d | |
|---|---|---|
| 5 | -150.090224 | 148.512017 |
| 6 | -160.666873 | 161.845279 |
| 7 | -937.918147 | 938.552848 |
| 8 | -435.804875 | 435.403579 |
| 9 | -173.221175 | 173.474398 |
| 10 | -747.112397 | 748.056181 |
| 11 | -296.367234 | 296.597757 |
| 12 | -192.299994 | 192.219812 |
| 13 | -69.323661 | 69.299697 |
| 14 | -384.082461 | 384.606373 |
In [13]:
Copied!
# use the to_numpy() method to convert for low level use.
vocs.objective_data(data).to_numpy()
# use the to_numpy() method to convert for low level use.
vocs.objective_data(data).to_numpy()
Out[13]:
array([[-150.09022424, 148.51201678],
[-160.66687258, 161.8452786 ],
[-937.91814694, 938.55284805],
[-435.80487457, 435.40357914],
[-173.22117499, 173.47439831],
[-747.11239683, 748.05618119],
[-296.36723393, 296.59775685],
[-192.29999407, 192.2198119 ],
[ -69.32366102, 69.29969668],
[-384.08246139, 384.60637304]])
In [14]:
Copied!
vocs.constraint_data(data)
vocs.constraint_data(data)
Out[14]:
| constraint_e | constraint_f | |
|---|---|---|
| 5 | 298.180448 | -297.024034 |
| 6 | 319.333745 | -323.690557 |
| 7 | 1873.836294 | -1877.105696 |
| 8 | 869.609749 | -870.807158 |
| 9 | 344.442350 | -346.948797 |
| 10 | 1492.224794 | -1496.112362 |
| 11 | 590.734468 | -593.195514 |
| 12 | 382.599988 | -384.439624 |
| 13 | 136.647322 | -138.599393 |
| 14 | 766.164923 | -769.212746 |
In [15]:
Copied!
vocs.feasibility_data(data)
vocs.feasibility_data(data)
Out[15]:
| feasible_e | feasible_f | feasible | |
|---|---|---|---|
| 5 | False | True | False |
| 6 | False | True | False |
| 7 | False | True | False |
| 8 | False | True | False |
| 9 | False | True | False |
| 10 | False | True | False |
| 11 | False | True | False |
| 12 | False | True | False |
| 13 | False | True | False |
| 14 | False | True | False |
In [16]:
Copied!
# normalize inputs to unit domain [0,1]
normed_data = vocs.normalize_inputs(data)
normed_data
# normalize inputs to unit domain [0,1]
normed_data = vocs.normalize_inputs(data)
normed_data
Out[16]:
| a | b | |
|---|---|---|
| 5 | 0.149301 | 0.894552 |
| 6 | 0.161256 | 0.205398 |
| 7 | 0.938235 | 0.341325 |
| 8 | 0.435604 | 0.600324 |
| 9 | 0.173348 | 0.436694 |
| 10 | 0.747584 | 0.264054 |
| 11 | 0.296482 | 0.442369 |
| 12 | 0.192260 | 0.520046 |
| 13 | 0.069312 | 0.505991 |
| 14 | 0.384344 | 0.369022 |
In [17]:
Copied!
# and denormalize
vocs.denormalize_inputs(normed_data)
# and denormalize
vocs.denormalize_inputs(normed_data)
Out[17]:
| a | b | |
|---|---|---|
| 5 | 149.301121 | 0.789104 |
| 6 | 161.256076 | -0.589203 |
| 7 | 938.235497 | -0.317351 |
| 8 | 435.604227 | 0.200648 |
| 9 | 173.347787 | -0.126612 |
| 10 | 747.584289 | -0.471892 |
| 11 | 296.482495 | -0.115261 |
| 12 | 192.259903 | 0.040091 |
| 13 | 69.311679 | 0.011982 |
| 14 | 384.344417 | -0.261956 |
Error handling¶
In [18]:
Copied!
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS.from_yaml(Y)
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS.from_yaml(Y)
In [19]:
Copied!
d = {"a": [1, 2, 3]}
df = pd.DataFrame(d)
df2 = pd.DataFrame(df).copy()
df2["b"] = np.nan
df2["b"] - 1
d = {"a": [1, 2, 3]}
df = pd.DataFrame(d)
df2 = pd.DataFrame(df).copy()
df2["b"] = np.nan
df2["b"] - 1
Out[19]:
0 NaN 1 NaN 2 NaN Name: b, dtype: float64
In [20]:
Copied!
data["a"] = np.nan
data["a"] = np.nan
In [21]:
Copied!
a = 2
def f(x=a):
return x
a = 99
f()
a = 2
def f(x=a):
return x
a = 99
f()
Out[21]:
2
In [22]:
Copied!
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
Out[22]:
| A | |
|---|---|
| 1 | 6.000000e+66 |
| 2 | 6.000000e+66 |
| 3 | 6.000000e+66 |
In [23]:
Copied!
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
form_objective_data(vocs.objectives, data)
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
form_objective_data(vocs.objectives, data)
Out[23]:
| objective_c | objective_d | |
|---|---|---|
| 9 | -1.0 | inf |
| 3 | -2.0 | inf |
| 4 | -3.0 | inf |
| 5 | -4.0 | inf |