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 | 677.206382 | 0.349897 | 1234 | 677.556279 | 676.856485 | 1355.112558 | 1353.712970 |
6 | 640.383761 | -0.122515 | 1234 | 640.261246 | 640.506277 | 1280.522493 | 1281.012553 |
7 | 21.886752 | -0.733619 | 1234 | 21.153133 | 22.620370 | 42.306265 | 45.240741 |
8 | 771.002422 | 0.757296 | 1234 | 771.759718 | 770.245127 | 1543.519436 | 1540.490253 |
9 | 172.645586 | -0.014281 | 1234 | 172.631305 | 172.659867 | 345.262610 | 345.319735 |
10 | 246.693018 | -0.992276 | 1234 | 245.700742 | 247.685294 | 491.401483 | 495.370589 |
11 | 3.109655 | -0.497302 | 1234 | 2.612353 | 3.606957 | 5.224706 | 7.213914 |
12 | 245.394749 | -0.975014 | 1234 | 244.419736 | 246.369763 | 488.839471 | 492.739526 |
13 | 149.887366 | 0.760326 | 1234 | 150.647693 | 149.127040 | 301.295385 | 298.254080 |
14 | 941.212874 | 0.780296 | 1234 | 941.993169 | 940.432578 | 1883.986339 | 1880.865156 |
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 | -677.556279 | 676.856485 |
6 | -640.261246 | 640.506277 |
7 | -21.153133 | 22.620370 |
8 | -771.759718 | 770.245127 |
9 | -172.631305 | 172.659867 |
10 | -245.700742 | 247.685294 |
11 | -2.612353 | 3.606957 |
12 | -244.419736 | 246.369763 |
13 | -150.647693 | 149.127040 |
14 | -941.993169 | 940.432578 |
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 | -677.556279 | 676.856485 |
6 | -640.261246 | 640.506277 |
7 | -21.153133 | 22.620370 |
8 | -771.759718 | 770.245127 |
9 | -172.631305 | 172.659867 |
10 | -245.700742 | 247.685294 |
11 | -2.612353 | 3.606957 |
12 | -244.419736 | 246.369763 |
13 | -150.647693 | 149.127040 |
14 | -941.993169 | 940.432578 |
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([[-677.55627895, 676.85648497], [-640.26124637, 640.50627655], [ -21.15313259, 22.62037049], [-771.7597181 , 770.24512668], [-172.63130502, 172.65986741], [-245.70074162, 247.68529437], [ -2.61235303, 3.6069568 ], [-244.41973571, 246.36976293], [-150.64769257, 149.12704009], [-941.99316938, 940.43257794]])
In [14]:
Copied!
vocs.constraint_data(data)
vocs.constraint_data(data)
Out[14]:
constraint_e | constraint_f | |
---|---|---|
5 | 1353.112558 | -1353.712970 |
6 | 1278.522493 | -1281.012553 |
7 | 40.306265 | -45.240741 |
8 | 1541.519436 | -1540.490253 |
9 | 343.262610 | -345.319735 |
10 | 489.401483 | -495.370589 |
11 | 3.224706 | -7.213914 |
12 | 486.839471 | -492.739526 |
13 | 299.295385 | -298.254080 |
14 | 1881.986339 | -1880.865156 |
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.677206 | 0.674948 |
6 | 0.640384 | 0.438742 |
7 | 0.021887 | 0.133191 |
8 | 0.771002 | 0.878648 |
9 | 0.172646 | 0.492859 |
10 | 0.246693 | 0.003862 |
11 | 0.003110 | 0.251349 |
12 | 0.245395 | 0.012493 |
13 | 0.149887 | 0.880163 |
14 | 0.941213 | 0.890148 |
In [17]:
Copied!
# and denormalize
vocs.denormalize_inputs(normed_data)
# and denormalize
vocs.denormalize_inputs(normed_data)
Out[17]:
a | b | |
---|---|---|
5 | 677.206382 | 0.349897 |
6 | 640.383761 | -0.122515 |
7 | 21.886752 | -0.733619 |
8 | 771.002422 | 0.757296 |
9 | 172.645586 | -0.014281 |
10 | 246.693018 | -0.992276 |
11 | 3.109655 | -0.497302 |
12 | 245.394749 | -0.975014 |
13 | 149.887366 | 0.760326 |
14 | 941.212874 | 0.780296 |
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 |