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 (
get_objective_data,
random_inputs,
get_constraint_data,
get_feasibility_data,
normalize_inputs,
denormalize_inputs,
)
from gest_api.vocs import MaximizeObjective
import pandas as pd
import numpy as np
import yaml
from xopt.vocs import VOCS
from xopt.vocs import (
get_objective_data,
random_inputs,
get_constraint_data,
get_feasibility_data,
normalize_inputs,
denormalize_inputs,
)
from gest_api.vocs import MaximizeObjective
import pandas as pd
import numpy as np
import yaml
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: 123
"""
vocs = VOCS(**yaml.safe_load(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: 123
"""
vocs = VOCS(**yaml.safe_load(Y))
vocs
Out[2]:
VOCS(variables={'a': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]), 'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])}, objectives={'c': MaximizeObjective(dtype=None), 'd': MinimizeObjective(dtype=None)}, constraints={'e': LessThanConstraint(dtype=None, value=2.0), 'f': GreaterThanConstraint(dtype=None, value=0.0)}, constants={'g': Constant(dtype=None, value=123)}, observables={})
In [3]:
Copied!
# as dict
dict(vocs)
# as dict
dict(vocs)
Out[3]:
{'variables': {'a': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]),
'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])},
'objectives': {'c': MaximizeObjective(dtype=None),
'd': MinimizeObjective(dtype=None)},
'constraints': {'e': LessThanConstraint(dtype=None, value=2.0),
'f': GreaterThanConstraint(dtype=None, value=0.0)},
'constants': {'g': Constant(dtype=None, value=123)},
'observables': {}}
In [4]:
Copied!
# re-parse dict
vocs2 = VOCS(**dict(vocs))
# re-parse dict
vocs2 = VOCS(**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': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]),
'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])}
In [7]:
Copied!
vocs.objectives["c"]
vocs.objectives["c"]
Out[7]:
MaximizeObjective(dtype=None)
In [8]:
Copied!
isinstance(vocs.objectives["c"], MaximizeObjective)
isinstance(vocs.objectives["c"], MaximizeObjective)
Out[8]:
True
In [9]:
Copied!
# json
vocs.model_dump_json()
# json
vocs.model_dump_json()
Out[9]:
'{"variables":{"a":{"dtype":null,"default_value":null,"domain":[0.0,1000.0],"type":"ContinuousVariable"},"b":{"dtype":null,"default_value":null,"domain":[-1.0,1.0],"type":"ContinuousVariable"}},"objectives":{"c":{"dtype":null,"type":"MaximizeObjective"},"d":{"dtype":null,"type":"MinimizeObjective"}},"constraints":{"e":{"dtype":null,"value":2.0,"type":"LessThanConstraint"},"f":{"dtype":null,"value":0.0,"type":"GreaterThanConstraint"}},"constants":{"g":{"dtype":null,"value":123,"type":"Constant"}},"observables":{}}'
Objective Evaluation¶
In [10]:
Copied!
data = pd.DataFrame(random_inputs(vocs, 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(random_inputs(vocs, 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[10]:
| a | b | g | c | d | e | f | |
|---|---|---|---|---|---|---|---|
| 5 | 532.169931 | -0.576752 | 123 | 531.593179 | 532.746683 | 1063.186358 | 1065.493366 |
| 6 | 726.108222 | -0.286606 | 123 | 725.821615 | 726.394828 | 1451.643231 | 1452.789656 |
| 7 | 180.045915 | -0.550618 | 123 | 179.495297 | 180.596532 | 358.990594 | 361.193064 |
| 8 | 233.266981 | -0.112140 | 123 | 233.154841 | 233.379121 | 466.309682 | 466.758243 |
| 9 | 874.790760 | -0.342284 | 123 | 874.448476 | 875.133043 | 1748.896951 | 1750.266087 |
| 10 | 671.235329 | -0.282035 | 123 | 670.953294 | 671.517365 | 1341.906588 | 1343.034730 |
| 11 | 275.051286 | 0.299341 | 123 | 275.350627 | 274.751945 | 550.701253 | 549.503889 |
| 12 | 922.021846 | -0.244697 | 123 | 921.777149 | 922.266543 | 1843.554298 | 1844.533086 |
| 13 | 609.894418 | 0.569164 | 123 | 610.463582 | 609.325254 | 1220.927164 | 1218.650508 |
| 14 | 995.730073 | -0.913090 | 123 | 994.816984 | 996.643163 | 1989.633967 | 1993.286326 |
In [11]:
Copied!
# These are in standard form for minimization, available as a function
get_objective_data(vocs, data)
# These are in standard form for minimization, available as a function
get_objective_data(vocs, data)
Out[11]:
| objective_c | objective_d | |
|---|---|---|
| 5 | -531.593179 | 532.746683 |
| 6 | -725.821615 | 726.394828 |
| 7 | -179.495297 | 180.596532 |
| 8 | -233.154841 | 233.379121 |
| 9 | -874.448476 | 875.133043 |
| 10 | -670.953294 | 671.517365 |
| 11 | -275.350627 | 274.751945 |
| 12 | -921.777149 | 922.266543 |
| 13 | -610.463582 | 609.325254 |
| 14 | -994.816984 | 996.643163 |
In [12]:
Copied!
# use the to_numpy() method to convert for low level use.
get_objective_data(vocs, data).to_numpy()
# use the to_numpy() method to convert for low level use.
get_objective_data(vocs, data).to_numpy()
Out[12]:
array([[-531.59317925, 532.74668294],
[-725.82161529, 726.39482793],
[-179.4952971 , 180.59653212],
[-233.15484092, 233.37912129],
[-874.44847572, 875.1330433 ],
[-670.95329414, 671.51736475],
[-275.35062664, 274.7519446 ],
[-921.77714882, 922.2665431 ],
[-610.46358189, 609.32525419],
[-994.81698371, 996.64316278]])
In [13]:
Copied!
get_constraint_data(vocs, data)
get_constraint_data(vocs, data)
Out[13]:
| constraint_e | constraint_f | |
|---|---|---|
| 5 | 1061.186358 | -1065.493366 |
| 6 | 1449.643231 | -1452.789656 |
| 7 | 356.990594 | -361.193064 |
| 8 | 464.309682 | -466.758243 |
| 9 | 1746.896951 | -1750.266087 |
| 10 | 1339.906588 | -1343.034730 |
| 11 | 548.701253 | -549.503889 |
| 12 | 1841.554298 | -1844.533086 |
| 13 | 1218.927164 | -1218.650508 |
| 14 | 1987.633967 | -1993.286326 |
In [14]:
Copied!
get_feasibility_data(vocs, data)
get_feasibility_data(vocs, data)
Out[14]:
| 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 [15]:
Copied!
# normalize inputs to unit domain [0,1]
normed_data = normalize_inputs(vocs, data)
normed_data
# normalize inputs to unit domain [0,1]
normed_data = normalize_inputs(vocs, data)
normed_data
Out[15]:
| a | b | |
|---|---|---|
| 5 | 0.532170 | 0.211624 |
| 6 | 0.726108 | 0.356697 |
| 7 | 0.180046 | 0.224691 |
| 8 | 0.233267 | 0.443930 |
| 9 | 0.874791 | 0.328858 |
| 10 | 0.671235 | 0.358982 |
| 11 | 0.275051 | 0.649671 |
| 12 | 0.922022 | 0.377651 |
| 13 | 0.609894 | 0.784582 |
| 14 | 0.995730 | 0.043455 |
In [16]:
Copied!
# and denormalize
denormalize_inputs(vocs, normed_data)
# and denormalize
denormalize_inputs(vocs, normed_data)
Out[16]:
| a | b | |
|---|---|---|
| 5 | 532.169931 | -0.576752 |
| 6 | 726.108222 | -0.286606 |
| 7 | 180.045915 | -0.550618 |
| 8 | 233.266981 | -0.112140 |
| 9 | 874.790760 | -0.342284 |
| 10 | 671.235329 | -0.282035 |
| 11 | 275.051286 | 0.299341 |
| 12 | 922.021846 | -0.244697 |
| 13 | 609.894418 | 0.569164 |
| 14 | 995.730073 | -0.913090 |
Error handling¶
In [17]:
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(**yaml.safe_load(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(**yaml.safe_load(Y))
In [18]:
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[18]:
0 NaN 1 NaN 2 NaN Name: b, dtype: float64
In [19]:
Copied!
data["a"] = np.nan
data["a"] = np.nan
In [20]:
Copied!
a = 2
def f(x=a):
return x
a = 99
f()
a = 2
def f(x=a):
return x
a = 99
f()
Out[20]:
2
In [21]:
Copied!
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
Out[21]:
| A | |
|---|---|
| 1 | 6.000000e+66 |
| 2 | 6.000000e+66 |
| 3 | 6.000000e+66 |
In [22]:
Copied!
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
get_objective_data(vocs, data)
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
get_objective_data(vocs, data)
Out[22]:
| objective_c | objective_d | |
|---|---|---|
| 9 | -1.0 | inf |
| 3 | -2.0 | inf |
| 4 | -3.0 | inf |
| 5 | -4.0 | inf |