Beispiel #1
0
def test_initial_guess3():
    x0 = [-1, 1]

    def f(args):
        [x, y] = args
        ans = 100 * (y - x**2)**2 + (1 - x)**2
        return ans

    demo = SteepestDescent(f, x0)
    root_est = demo.find_root()
    real_root = np.array([1., 1.])
    assert real_root.all() == root_est.all()
Beispiel #2
0
    def __init__(self):
        self.current = Board()
        self.best = Board()
        random.seed()
        self.k = 1
        self.k_max = None
        self.max_score = None
        self.N = 0
        self.local_search = SteepestDescent()

        self.chances = {
            Tile.CENTRAL: 0.0,
            Tile.BORDER: 0.0,
            Tile.CORNER: 0.0,
        }

        self.prob_map = {
            Tile.CENTRAL: 0.0,
            Tile.BORDER: 0.0,
            Tile.CORNER: 0.0,
        }
        self.total = 25.0
Beispiel #3
0
  def __init__(self):
    self.current = Board()
    self.best = Board()
    random.seed()
    self.k = 1
    self.k_max = None
    self.max_score = None
    self.N = 0
    self.local_search = SteepestDescent()


    self.chances = {
      Tile.CENTRAL : 0.0,
      Tile.BORDER : 0.0,
      Tile.CORNER : 0.0,
    }

    self.prob_map = {
      Tile.CENTRAL : 0.0,
      Tile.BORDER : 0.0,
      Tile.CORNER : 0.0,
    }
    self.total = 25.0
from modeller import *
from modeller.optimizers import actions
from modeller.scripts import complete_pdb

# Load our custom steepest descent optimizer
from steepest_descent import SteepestDescent

env = environ()
env.io.atom_files_directory = ['../atom_files']
env.libs.topology.read(file='$(LIB)/top_heav.lib')
env.libs.parameters.read(file='$(LIB)/par.lib')

# Read in the initial structure:
code = '1fdn'
mdl = complete_pdb(env, code)
atmsel = selection(mdl)

# Generate the restraints:
mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)

# Optimize with our custom optimizer:
opt = SteepestDescent(max_iterations=80)
opt.optimize(atmsel, actions=actions.trace(5))
Beispiel #5
0
class VNS:
    def __init__(self):
        self.current = Board()
        self.best = Board()
        random.seed()
        self.k = 1
        self.k_max = None
        self.max_score = None
        self.N = 0
        self.local_search = SteepestDescent()

        self.chances = {
            Tile.CENTRAL: 0.0,
            Tile.BORDER: 0.0,
            Tile.CORNER: 0.0,
        }

        self.prob_map = {
            Tile.CENTRAL: 0.0,
            Tile.BORDER: 0.0,
            Tile.CORNER: 0.0,
        }
        self.total = 25.0

    def load_game_file(self, filename):
        tiles = self.current.load_game_file(filename)
        self.current.arrange(tiles)
        self.N = self.current.N
        self.max_score = 2 * self.N * (self.N - 1)
        self.set_chances()
        self.best = deepcopy(self.current)
        self.k_max = self.N / 2

    def set_chances(self):
        curr_tot = 0.0
        self.chances[Tile.CENTRAL] = 0.0
        for i in range(1, self.k + 1, 1):
            j = self.N - 2 * i
            self.chances[Tile.CENTRAL] += 2 * j + 2 * (j - 2)

        if self.k == self.k_max and self.N % 2 == 1:
            self.chances[Tile.CENTRAL] += 1

        self.chances[Tile.BORDER] = 4.0 * (self.N - 2)
        self.chances[Tile.CORNER] = 4.0

        for k, v in self.chances.items():
            curr_tot += v
        for k, v in self.chances.items():
            self.chances[k] = self.chances[k] / curr_tot

    def compute(self, sec=30):
        print 'compute....'
        current = self.best
        timer = time.time() + sec
        while (self.k == self.k_max
               or time.time() < timer) and self.best.score < self.max_score:
            self.generate_neighboor(current)
            local = self.local_search.search(current)
            if local.score > current.score:
                current = deepcopy(local)
                self.k = 1
            else:
                if self.k < self.k_max:
                    self.k = self.k + 1

            self.set_chances()
            if current.score > self.best.score:
                self.best = deepcopy(current)
                print 'best: {}  '.format(self.best.score)

    def generate_neighboor(self, board):
        tile_list = self.generate_random_tile_type(board)
        tile1 = self.generate_random_tile(tile_list)
        tile2 = self.generate_random_tile(tile_list)
        board.swap(tile1, tile2)

    def generate_random_tile_type(self, board):
        rnd = random.randint(0, 2)

        t_chance = self.prob_map[rnd] / self.total
        while t_chance > self.chances.get(rnd):
            rnd = random.randint(0, 2)
            t_chance = self.prob_map[rnd] / self.total

        t = self.prob_map.get(rnd, None)
        if t is not None:
            self.prob_map[rnd] += 1
        self.total += 1

        return board.tiles[rnd]

    def generate_random_tile(self, tiles_list):
        rnd = random.choice(tiles_list)
        while not self.isinneghiborhood(self.k, rnd.row, rnd.col):
            rnd = random.choice(tiles_list)
        return rnd

    def isinneghiborhood(self, k, i, j):
        expr = ( (i < k or i >= self.current.N-1-k) and 0 <= j <= self.current.N-1 ) or \
               ( (j < k or j >= self.current.N-1-k) and 0 <= i <= self.current.N-1 )
        return expr

    def print_exit_file(self):
        with open('end.txt', 'w') as f:
            for l in self.best.board_matrix:
                for el in l:
                    u, r, d, l = el.tile.pieces
                    outstring = '{} {} {} {}\n'.format(u, r, d, l)
                    f.writelines(outstring)
