예제 #1
0
def mcmc_round2(args,X_star):

    sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
    import foo as foo_ulp
    reload(foo_ulp)

    if args.showTime: print "[Xsat] round2 with single processor"

    res_round1_in_ulp=foo_ulp.R(* X_star)
    if res_round1_in_ulp<=args.round2_threshold:
        if args.showResult:
            print "round 2 is dismissed. XSat uses the the X_star from the round1"
            print " ==> ulp distance ", res_round1_in_ulp 
            print
        return (X_star,res_round1_in_ulp) 
    
    R_star=res_round1_in_ulp
    for (scale,scale_inv) in scales():
        res = op.basinhopping(lambda x: foo_ulp.R(* scale(x)),scale_inv(X_star),niter=args.round2_niter ,minimizer_kwargs={'method':args.method},callback=_callback_global,stepsize=args.round2_stepsize)
        if res.fun<R_star:
            X_star=scale(tr_help(res.x))
            R_star=res.fun
        if args.showResult:
            print "result (round 2): where scale(0.1) =", scale(0.1)
            print res
            print
        if R_star<args.round2_threshold:break
            

    return X_star,R_star
예제 #2
0
def run_mcmc_single(args):

    sys.path.insert(0,os.path.join(os.getcwd(),"build/R_square"))
    import foo as foo_square

    sp=np.zeros(foo_square.dim)+args.startPoint
 
    obj=lambda X:foo_square.R(* X)
    res=op.basinhopping(obj,sp,niter=args.niter,stepsize=args.stepSize,minimizer_kwargs={'method':args.method},callback=_callback_global)        
    if args.showResult:
        print "result round 1 with single processor "
        print res
        print    
    
        
    if args.round2:
        if args.showTime: print "[Xsat] round2 with single processor"

        sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
        import foo as foo_ulp
        reload(foo_ulp) #necessary because name 'foo' now still points to foo_square
              
        X_star=[res.x+0] if res.x.ndim==0 else res.x
        obj_near=lambda N:foo_ulp.R(* nth_fp_vectorized(N, X_star))
        #print op.fmin_powell(obj_near,np.zeros(foo.dim))
        print "*"*50
        print obj_near(0)
        print "*"*50
        
        res_round2 = op.basinhopping(obj_near,np.zeros(foo_ulp.dim),niter=args.round2_niter,stepsize=100.0,minimizer_kwargs={'method':args.method},callback=_callback_global)
        if args.showResult:
            print "result (round 2):"
            print res_round2
            print

            
            res.fun=res_round2.fun
            res.x=nth_fp_vectorized(res_round2.x,X_star)

    return res
예제 #3
0
def mcmc(args,i):

    #sys.path.insert(0,os.path.join(os.getcwd(),"build/R_square"))
    sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
    import foo 
    reload(foo) #necessary because name 'foo' now still points to foo_square


    
    np.random.seed()
    t_start_round1=time.time()
    if args.method=='noop_min': _minimizer_kwargs=dict(method=noop_min)
    else: _minimizer_kwargs=dict(method=args.method)
    sp=np.zeros(foo.dim)+args.startPoint+i

    res= op.basinhopping(lambda X: R_quick(X,i,foo.R),sp,niter=args.niter,stepsize=args.stepSize, minimizer_kwargs=_minimizer_kwargs,callback=_callback_global)
    if args.showResult:
        print "result (round 1) with i = ",i,":"
        print res
        print    

    # do some change here. If the first round gives a good/bad enough result, no need for the second. 

    X_star=scale(res.x,i)
    R_star=res.fun
    
    if res.fun!=0 and res.fun<args.round2_threshold:
    #if args.round2:

        if args.showTime: print "[Xsat] round2_move"

        sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
        import foo 
        reload(foo)

        sp=np.array([res.x+0]) if res.x.ndim==0 else res.x
        obj_near=lambda N:foo.R(* nth_fp_vectorized(N, scale(sp,i)))
        #print op.fmin_powell(obj_near,np.zeros(foo.dim))


        res_round2 = op.basinhopping(obj_near,np.zeros(foo.dim),niter=args.round2_niter,stepsize=args.round2_stepsize,minimizer_kwargs=_minimizer_kwargs,callback=_callback_global)
        #        res_round2 = op.fmin_powell(obj_near,np.zeros(foo.dim))

        if args.showResult:
            print "result (round 2) with i = ",i
            print res_round2
            print
        R_star=res_round2.fun
        ##change this because I could have used the R_quick.
        X_star= nth_fp_vectorized(res_round2.x,scale(sp,i))
        
    return (X_star,R_star)
예제 #4
0
def mcmc_round1(args):
    sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
    #sys.path.insert(0,os.path.join(os.getcwd(),"build/R_square"))
    import foo as foo_square
    reload(foo_square)
    
    sp=np.zeros(foo_square.dim)+args.startPoint
 
    obj=lambda X:foo_square.R(* X)
    res=op.basinhopping(obj,sp,niter=args.niter,stepsize=args.stepSize,minimizer_kwargs={'method':args.method},callback=_callback_global)        
    if args.showResult:
        print "Result round 1 with single processor "
        print res
        print    
    return tr_help(res.x),res.fun
예제 #5
0
def mcmc_round3(args,X_star):
    sys.path.insert(0,os.path.join(os.getcwd(),"build/R_ulp"))
    import foo as foo_ulp
    reload(foo_ulp)
    if args.showTime: print "[Xsat] round3 with single processor"
    obj_near=lambda N:foo_ulp.R(* nth_fp_vectorized(N, X_star))
    res = op.basinhopping(obj_near,np.zeros(foo_ulp.dim),niter=args.round3_niter,minimizer_kwargs={'method':args.method},callback=_callback_global,stepsize=args.round3_stepsize)

    if args.showResult:
        print "result (round 3):"
        print res
        print


    R_star=res.fun
    X_star=tr_help(nth_fp_vectorized(res.x,X_star))


    return (X_star,R_star)
예제 #6
0
def mcmc(args, i):
    np.random.seed()
    t_start_round1 = time.time()
    if args.method == 'noop_min': _minimizer_kwargs = dict(method=noop_min)
    else: _minimizer_kwargs = dict(method=args.method)
    sp = np.zeros(foo.dim) + args.startPoint + i

    res = op.basinhopping(lambda X: R_quick(X, i, foo.R),
                          sp,
                          niter=args.niter,
                          stepsize=args.stepSize,
                          minimizer_kwargs=_minimizer_kwargs,
                          callback=_callback_global)
    if args.showResult:
        print "result (round 1) with i = ", i, ":", '\n', res, '\n'

    # do some change here. If the first round gives a good/bad enough result, no need for the second.
    X_star = scale(res.x, i)
    R_star = res.fun

    if res.fun != 0 and res.fun < args.round2_threshold:
        if args.showTime: print "[Xsat] round2_move"
        sp = np.array([res.x + 0]) if res.x.ndim == 0 else res.x
        obj_near = lambda N: foo.R(*nth_fp_vectorized(N, scale(sp, i)))

        res_round2 = op.basinhopping(obj_near,
                                     np.zeros(foo.dim),
                                     niter=args.round2_niter,
                                     stepsize=args.round2_stepsize,
                                     minimizer_kwargs=_minimizer_kwargs,
                                     callback=_callback_global)
        if args.showResult:
            print "result (round 2) with i = ", i, '\n', res_round2, '\n'

        R_star = res_round2.fun
        ##change this because I could have used the R_quick.
        X_star = nth_fp_vectorized(res_round2.x, scale(sp, i))
    return (X_star, R_star)
예제 #7
0
     if args.showTime: print "[Xsat] verify X_star with z3 front-end"        
     verified = verify_solution(expr_z3,X_star, symbolTable,printModel=args.printModel)
     if verified and R_star!=0:
         sys.stderr.write("WARNING!!!!!!!!!!!!!!!! Actually sat.\n")
     elif not verified and R_star==0:
         sys.stderr.write("WARNING!!!!!!!!!!!!!!!  Wrong model !\n")
     else:
         pass
     
 if args.verify2:
     
     if args.showTime: print "[Xsat] verify X_star with build/R_verify"                
     sys.path.insert(0,os.path.join(os.getcwd(),"build/R_verify"))
     import foo as foo_verify
     reload(foo_verify) #necessary because name 'foo' now still points to foo_square
     verify_res=foo_verify.R(*X_star)  if foo_verify.dim==1 else foo_verify.R(*(X_star))
         
     if verify_res==0 and R_star!=0:
         sys.stderr.write("WARNING from verify2 (using include/R_verify/xsat.h) !!!!!!!!!!!!!!!! Actually sat.\n")
     elif verify_res!=0 and R_star==0:
         sys.stderr.write("WARNING from verify2  (using include/R_verify/xsat.h) !!!!!!!!!!!!!!!  Wrong model ! \n")
     else:
         pass
     
 t_verify=time.time()
 
 if args.showSymbolTable:
     print symbolTable
 if args.showConstraint: print expr_z3
 if args.showVariableNumber: print "nVar = ", len(symbolTable)