Example for the serialization of a campaign

This example shows how to serialize and also de-serialize a campaign. It demonstrates and shows that the “original” and “new” objects behave the same.

This example assumes some basic familiarity with using BayBE. We thus refer to campaign for a basic example.

Necessary imports

import numpy as np
from baybe import Campaign
from baybe.objective import Objective
from baybe.parameters import (
    CategoricalParameter,
    NumericalDiscreteParameter,
)
from baybe.recommenders import (
    FPSRecommender,
    SequentialGreedyRecommender,
    TwoPhaseMetaRecommender,
)
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget

Experiment setup

parameters = [
    CategoricalParameter(
        name="Granularity",
        values=["coarse", "medium", "fine"],
        encoding="OHE",
    ),
    NumericalDiscreteParameter(
        name="Pressure[bar]",
        values=[1, 5, 10],
        tolerance=0.2,
    ),
    NumericalDiscreteParameter(
        name="Temperature[degree_C]",
        values=np.linspace(100, 200, 10),
    ),
]

Creating the campaign

campaign = Campaign(
    searchspace=SearchSpace.from_product(parameters=parameters, constraints=None),
    objective=Objective(
        mode="SINGLE", targets=[NumericalTarget(name="Yield", mode="MAX")]
    ),
    recommender=TwoPhaseMetaRecommender(
        recommender=SequentialGreedyRecommender(),
        initial_recommender=FPSRecommender(),
    ),
)

Serialization and de-serialization

We begin by printing the original campaign

print("Original object")
print(campaign, end="\n" * 3)
Original object
Campaign
         
 Meta Data
 Batches Done: 0

Fits Done: 0

 Search Space
          
  Search Space Type: DISCRETE
  
  Discrete Search Space
               
   Discrete Parameters
                       Name                        Type  Num_Values

Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None

   Experimental Representation
               
  Granularity  Pressure[bar]  Temperature[degree_C]
   0       coarse            1.0             100.000000
   1       coarse            1.0             111.111111
   2       coarse            1.0             122.222222
   ..         ...            ...                    ...
   87        fine           10.0             177.777778
   88        fine           10.0             188.888889
   89        fine           10.0             200.000000
   
   [90 rows x 3 columns]
   
   Metadata:

was_recommended: 0/90

was_measured: 0/90

dont_recommend: 0/90

   Constraints
   Empty DataFrame
   Columns: []
   Index: []
               
   Computational Representation
               
   Granularity_coarse  Granularity_medium  ...  Pressure[bar]  Temperature[degree_C]
   0                    1                   0  ...            1.0

100.000000 1 1 0 … 1.0 111.111111 2 1 0 … 1.0 122.222222 .. … … … … … 87 0 0 … 10.0 177.777778 88 0 0 … 10.0 188.888889 89 0 0 … 10.0 200.000000

   [90 rows x 5 columns]
 
 Objective
          
  Mode: SINGLE
          
  Targets 
                Type   Name Mode  Lower_Bound  Upper_Bound Transformation  \
  0  NumericalTarget  Yield  MAX         -inf          inf           None   
  
     Weight  
  0   100.0  
          
  Combine Function: GEOM_MEAN
 
 TwoPhaseMetaRecommender(allow_repeated_recommendations=None,

allow_recommending_already_measured=None, initial_recommender=FPSRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True), recommender=SequentialGreedyRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True, surrogate_model=GaussianProcessSurrogate(model_params={}, _model=None), acquisition_function_cls=’qEI’, _acquisition_function=None, hybrid_sampler=’None’, sampling_percentage=1.0), switch_after=1)

We next serialize the campaign to JSON. This yields a JSON representation in string format. Since it is rather complex, we do not print this string here. Note: Dataframes are binary-encoded and are hence not human-readable.

string = campaign.to_json()

Deserialize the JSON string back to an object.

print("Deserialized object")
campaign_recreate = Campaign.from_json(string)
print(campaign_recreate, end="\n" * 3)
Deserialized object
Campaign
         
 Meta Data
 Batches Done: 0

Fits Done: 0

 Search Space
          
  Search Space Type: DISCRETE
  
  Discrete Search Space
               
   Discrete Parameters
                       Name                        Type  Num_Values

Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None

   Experimental Representation
               
  Granularity  Pressure[bar]  Temperature[degree_C]
   0       coarse            1.0             100.000000
   1       coarse            1.0             111.111111
   2       coarse            1.0             122.222222
   ..         ...            ...                    ...
   87        fine           10.0             177.777778
   88        fine           10.0             188.888889
   89        fine           10.0             200.000000
   
   [90 rows x 3 columns]
   
   Metadata:

was_recommended: 0/90

was_measured: 0/90

dont_recommend: 0/90

   Constraints
   Empty DataFrame
   Columns: []
   Index: []
               
   Computational Representation
               
   Granularity_coarse  Granularity_medium  ...  Pressure[bar]  Temperature[degree_C]
   0                    1                   0  ...            1.0

100.000000 1 1 0 … 1.0 111.111111 2 1 0 … 1.0 122.222222 .. … … … … … 87 0 0 … 10.0 177.777778 88 0 0 … 10.0 188.888889 89 0 0 … 10.0 200.000000

   [90 rows x 5 columns]
 
 Objective
          
  Mode: SINGLE
          
  Targets 
                Type   Name Mode  Lower_Bound  Upper_Bound Transformation  \
  0  NumericalTarget  Yield  MAX         -inf          inf           None   
  
     Weight  
  0   100.0  
          
  Combine Function: GEOM_MEAN
 
 TwoPhaseMetaRecommender(allow_repeated_recommendations=None,

allow_recommending_already_measured=None, initial_recommender=FPSRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True), recommender=SequentialGreedyRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True, surrogate_model=GaussianProcessSurrogate(model_params={}, _model=None), acquisition_function_cls=’qEI’, _acquisition_function=None, hybrid_sampler=’None’, sampling_percentage=1.0), switch_after=1)

# Verify that both objects are equal.
assert campaign == campaign_recreate
print("Passed basic assertion check!")
Passed basic assertion check!

Comparing recommendations in both objects

To further show how serialization affects working with campaigns, we will now create and compare some recommendations in both campaigns.

recommendation_orig = campaign.recommend(batch_size=2)
recommendation_recreate = campaign_recreate.recommend(batch_size=2)
print("Recommendation from original object:")
print(recommendation_orig)
Recommendation from original object:
   Granularity  Pressure[bar]  Temperature[degree_C]
0       coarse            1.0                  100.0
59      medium           10.0                  200.0
print("Recommendation from recreated object:")
print(recommendation_recreate)
Recommendation from recreated object:
   Granularity  Pressure[bar]  Temperature[degree_C]
0       coarse            1.0                  100.0
59      medium           10.0                  200.0