docstr: Optimize a graph.
  
  Parameters
  ----------
  graph : EntityGraph
      Tileable or chunk graph.
  
  Returns
  -------
  optimization_records : OptimizationRecords
      Optimization records.
docstr_tok: ["Optimize","a","graph",".","Parameters","--","--","--","--","--","graph",":","EntityGraph","Tileable","or","chunk","graph",".","Returns","--","--","--","-","optimization_records",":","OptimizationRecords","Optimization","records","."]
code: def optimize(cls, graph: EntityGraph) -> OptimizationRecords:
      """
      Optimize a graph.
  
      Parameters
      ----------
      graph : EntityGraph
          Tileable or chunk graph.
  
      Returns
      -------
      optimization_records : OptimizationRecords
          Optimization records.
      """
      records = OptimizationRecords()
      cached_rule = functools.lru_cache(maxsize=None)(
          lambda _rule_type: _rule_type(graph, records, cls)
      )
  
      for rule_type in cls._rule_types:
          rule = cached_rule(rule_type)
          if rule.apply():
              cls._replace_inputs(graph, records)
              new_results = []
              for result in graph.results:
                  new_results.append(
                      records.get_optimization_result(
                          result, default=result
                      )
                  )
              graph.results = new_results
  
      return records
  
imports: import functools
  import weakref
  from abc import ABC, abstractmethod
  from collections import defaultdict
  from dataclasses import dataclass
  from enum import Enum
  from typing import Dict, List, Optional, Type, Set
  from ...core import OperandType, EntityType, enter_mode
  from ...core.graph import EntityGraph
  from ...utils import implements
label_desc: Use image node_id 3 for calling the Optimizer obj's underlying member method code with example usage: obj.optimize(cls, graph) and returns: records