|
491 | 491 | "point" |
492 | 492 | ] |
493 | 493 | }, |
494 | | - { |
495 | | - "cell_type": "code", |
496 | | - "execution_count": null, |
497 | | - "metadata": {}, |
498 | | - "outputs": [], |
499 | | - "source": [ |
500 | | - "from dask.distributed import as_completed\n", |
501 | | - "from random import uniform\n", |
502 | | - "\n", |
503 | | - "scale = 5 # Intial random perturbation scale\n", |
504 | | - "best_point = (0, 0) # Initial guess\n", |
505 | | - "best_score = float('inf') # Best score so far\n", |
506 | | - "startx = [uniform(-scale, scale) for _ in range(10)]\n", |
507 | | - "starty = [uniform(-scale, scale) for _ in range(10)]\n", |
508 | | - "\n", |
509 | | - "# set up plot\n", |
510 | | - "source = ColumnDataSource({'x': startx, 'y': starty, 'c': ['grey'] * 10})\n", |
511 | | - "p.circle(source=source, x='x', y='y', color='c')\n", |
512 | | - "t = show(p, notebook_handle=True)\n", |
513 | | - "\n", |
514 | | - "# initial 10 random points\n", |
515 | | - "futures = [c.submit(rosenbrock, (x, y)) for x, y in zip(startx, starty)]\n", |
516 | | - "iterator = as_completed(futures)\n", |
517 | | - "\n", |
518 | | - "for res in iterator:\n", |
519 | | - " # take a completed point, is it an improvement?\n", |
520 | | - " point, score = res.result()\n", |
521 | | - " if score < best_score:\n", |
522 | | - " best_score, best_point = score, point\n", |
523 | | - " print(score, point)\n", |
524 | | - "\n", |
525 | | - " x, y = best_point\n", |
526 | | - " newx, newy = (x + uniform(-scale, scale), y + uniform(-scale, scale))\n", |
527 | | - " \n", |
528 | | - " # update plot\n", |
529 | | - " source.stream({'x': [newx], 'y': [newy], 'c': ['grey']}, rollover=20)\n", |
530 | | - " push_notebook(t)\n", |
531 | | - " \n", |
532 | | - " # add new point, dynamically, to work on the cluster\n", |
533 | | - " new_point = c.submit(rosenbrock, (newx, newy))\n", |
534 | | - " iterator.add(new_point) # Start tracking new task as well\n", |
535 | | - "\n", |
536 | | - " # Narrow search and consider stopping\n", |
537 | | - " scale *= 0.99\n", |
538 | | - " if scale < 0.001:\n", |
539 | | - " break\n", |
540 | | - "point" |
541 | | - ] |
542 | | - }, |
543 | | - { |
544 | | - "cell_type": "code", |
545 | | - "execution_count": null, |
546 | | - "metadata": {}, |
547 | | - "outputs": [], |
548 | | - "source": [ |
549 | | - "from dask.distributed import as_completed\n", |
550 | | - "from random import uniform\n", |
551 | | - "\n", |
552 | | - "scale = 5 # Intial random perturbation scale\n", |
553 | | - "best_point = (0, 0) # Initial guess\n", |
554 | | - "best_score = float('inf') # Best score so far\n", |
555 | | - "startx = [uniform(-scale, scale) for _ in range(10)]\n", |
556 | | - "starty = [uniform(-scale, scale) for _ in range(10)]\n", |
557 | | - "\n", |
558 | | - "# set up plot\n", |
559 | | - "source = ColumnDataSource({'x': startx, 'y': starty, 'c': ['grey'] * 10})\n", |
560 | | - "p.circle(source=source, x='x', y='y', color='c')\n", |
561 | | - "t = show(p, notebook_handle=True)\n", |
562 | | - "\n", |
563 | | - "# initial 10 random points\n", |
564 | | - "futures = [c.submit(rosenbrock, (x, y)) for x, y in zip(startx, starty)]\n", |
565 | | - "iterator = as_completed(futures)\n", |
566 | | - "\n", |
567 | | - "for res in iterator:\n", |
568 | | - " # take a completed point, is it an improvement?\n", |
569 | | - " point, score = res.result()\n", |
570 | | - " if score < best_score:\n", |
571 | | - " best_score, best_point = score, point\n", |
572 | | - " print(score, point)\n", |
573 | | - "\n", |
574 | | - " x, y = best_point\n", |
575 | | - " newx, newy = (x + uniform(-scale, scale), y + uniform(-scale, scale))\n", |
576 | | - " \n", |
577 | | - " # update plot\n", |
578 | | - " source.stream({'x': [newx], 'y': [newy], 'c': ['grey']}, rollover=20)\n", |
579 | | - " push_notebook(t)\n", |
580 | | - " \n", |
581 | | - " # add new point, dynamically, to work on the cluster\n", |
582 | | - " new_point = c.submit(rosenbrock, (newx, newy))\n", |
583 | | - " iterator.add(new_point) # Start tracking new task as well\n", |
584 | | - "\n", |
585 | | - " # Narrow search and consider stopping\n", |
586 | | - " scale *= 0.99\n", |
587 | | - " if scale < 0.001:\n", |
588 | | - " break\n", |
589 | | - "point" |
590 | | - ] |
591 | | - }, |
592 | 494 | { |
593 | 495 | "cell_type": "markdown", |
594 | 496 | "metadata": {}, |
|
0 commit comments