import numpy as np
import scipy as sp
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt
import scipy.optimize as opt
from pandas.io.pytables import HDFStore
from datetime import datetime

def optimize(self, opts={}):

    x0 = self.portfolio.weights.tolist()
    iterations = opts.get("iterations", 5)
    # x0 = self.portfolio.weights #(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)   
    # sp.optimize.anneal(self.utilityFunction, x0)
    coolingIter=100

    myopts = {
            'schedule'     : 'boltzmann',   # Non-default value.
            'maxfev'       : None,  # Default, formerly `maxeval`.
            'maxiter'      : 500,   # Non-default value.
            'maxaccept'    : None,  # Default value.
            'ftol'         : 1e-6,  # Default, formerly `feps`.
            'T0'           : None,  # Default value.
            'Tf'           : 1e-12, # Default value.
            'boltzmann'    : 1.0,   # Default value.
            'learn_rate'   : 0.5,   # Default value.
            'quench'       : 1.0,   # Default value.
            'm'            : 1.0,   # Default value.
            'n'            : 1.0,   # Default value.
            'lower'        : -10,   # Non-default value.
            'upper'        : +10,   # Non-default value.
            'dwell'        : 250,   # Non-default value.
            'disp'         : True   # Default value.
            }

    # {
    # 'schedule'     : 'cauchy',   # Non-default value.
    # 'maxfev'       : 20000,  # formerly `maxeval`, Cease iterations if the number of function evaluations exceeds maxeval.
    # 'maxiter'      : coolingIter,   # Cease iterations if the number of cooling iterations exceeds maxiter.
    # 'maxaccept'    : None,  # Default value.
    # 'ftol'         : 1e-3,  # Default, formerly `feps`.
    # 'T0'           : 0.5,  # Default value.
    # 'Tf'           : 1e-3, # Default value.
    # 'boltzmann'    : 1.0,   # Default value.
    # 'learn_rate'   : 0.5,   # Default value.
    # 'quench'       : 1.0,   # Default value.
    # 'm'            : 1.0,   # Default value.
    # 'n'            : 1.0,   # Default value.
    # 'lower'        : self.portfolio.weights[0]*0.5,   # Non-default value.
    # 'upper'        : self.portfolio.weights[0]*1.5,   # Non-default value.
    # 'dwell'        : iterations,   # The number of times to execute the inner loop at each value of the temperature
    # 'disp'         : True   # Default value.
    # }

    # res = sp.optimize.anneal(self.utilityFunction, x0, disp=True)
    res = sp.optimize.minimize(self.utilityFunction, x0, method='Anneal', options=myopts)

    print res      
    print self 