def problem_2d():
    cnstr = ({'type':'ineq','fun':g1high},{'type':'ineq','fun':g2})
    #cnstr = ({'type':'ineq','fun':g1high},)
    print minimize(fhigh,[3.,3.],method='SLSQP',bounds=((0,10),(0,10),),constraints=cnstr)
    xL = np.array([-2,-2])
    xU = np.array([2,2])
    x0 = np.array([1,1])
    delta = 1.0
    nw = 3
    tol = 1e-4
    err = tol+1
    iterMax = 50
    nIter = 0

    prefix = 'AVCM'
    fscaled = AdditiveScaling(fhigh,flow,nw)
    gscaled = AdditiveScaling(g1high,g1low,nw)
    #xDOE = np.array([[10.0,3.3333],[0.0,6.6667],[3.3333,0.0],[6.6667,10.0]])
    #fscaled.initialize_by_points(xDOE)
    #gscaled.initialize_by_points(xDOE)

    trReg = TrustRegionManagement(delta)

    report = FileOutput('twodim_results_20140717.py')
    _x10 = list()
    _x20 = list()
    _x1new = list()
    _x2new = list()
    _fh = list()
    _fl = list()
    _fs = list()
    _gh = list()
    _gl = list()
    _gs = list()
    _delta = list()
    _rho = list()
    _err = list()

    while err>tol and nIter<iterMax:
        fscaled.construct_scaling_model(x0)
        gscaled.construct_scaling_model(x0)
        bnds = get_bounds(x0,delta,xL,xU)
        cnstr = ({'type':'ineq','fun':gscaled},{'type':'ineq','fun':g2})
        #cnstr = ({'type':'ineq','fun':gscaled},)
        rslt = minimize(fscaled,x0,method='SLSQP',bounds=bnds,constraints=cnstr)
        xnew = rslt.x
        fnew = rslt.fun
        rho1 = fscaled.get_trust_region_ratio(xnew)
        rho2 = gscaled.get_trust_region_ratio(xnew)
        err = np.linalg.norm([x0-xnew])
        delta1,x0 = trReg.adjust(rho1,x0,xnew)
        delta2,tmp = trReg.adjust(rho2,x0,xnew)
        delta = min(delta1,delta2)
        nIter += 1
        print '%.4f\t%.4f\t%.4f\t%.4f\t%.2e\t%.4f\t%.4f'%(xnew[0],xnew[1],x0[0],x0[1], err, fnew, rho1)

        _x10.append(x0[0])
        _x20.append(x0[1])
        _x1new.append(xnew[0])
        _x2new.append(xnew[1])
        _fh.append(fhigh(xnew))
        _fl.append(flow(xnew))
        _fs.append(fnew)
        _gh.append(g1high(xnew))
        _gl.append(g1low(xnew))
        _gs.append(gscaled(xnew))
        _delta.append(delta)
        _rho.append(rho1)
        _err.append(err)
    
    report.write_string('#===== %s ====='%prefix)
    report.write_string(prefix+'fhEval = %d'%fscaled.fHigh._nEval)
    report.write_string(prefix+'flEval = %d'%fscaled.fLow._nEval)
    report.write_string(prefix+'ghEval = %d'%gscaled.fHigh._nEval)
    report.write_string(prefix+'glEval = %d'%gscaled.fLow._nEval)
    report.write_string(prefix+'nIter = %d'%nIter)
    report.write_array(_x10,prefix+'x10')
    report.write_array(_x20,prefix+'x20')
    report.write_array(_x1new,prefix+'x1new')
    report.write_array(_x2new,prefix+'x2new')
    report.write_array(_fh,prefix+'fh')
    report.write_array(_fl,prefix+'fl')
    report.write_array(_fs,prefix+'fs')
    report.write_array(_gh,prefix+'gh')
    report.write_array(_gl,prefix+'gl')
    report.write_array(_gs,prefix+'gs')
    report.write_array(_delta,prefix+'delta')
    report.write_array(_rho,prefix+'rho')
    report.write_array(_err,prefix+'err')
def run_test_additive():
    xL, x0, xU = 0.0, 0.5, 1.0
    delta = 0.5
    xDOE = np.array([0.2,0.5,0.8])
    prefix = 'RVFMadd'
    fsc = HybridScaling(fhigh,flow,3,1e-6,1.0)
    report = FileOutput('RVFM_20151103.py')
    fsc.initialize_by_points(xDOE)
    
    frdo = RobustObjective(fhigh)
    
    trReg = TrustRegionManagement(delta)
    tol = 1e-6
    err = tol+1
    iterMax = 50
    nIter = 0
    x = np.linspace(0,1,100)
    yh = np.array([fhigh(_x) for _x in x])
    yl = np.array([flow(_x) for _x in x])

    _x0 = list()
    _fh = list()
    _fl = list()
    _fs = list()
    _rho = list()
    _err = list()
    _delta = list()
    print 'xnew\tx0\terr\tfnew\trho'
    while err>tol and nIter<iterMax:
        fsc.construct_scaling_model(x0)
        bnds = get_bounds(x0,delta,xL,xU)
        rslt = minimize(frdo,x0,method='SLSQP',bounds=bnds)
        xnew = rslt.x[0]
        fnew = rslt.fun
        rho = fsc.get_trust_region_ratio(xnew)
        err = abs(x0-xnew)
        delta,x0 = trReg.adjust(rho,x0,xnew)
        nIter += 1
        print '%.4f\t%.4f\t%.2e\t%.4f\t%.4f'%(xnew,x0, err, fnew, rho)
        
        _x0.append(xnew)
        _fh.append(fhigh(xnew))
        _fl.append(flow(xnew))
        _fs.append(fnew)
        _rho.append(rho)
        _err.append(err)
        _delta.append(delta)
    
    print nIter
    print fsc._scalingAdd.fHigh._nEval
    print fsc._scalingAdd.fLow._nEval

    report.write_string('#===== %s ====='%prefix)
    report.write_string(prefix+'fHighEval = %d'%fsc._scalingAdd.fHigh._nEval)
    report.write_string(prefix+'fLowEval = %d'%fsc._scalingAdd.fLow._nEval)
    report.write_string(prefix+'nIter = %d'%nIter)
    report.write_array(_x0,prefix+'x0')
    report.write_array(_fh,prefix+'fh')
    report.write_array(_fl,prefix+'fl')
    report.write_array(_fs,prefix+'fs')
    report.write_array(_rho,prefix+'rho')
    report.write_array(_err,prefix+'err')
    report.write_array(_delta,prefix+'delta')
    report.write_array(x, 'x')
    report.write_array(yh,'fhigh')
    report.write_array(yl,'flow')
    fSc = np.array([fsc(_x) for _x in x])
    report.write_array(fSc,prefix+'fscaled')
    report.write_array(fsc._scalingAdd.fHigh._histXpart,prefix+'xHist')
    report.write_array(fsc._scalingAdd.fHigh._histFpart,prefix+'fHist')

    fsc._scalingAdd.fHigh.write_history('history_20140715.txt')
    plt.figure(1)
    plt.hold(True)
    plt.plot(x,yh,'b')
    plt.plot(x,yl,'r')
    plt.plot(x,fSc,'k')
    plt.plot(fsc._scalingAdd.fHigh._histXpart,fsc._scalingAdd.fHigh._histFpart,'go')
    plt.plot(x0,fsc.fHigh(x0,False),'ro')
    plt.axis([0,1,-10,20])
    plt.show()
    plt.cla()