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 | 449.054041 | 0.072177 | 1234 | 449.126217 | 448.981864 | 898.252434 | 897.963728 |
6 | 884.358091 | -0.858172 | 1234 | 883.499919 | 885.216263 | 1766.999838 | 1770.432525 |
7 | 381.443598 | -0.879676 | 1234 | 380.563923 | 382.323274 | 761.127846 | 764.646548 |
8 | 496.098734 | -0.818988 | 1234 | 495.279746 | 496.917722 | 990.559492 | 993.835443 |
9 | 893.855142 | -0.103630 | 1234 | 893.751512 | 893.958773 | 1787.503024 | 1787.917546 |
10 | 982.108468 | -0.295690 | 1234 | 981.812778 | 982.404158 | 1963.625557 | 1964.808316 |
11 | 741.449439 | -0.756023 | 1234 | 740.693416 | 742.205462 | 1481.386832 | 1484.410923 |
12 | 677.548089 | -0.895149 | 1234 | 676.652939 | 678.443238 | 1353.305878 | 1356.886476 |
13 | 234.039916 | -0.089959 | 1234 | 233.949957 | 234.129874 | 467.899913 | 468.259749 |
14 | 997.441646 | 0.528358 | 1234 | 997.970003 | 996.913288 | 1995.940007 | 1993.826576 |
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 | -449.126217 | 448.981864 |
6 | -883.499919 | 885.216263 |
7 | -380.563923 | 382.323274 |
8 | -495.279746 | 496.917722 |
9 | -893.751512 | 893.958773 |
10 | -981.812778 | 982.404158 |
11 | -740.693416 | 742.205462 |
12 | -676.652939 | 678.443238 |
13 | -233.949957 | 234.129874 |
14 | -997.970003 | 996.913288 |
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 | -449.126217 | 448.981864 |
6 | -883.499919 | 885.216263 |
7 | -380.563923 | 382.323274 |
8 | -495.279746 | 496.917722 |
9 | -893.751512 | 893.958773 |
10 | -981.812778 | 982.404158 |
11 | -740.693416 | 742.205462 |
12 | -676.652939 | 678.443238 |
13 | -233.949957 | 234.129874 |
14 | -997.970003 | 996.913288 |
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([[-449.12621722, 448.98186411], [-883.49991894, 885.21626252], [-380.56392278, 382.32327383], [-495.27974621, 496.91772154], [-893.75151215, 893.95877279], [-981.81277845, 982.40415818], [-740.69341602, 742.20546167], [-676.65293909, 678.44323805], [-233.9499567 , 234.12987442], [-997.9700035 , 996.91328789]])
In [14]:
Copied!
vocs.constraint_data(data)
vocs.constraint_data(data)
Out[14]:
constraint_e | constraint_f | |
---|---|---|
5 | 896.252434 | -897.963728 |
6 | 1764.999838 | -1770.432525 |
7 | 759.127846 | -764.646548 |
8 | 988.559492 | -993.835443 |
9 | 1785.503024 | -1787.917546 |
10 | 1961.625557 | -1964.808316 |
11 | 1479.386832 | -1484.410923 |
12 | 1351.305878 | -1356.886476 |
13 | 465.899913 | -468.259749 |
14 | 1993.940007 | -1993.826576 |
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.449054 | 0.536088 |
6 | 0.884358 | 0.070914 |
7 | 0.381444 | 0.060162 |
8 | 0.496099 | 0.090506 |
9 | 0.893855 | 0.448185 |
10 | 0.982108 | 0.352155 |
11 | 0.741449 | 0.121989 |
12 | 0.677548 | 0.052425 |
13 | 0.234040 | 0.455021 |
14 | 0.997442 | 0.764179 |
In [17]:
Copied!
# and denormalize
vocs.denormalize_inputs(normed_data)
# and denormalize
vocs.denormalize_inputs(normed_data)
Out[17]:
a | b | |
---|---|---|
5 | 449.054041 | 0.072177 |
6 | 884.358091 | -0.858172 |
7 | 381.443598 | -0.879676 |
8 | 496.098734 | -0.818988 |
9 | 893.855142 | -0.103630 |
10 | 982.108468 | -0.295690 |
11 | 741.449439 | -0.756023 |
12 | 677.548089 | -0.895149 |
13 | 234.039916 | -0.089959 |
14 | 997.441646 | 0.528358 |
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 |