Polynomial_Normbounds



In [25]:
# Set up infrastructure and basic problem parameters
import multiprocessing as mp
import numpy as np
import datetime, os
from ContNoRegret.NoRegretAlgos import ContNoRegretProblem
from ContNoRegret.Domains import nBox, UnionOfDisjointnBoxes, DifferenceOfnBoxes, unitbox, hollowbox
from ContNoRegret.LossFunctions import random_PolynomialLosses, random_AffineLosses, random_QuadraticLosses, PolynomialLossFunction
from ContNoRegret.NoRegretAlgos import ContNoRegretProblem
from ContNoRegret.utils import CNR_worker, plot_results, save_results, circular_tour
from ContNoRegret.animate import save_animations
from ContNoRegret.Potentials import (ExponentialPotential, IdentityPotential, pNormPotential, CompositePotential,
                                        ExpPPotential, pExpPotential, HuberPotential, LogtasticPotential, FractionalLinearPotential)

# from ContNoRegret.loss_params import *

import matplotlib.pyplot as plt

tmpfolder = '/Volumes/tmp/'


T = 500 # Time horizon
L = 5.0 # Uniform bound on the Lipschitz constant
M = 10
N = 2500 # Number of parallel algorithm instances
Ngrid = 250000 # Number of gridpoints for the sampling step

dom = unitbox(2)
nus = [0.05, 1]

# lossfuncs = []
while len(lossfuncs) < T:
    tmpfuncs = np.array(random_PolynomialLosses(dom, 10, M, L, 4, [0,1,2,3,4], high_ratio=True))
    normbounds = {nu: np.array([lossfunc.norm(2/nu, tmpfolder=tmpfolder) for lossfunc in tmpfuncs]) for nu in nus}
    Ms = {nu: np.array(normbounds[nu]) for nu in nus}
    for i in range(len(normbounds)):
        ratio = normbounds[nus[0]][i]/normbounds[nus[1]][i]
        if ratio > 4:
            lossfuncs.append(tmpfuncs[i])
        

# lossfuncs = [PolynomialLossFunction(dom, coeff, expo) for coeff,expo in zip(coeffs,exponents)]
# Minf, M2 = np.max(inf_norms), np.max(two_norms)

# # create Continuous No-Regret problem
# prob = ContNoRegretProblem(dom, lossfuncs, L, Minf, desc='normtest')
    
# # Select a number of potentials for the Dual Averaging algorithm
# potentials = [ExponentialPotential(), pNormPotential(1.05, M=Minf), pNormPotential(2, M=M2)]
  
# # the following runs fine if the script is the __main__ method, but crashes when running from ipython
# pool = mp.Pool(processes=mp.cpu_count()-1)
# processes = []

# DAkwargs = [{'opt_rate':True, 'Ngrid':Ngrid, 'potential':pot, 'pid':i, 
#              'tmpfolder':tmpfolder, 'label':pot.desc} for i,pot in enumerate(potentials)]
# processes += [pool.apply_async(CNR_worker, (prob, N, 'DA'), kwarg) for kwarg in DAkwargs]

# # wait for the processes to finish an collect the results
# results = [process.get() for process in processes]


/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/kernel/__main__.py:36: RuntimeWarning: invalid value encountered in double_scalars
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/kernel/__main__.py:36: RuntimeWarning: invalid value encountered in double_scalars
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/kernel/__main__.py:36: RuntimeWarning: invalid value encountered in double_scalars

In [26]:
coefficients = [list(lossfunc.coeffs) for lossfunc in lossfuncs]
exponents = [list(lossfunc.exponents) for lossfunc in lossfuncs]
len(coefficients)


Out[26]:
500

In [27]:
with open('normbound_params_n2.py', 'w') as f:
    f.write('coeffs = {}\n'.format(coefficients) +'exponents = {}'.format(exponents))

In [28]:
two_norms = [lossfunc.norm(2,tmpfolder=tmpfolder) for lossfunc in lossfuncs]
inf_norms = [lossfunc.max() for lossfunc in lossfuncs]

In [30]:
with open('normbound_params_n2_norms.py', 'w') as f:
    f.write('two_norms = {}\n'.format(two_norms) +'inf_norms = {}'.format(inf_norms))

In [7]:
from ContNoRegret.LossFunctions import AffineLossFunction
from ContNoRegret.Domains import unitbox
dom = unitbox(2)
l = AffineLossFunction(dom, [-0.2,0], 0.05)

In [14]:
L, T = 5, 20
lossfuncs = [AffineLossFunction(dom, [(-1)**t*t/T*L, 0], 0.5*t/T*L) for t in range(T)]

In [21]:
lossfuncs[15].a


Out[21]:
array([-3.75,  0.  ])

In [ ]: