original_docstring: Run the solvers for the given set of problems.
docstring: Run the solvers for the given set of problems.
docstring_tokens: Run the solvers for the given set of problems .
code: def run_solvers(
      solvers: Sequence[Solver],
      problems: ProblemSet,
      *,
      job_id: str,
      output_dir: Path,
      logger: Logger,
      keep_temp: bool = False,
  ) -> None:
      """Run the solvers for the given set of problems."""
      # Log for problems.
  
      problem_file = output_dir / f"{job_id}.problems.log"
  
      with problem_file.open(mode="w") as f:
          for p in problems:
              print(p, file=f)
  
      # Run solvers.
  
      def get_timing_information(results: Sequence[Result], n_warmups: int) -> str:
          """Return the timing information as a string."""
          times = [r.time for r in results][n_warmups:]
          if len(times) == 0:
              return ""
          if len(times) == 1:
              return f" ({times[0]:.3f} sec)"
          mean = statistics.mean(times)
          stdev = statistics.stdev(times)
          max_t, max_i = max((t, i) for i, t in enumerate(times))
          return (
              f" (average: {mean:.3f} +- {stdev:.3f} sec,"
              f" slowest: {max_t:.3f} sec on {max_i + 1 + n_warmups})"
          )
  
      results: List[Tuple[str, Sequence[Result], Path]] = []
  
      for s in solvers:
          s._problem_file = problem_file  # Yes, this is ugly.
          t1 = time.time()
          r = s.solve(problems)
          t2 = time.time()
          if r and len(r) == len(problems):
              results.append((s.name, r, s._output_dir))
              s.logger.info(
                  f"{t2 - t1:.3f} sec{get_timing_information(r, problems.n_warmups)}"
              )
          else:
              s.logger.error("failed")
  
      # Check the consistency.
  
      check_logger = logger.getChild("Check")
  
      wrong: Set[str] = set()
  
      if problems.problem_type == "gcd":
          if len(results) >= 2:
              for i in range(len(problems)):
                  p0 = results[0][1][i].answer[0]
                  for j in range(1, len(results)):
                      pj = results[j][1][i].answer[0]
                      if not p0.equals_without_unit(pj):
                          name0 = results[0][0]
                          namej = results[j][0]
                          check_logger.error(
                              f"{name0}:{namej}:{i + 1}: inconsistent answers"
                          )
                          wrong.add(name0)
                          wrong.add(namej)
      elif problems.problem_type == "factor":
          for name, res, _ in results:
              for i, ri in enumerate(res):
                  product = functools.reduce(operator.mul, ri.answer, Polynomial(1))
                  if problems[i].p != product:
                      check_logger.error(f"{name}:{i + 1}: wrong answer")
                      wrong.add(name)
  
      # Remove the solver's output directory only if succeeded.
  
      if not keep_temp:
          for name, _, path in results:
              if name not in wrong and path.exists():
                  shutil.rmtree(path)
  
      if results:
          # Write the timings into a CSV file.
  
          output_csv_file = output_dir / f"{job_id}.csv"
  
          plot.write_csv(
              output_csv_file, problems, {name: res for name, res, _ in results}
          )
  
          logger.info(f"output_csv_file = {output_csv_file}")
  
          # Generate plots.
  
          plot_output_dir = output_csv_file.with_suffix(".figures")
          suffix = ".pdf"
  
          plot.make_plots(output_csv_file, plot_output_dir, suffix)
  
          logger.info(f"figures are in {plot_output_dir}")
code_tokens: def run_solvers ( solvers : Sequence [ Solver ] , problems : ProblemSet , * , job_id : str , output_dir : Path , logger : Logger , keep_temp : bool = False , ) -> None : problem_file = output_dir / f"{job_id}.problems.log" with problem_file . open ( mode = "w" ) as f : for p in problems : print ( p , file = f ) def get_timing_information ( results : Sequence [ Result ] , n_warmups : int ) -> str : """Return the timing information as a string.""" times = [ r . time for r in results ] [ n_warmups : ] if len ( times ) == 0 : return "" if len ( times ) == 1 : return f" ({times[0]:.3f} sec)" mean = statistics . mean ( times ) stdev = statistics . stdev ( times ) max_t , max_i = max ( ( t , i ) for i , t in enumerate ( times ) ) return ( f" (average: {mean:.3f} +- {stdev:.3f} sec," f" slowest: {max_t:.3f} sec on {max_i + 1 + n_warmups})" ) results : List [ Tuple [ str , Sequence [ Result ] , Path ] ] = [ ] for s in solvers : s . _problem_file = problem_file t1 = time . time ( ) r = s . solve ( problems ) t2 = time . time ( ) if r and len ( r ) == len ( problems ) : results . append ( ( s . name , r , s . _output_dir ) ) s . logger . info ( f"{t2 - t1:.3f} sec{get_timing_information(r, problems.n_warmups)}" ) else : s . logger . error ( "failed" ) check_logger = logger . getChild ( "Check" ) wrong : Set [ str ] = set ( ) if problems . problem_type == "gcd" : if len ( results ) >= 2 : for i in range ( len ( problems ) ) : p0 = results [ 0 ] [ 1 ] [ i ] . answer [ 0 ] for j in range ( 1 , len ( results ) ) : pj = results [ j ] [ 1 ] [ i ] . answer [ 0 ] if not p0 . equals_without_unit ( pj ) : name0 = results [ 0 ] [ 0 ] namej = results [ j ] [ 0 ] check_logger . error ( f"{name0}:{namej}:{i + 1}: inconsistent answers" ) wrong . add ( name0 ) wrong . add ( namej ) elif problems . problem_type == "factor" : for name , res , _ in results : for i , ri in enumerate ( res ) : product = functools . reduce ( operator . mul , ri . answer , Polynomial ( 1 ) ) if problems [ i ] . p != product : check_logger . error ( f"{name}:{i + 1}: wrong answer" ) wrong . add ( name ) if not keep_temp : for name , _ , path in results : if name not in wrong and path . exists ( ) : shutil . rmtree ( path ) if results : output_csv_file = output_dir / f"{job_id}.csv" plot . write_csv ( output_csv_file , problems , { name : res for name , res , _ in results } ) logger . info ( f"output_csv_file = {output_csv_file}" ) plot_output_dir = output_csv_file . with_suffix ( ".figures" ) suffix = ".pdf" plot . make_plots ( output_csv_file , plot_output_dir , suffix ) logger . info ( f"figures are in {plot_output_dir}" )
short_docstring: Run the solvers for the given set of problems.
short_docstring_tokens: Run the solvers for the given set of problems .
comment: """Run the solvers for the given set of problems."""$SEP$# Log for problems.$SEP$# Run solvers.$SEP$"""Return the timing information as a string."""$SEP$# Yes, this is ugly.$SEP$# Check the consistency.$SEP$# Remove the solver's output directory only if succeeded.$SEP$# Write the timings into a CSV file.$SEP$# Generate plots.