Beispiel #6
0
class VNS:
  def __init__(self):
    self.current = Board()
    self.best = Board()
    random.seed()
    self.k = 1
    self.k_max = None
    self.max_score = None
    self.N = 0
    self.local_search = SteepestDescent()


    self.chances = {
      Tile.CENTRAL : 0.0,
      Tile.BORDER : 0.0,
      Tile.CORNER : 0.0,
    }

    self.prob_map = {
      Tile.CENTRAL : 0.0,
      Tile.BORDER : 0.0,
      Tile.CORNER : 0.0,
    }
    self.total = 25.0

  def load_game_file(self,filename):
    tiles = self.current.load_game_file(filename)
    self.current.arrange(tiles)
    self.N = self.current.N
    self.max_score = 2*self.N*(self.N-1)
    self.set_chances()
    self.best = deepcopy(self.current)
    self.k_max = self.N / 2


  def set_chances(self):
    curr_tot = 0.0
    self.chances[Tile.CENTRAL] = 0.0
    for i in range(1,self.k+1,1):
      j = self.N -2*i
      self.chances[Tile.CENTRAL] += 2*j + 2*(j-2)

    if self.k == self.k_max and self.N % 2 == 1:
      self.chances[Tile.CENTRAL] += 1

    self.chances[Tile.BORDER] = 4.0*(self.N-2)
    self.chances[Tile.CORNER] = 4.0

    for k,v in self.chances.items():
      curr_tot += v
    for k,v in self.chances.items():
      self.chances[k] = self.chances[k]/curr_tot



  def compute(self,sec=30):
    print 'compute....'
    current = self.best
    timer = time.time() + sec
    while (self.k == self.k_max or time.time() < timer) and self.best.score < self.max_score:
      self.generate_neighboor(current)
      local = self.local_search.search(current)
      if local.score > current.score:
        current = deepcopy(local)
        self.k = 1
      else:
        if self.k < self.k_max:
          self.k = self.k + 1

      self.set_chances()
      if current.score > self.best.score:
        self.best = deepcopy(current)
        print 'best: {}  '.format(self.best.score)


  def generate_neighboor(self,board):
    tile_list = self.generate_random_tile_type(board)
    tile1 = self.generate_random_tile(tile_list)
    tile2 = self.generate_random_tile(tile_list)
    board.swap(tile1,tile2)


  def generate_random_tile_type(self,board):
    rnd = random.randint(0, 2)


    t_chance = self.prob_map[rnd]/self.total
    while t_chance > self.chances.get(rnd):
      rnd = random.randint(0, 2)
      t_chance = self.prob_map[rnd]/self.total


    t = self.prob_map.get(rnd,None)
    if t is not None:
      self.prob_map[rnd] += 1
    self.total += 1

    return board.tiles[rnd]


  def generate_random_tile(self,tiles_list):
    rnd = random.choice(tiles_list)
    while not self.isinneghiborhood(self.k,rnd.row,rnd.col):
      rnd = random.choice(tiles_list)
    return rnd

  def isinneghiborhood(self,k,i,j):
    expr = ( (i < k or i >= self.current.N-1-k) and 0 <= j <= self.current.N-1 ) or \
           ( (j < k or j >= self.current.N-1-k) and 0 <= i <= self.current.N-1 )
    return expr


  def print_exit_file(self):
    with open('end.txt','w') as f:
      for l in self.best.board_matrix:
        for el in l:
          u,r,d,l = el.tile.pieces
          outstring = '{} {} {} {}\n'.format(u,r,d,l)
          f.writelines(outstring)
import AnnoDomini.AutoDiff as AD
import numpy as np
from matplotlib import pyplot as plt
from scipy import linalg as la
from steepest_descent import SteepestDescent
from matplotlib import pyplot as plt


def f(args):
    [x, y] = args
    ans = 100 * (y - x**2)**2 + (1 - x)**2
    return ans


x0 = [2, 1]
sd = SteepestDescent(f, x0)
root = sd.find_root()
traj = sd.xs

x = [traj[i][0] for i in range(len(traj))]
y = [traj[i][1] for i in range(len(traj))]
traj2 = np.concatenate(
    (np.array(x).reshape(-1, 1), np.array(y).reshape(-1, 1)), axis=1)
# print(root)
# print(traj2)


def plot_descent(ans):
    X, Y = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(-2, 8, 100))
    Z = f(np.array([X, Y]))
    fig = plt.subplots(1, 1, figsize=(10, 7))