{
   "components": {
     "comp-getbesttrialop": {
       "executorLabel": "exec-getbesttrialop",
       "inputDefinitions": {
         "parameters": {
           "study_spec_metrics": {
             "parameterType": "LIST"
           },
           "trials": {
             "parameterType": "LIST"
           }
         }
       },
       "outputDefinitions": {
         "parameters": {
           "Output": {
             "parameterType": "STRING"
           }
         }
       }
     }
   },
   "deploymentSpec": {
     "executors": {
       "exec-getbesttrialop": {
         "container": {
           "args": [
             "--executor_input",
             "{{$}}",
             "--function_to_execute",
             "GetBestTrialOp"
           ],
           "command": [
             "sh",
             "-c",
             "\nif ! [ -x \"$(command -v pip)\" ]; then\n    python3 -m ensurepip || python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet     --no-warn-script-location 'google-cloud-aiplatform' 'kfp==2.0.0-beta.12' && \"$0\" \"$@\"\n",
             "sh",
             "-ec",
             "program_path=$(mktemp -d)\nprintf \"%s\" \"$0\" > \"$program_path/ephemeral_component.py\"\npython3 -m kfp.components.executor_main                         --component_module_path                         \"$program_path/ephemeral_component.py\"                         \"$@\"\n",
             "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import *\n\ndef GetBestTrialOp(trials: list, study_spec_metrics: list) -> str:\n  \"\"\"Retrieves the best trial from the trials.\n\n  Args:\n      trials (list):\n        Required. List representing the intermediate\n        JSON representation of the trials from the hyperparameter tuning job.\n      study_spec_metrics (list):\n        Required. List serialized from dictionary representing the metrics to optimize.\n        The dictionary key is the metric_id, which is reported by your training\n        job, and the dictionary value is the optimization goal of the metric\n        ('minimize' or 'maximize'). example:\n        metrics = hyperparameter_tuning_job.serialize_metrics(\n            {'loss': 'minimize', 'accuracy': 'maximize'})\n\n  Returns:\n      String representing the intermediate JSON representation of the best\n      trial from the list of trials.\n\n  Raises:\n      RuntimeError: If there are multiple metrics.\n  \"\"\"\n  from google.cloud.aiplatform.aiplatform_v1.types import study\n\n  if len(study_spec_metrics) > 1:\n    raise RuntimeError('Unable to determine best parameters for multi-objective'\n                       ' hyperparameter tuning.')\n  trials_list = [study.Trial.from_json(trial) for trial in trials]\n  best_trial = None\n  goal = study_spec_metrics[0]['goal']\n  best_fn = None\n  if goal == study.StudySpec.MetricSpec.GoalType.MAXIMIZE:\n    best_fn = max\n  elif goal == study.StudySpec.MetricSpec.GoalType.MINIMIZE:\n    best_fn = min\n  best_trial = best_fn(\n      trials_list, key=lambda trial: trial.final_measurement.metrics[0].value)\n\n  return study.Trial.to_json(best_trial)\n\n"
           ],
           "image": "python:3.7"
         }
       }
     }
   },
   "pipelineInfo": {
     "name": "get-best-trial-op-test"
   },
   "root": {
     "dag": {
       "tasks": {
         "getbesttrialop": {
           "cachingOptions": {
             "enableCache": true
           },
           "componentRef": {
             "name": "comp-getbesttrialop"
           },
           "inputs": {
             "parameters": {
               "study_spec_metrics": {
                 "runtimeValue": {
                   "constant": [
                     {
                       "goal": 1.0,
                       "metric_id": "accuracy"
                     }
                   ]
                 }
               },
               "trials": {
                 "runtimeValue": {
                   "constant": [
                     "{\n \"id\": \"1\",\n \"state\": 4,\n \"parameters\": [\n {\n \"parameterId\": \"learning_rate\",\n \"value\": 0.03162277660168379\n },\n {\n \"parameterId\": \"momentum\",\n \"value\": 0.5\n },\n {\n \"parameterId\": \"num_neurons\",\n \"value\": 128.0\n }\n ],\n \"finalMeasurement\": {\n \"stepCount\": \"10\",\n \"metrics\": [\n {\n \"metricId\": \"accuracy\",\n \"value\": 0.734375\n }\n ]\n },\n \"startTime\": \"2021-12-10T00:41:57.675086142Z\",\n \"endTime\": \"2021-12-10T00:52:35Z\",\n \"name\": \"\",\n \"measurements\": [],\n \"clientId\": \"\",\n \"infeasibleReason\": \"\",\n \"customJob\": \"\"\n}"
                   ]
                 }
               }
             }
           },
           "taskInfo": {
             "name": "getbesttrialop"
           }
         }
       }
     }
   }
 }