Esempio n. 1
0
  void intersect(const svector<F,V>& y, Op& op) const {
    // can't do bounded search with std::map, otherwise we would do double binary search
    // these thresholds have not been well optimized
    if (m.size() < 0.1*y.size()) {
      for (const_iterator xit=m.begin(); xit != m.end(); ++xit) {
	const_iterator yit = y.find(xit->first);
	if (yit != y.end())
	  op(xit->first, xit->second, yit->second);
      }
    } else if (0.1*m.size() > y.size()) {
      for (const_iterator yit=y.begin(); yit != y.end(); ++yit) {
	const_iterator xit = m.find(yit->first);
	if (xit != m.end())
	  op(yit->first, xit->second, yit->second);
      }
    } else {
      const_iterator xit = m.begin();
      const_iterator yit = y.begin();
      while (xit != m.end() && yit != y.end()) {
	if (xit->first > yit->first) {
	  ++yit;
	} else if (xit->first < yit->first) {
	  ++xit;
	} else {
	  op(xit->first, xit->second, yit->second);
	  ++xit; ++yit;
	}
      }
    }
  }
Esempio n. 2
0
/**
   \brief return the complement of variables that are currently assigned.
*/
void theory_wmaxsat::get_assignment(svector<bool>& result) {
    result.reset();
    
    if (!m_found_optimal) {
        for (unsigned i = 0; i < m_vars.size(); ++i) {
            result.push_back(false);
        }
    }
    else {
        std::sort(m_cost_save.begin(), m_cost_save.end());
        for (unsigned i = 0,j = 0; i < m_vars.size(); ++i) {
            if (j < m_cost_save.size() && m_cost_save[j] == static_cast<theory_var>(i)) {
                result.push_back(false);
                ++j;
            }
            else {
                result.push_back(true);
            }
        }
    }
    TRACE("opt",
          tout << "cost save: ";
          for (unsigned i = 0; i < m_cost_save.size(); ++i) {
              tout << m_cost_save[i] << " ";
          }
          tout << "\nvars: ";
          for (unsigned i = 0; i < m_vars.size(); ++i) {
              tout << mk_pp(m_vars[i].get(), get_manager()) << " ";
          }
          tout << "\nassignment: ";
          for (unsigned i = 0; i < result.size(); ++i) {
              tout << result[i] << " ";
          }
          tout << "\n";);                  
Esempio n. 3
0
 void sort_core(svector<Numeral> & as, unsigned_vector & xs, numeral_buffer<Numeral, numeral_manager> & buffer) {
     std::sort(xs.begin(), xs.end());
     unsigned num = as.size();
     for (unsigned i = 0; i < num; i++) {
         m().swap(as[i], buffer[xs[i]]);
     }
 }
Esempio n. 4
0
 void sort(svector<Numeral> & as, unsigned_vector & xs, numeral_buffer<Numeral, numeral_manager> & buffer) {
     unsigned num = as.size();
     for (unsigned i = 0; i < num; i++) {
         m().set(buffer[xs[i]], as[i]);
     }
     sort_core(as, xs, buffer);
 }
 virtual model_converter * translate(ast_translation & translator) {
     pb_preproc_model_converter* mc = alloc(pb_preproc_model_converter, translator.to());
     for (unsigned i = 0; i < m_const.size(); ++i) {
         mc->set_value_p(translator(m_const[i].first), translator(m_const[i].second));
     }
     return mc;
 }
Esempio n. 6
0
 void extract(unsigned_vector& indices)
 {
     for (unsigned i = 0; i < m_indices.size(); ++i) {
         if (m_indices[i]) {
             indices.push_back(i);
         }
     }
 }
Esempio n. 7
0
    ~Task() {
	if (v) {delete v; v=nullptr;}
	if (blob) {delete blob; blob=nullptr;}
	if (msgs) {
	    for(int i=0;i<msgs->size();++i)
            delete msgs->operator[](i);
	    delete msgs;
	    msgs=nullptr;
	}	
    }
Esempio n. 8
0
 void sieve_relation_plugin::collect_inner_signature(const relation_signature & s, 
         const svector<bool> & inner_columns, relation_signature & inner_sig) {
     SASSERT(inner_columns.size()==s.size());
     inner_sig.reset();
     unsigned n = s.size();
     for(unsigned i=0; i<n; i++) {
         if(inner_columns[i]) {
             inner_sig.push_back(s[i]);
         }
     }
 }
Esempio n. 9
0
 void display_model(std::ostream & out) const {
     int sz = m_model.size();
     for (int i = 1; i < sz; i++) {
         if (m_model[i] == l_true) {
             out << i << " ";
         }
         else if (m_model[i] == l_false) {
             out << -i << " ";
         }
     }
     out << "\n";
 }
Esempio n. 10
0
static void add_random_ineq(opt::model_based_opt& mbo,
                            random_gen& r,
                            svector<int>  const& values,
                            unsigned max_vars,
                            unsigned max_coeff) {
    unsigned num_vars = values.size();
    uint_set used_vars;
    vector<var> vars;
    int value = 0;
    for (unsigned i = 0; i < max_vars; ++i) {
        unsigned x = r(num_vars);
        if (used_vars.contains(x)) {
            continue;
        }
        used_vars.insert(x);
        int coeff = r(max_coeff + 1);
        if (coeff == 0) {
            continue;
        }
        unsigned sign = r(2);
		coeff = sign == 0 ? coeff : -coeff;
        vars.push_back(var(x, rational(coeff)));
        value += coeff*values[x];
    }
    unsigned abs_value = value < 0 ? - value : value;
    // value + k <= 0
    // k <= - value
    // range for k is 2*|value|
    // k <= - value - range
    opt::ineq_type rel = opt::t_le;

    int coeff = 0;
    if (r(4) == 0) {
        rel = opt::t_eq;
        coeff = -value;
    }
    else {
        if (abs_value > 0) {
            coeff = -value - r(2*abs_value);
        }
        else {
            coeff = 0;
        }
        if (coeff != -value && r(3) == 0) {
            rel = opt::t_lt;
        }   
    }
    mbo.add_constraint(vars, rational(coeff), rel);
}
Esempio n. 11
0
 void operator()(expr * e)
 {
     if (is_app(e)) {
         func_decl * sym = to_app(e)->get_decl();
         unsigned idx;
         if (m_parent.try_get_index(sym, idx)) {
             SASSERT(idx > 0);
             --idx;
             if (m_indices.size() <= idx) {
                 m_indices.resize(idx + 1, false);
             }
             m_indices[idx] = true;
         }
     }
 }
Esempio n. 12
0
static void test_sorting4_r(unsigned i, svector<unsigned>& in) {
    if (i == in.size()) {
        svector<unsigned> out;
        unsigned_ext uext;
        sorting_network<unsigned_ext> sn(uext);
        sn(in, out);
        is_sorted(out);
        std::cout << "sorted\n";
    }
    else {
        in[i] = 0;
        test_sorting4_r(i+1, in);
        in[i] = 1;
        test_sorting4_r(i+1, in);
    }
}
Esempio n. 13
0
    virtual void execute(cmd_context & ctx) {
        if(m_arg_idx<2) {
            throw cmd_exception("at least 2 arguments expected");
        }
        ensure_domain(ctx);
        ast_manager& m = ctx.m();

        func_decl_ref pred(
            m.mk_func_decl(m_rel_name, m_domain->size(), m_domain->c_ptr(), m.mk_bool_sort()), m);
        ctx.insert(pred);
        datalog::context& dctx = m_dl_ctx->get_dl_context();
        dctx.register_predicate(pred, false);
        if(!m_kinds.empty()) {
            dctx.set_predicate_representation(pred, m_kinds.size(), m_kinds.c_ptr());
        }
        m_domain = 0;
    }
Esempio n. 14
0
    void sieve_relation_plugin::extract_inner_columns(const relation_signature & s, relation_plugin & inner, 
            svector<bool> & inner_columns) {
        SASSERT(inner_columns.size()==s.size());
        unsigned n = s.size();
        relation_signature inner_sig_singleton;
        for(unsigned i=0; i<n; i++) {
            inner_sig_singleton.reset();
            inner_sig_singleton.push_back(s[i]);
            inner_columns[i] = inner.can_handle_signature(inner_sig_singleton);
        }
#if Z3DEBUG 
        //we assume that if a relation plugin can handle two sets of columns separetely, 
        //it can also handle them in one relation
        relation_signature inner_sig;
        collect_inner_signature(s, inner_columns, inner_sig);
        SASSERT(inner.can_handle_signature(inner_sig));
#endif
    }
Esempio n. 15
0
 join_fn(sieve_relation_plugin & p, const relation_base & r1, const relation_base & r2, unsigned col_cnt, 
             const unsigned * cols1, const unsigned * cols2, relation_join_fn * inner_join_fun)
         : convenient_relation_join_fn(r1.get_signature(), r2.get_signature(), col_cnt, cols1, cols2),
         m_plugin(p),
         m_inner_join_fun(inner_join_fun) {
     bool r1_sieved = r1.get_plugin().is_sieve_relation();
     bool r2_sieved = r2.get_plugin().is_sieve_relation();
     const sieve_relation * sr1 = r1_sieved ? static_cast<const sieve_relation *>(&r1) : 0;
     const sieve_relation * sr2 = r2_sieved ? static_cast<const sieve_relation *>(&r2) : 0;
     if(r1_sieved) {
         m_result_inner_cols.append(sr1->m_inner_cols);
     }
     else {
         m_result_inner_cols.resize(r1.get_signature().size(), true);
     }
     if(r2_sieved) {
         m_result_inner_cols.append(sr2->m_inner_cols);
     }
     else {
         m_result_inner_cols.resize(m_result_inner_cols.size() + r2.get_signature().size(), true);
     }
 }
Esempio n. 16
0
BOOL	CreateNode(Fvector& vAt, vertex& N)
{
	// *** Query and cache polygons for ray-casting
	Fvector	PointUp;		PointUp.set(vAt);	PointUp.y	+= RCAST_Depth;		SnapXZ	(PointUp);
	Fvector	PointDown;		PointDown.set(vAt);	PointDown.y	-= RCAST_Depth;		SnapXZ	(PointDown);

	Fbox	BB;				BB.set	(PointUp,PointUp);		BB.grow(g_params.fPatchSize/2);	// box 1
	Fbox	B2;				B2.set	(PointDown,PointDown);	B2.grow(g_params.fPatchSize/2);	// box 2
	BB.merge(B2			);
	BoxQuery(BB,false	);
	u32	dwCount = XRC.r_count();
	if (dwCount==0)	{
//		Log("chasm1");
		return FALSE;			// chasm?
	}

	// *** Transfer triangles and compute sector
	R_ASSERT(dwCount<RCAST_MaxTris);
	static svector<tri,RCAST_MaxTris> tris;		tris.clear();
	for (u32 i=0; i<dwCount; i++)
	{
		tri&		D = tris.last();
		CDB::RESULT	&rp = XRC.r_begin()[i];
		CDB::TRI&	T = *(Level.get_tris()+rp.id);

		D.v[0].set	(rp.verts[0]);
		D.v[1].set	(rp.verts[1]);
		D.v[2].set	(rp.verts[2]);
		D.sector	= T.sector;
		D.N.mknormal(D.v[0],D.v[1],D.v[2]);
		if (D.N.y<=0)	continue;

		tris.inc	();
	}
	if (tris.size()==0)	{
//		Log("chasm2");
		return FALSE;			// chasm?
	}

	// *** Perform ray-casts and calculate sector
	WORD Sector = 0xfffe;	// mark as first time

	static svector<Fvector,RCAST_Total>	points;		points.clear();
	static svector<Fvector,RCAST_Total>	normals;	normals.clear();
	Fvector P,D; D.set(0,-1,0);

	float coeff = 0.5f*g_params.fPatchSize/float(RCAST_Count);

	for (int x=-RCAST_Count; x<=RCAST_Count; x++) 
	{
		P.x = vAt.x + coeff*float(x);
		for (int z=-RCAST_Count; z<=RCAST_Count; z++) {
			P.z = vAt.z + coeff*float(z);
			P.y = vAt.y + 10.f;

			float	tri_min_range	= flt_max;
			int		tri_selected	= -1;
			float	range,u,v;
			for (i=0; i<u32(tris.size()); i++) 
			{
				if (CDB::TestRayTri(P,D,tris[i].v,u,v,range,false)) 
				{
					if (range<tri_min_range) {
						tri_min_range	= range;
						tri_selected	= i;
					}
				}
			}
			if (tri_selected>=0) {
				P.y -= tri_min_range;
				points.push_back(P);
				normals.push_back(tris[tri_selected].N);
				WORD TS = WORD(tris[tri_selected].sector);
				if (Sector==0xfffe)	Sector = TS;
				else 				if (Sector!=TS) Sector=InvalidSector;
			}
		}
	}
	if (points.size()<3) {
//		Msg		("Failed to create node at [%f,%f,%f].",vAt.x,vAt.y,vAt.z);
		return	FALSE;
	}
	if (float(points.size())/float(RCAST_Total) < 0.7f) {
//		Msg		("Partial chasm at [%f,%f,%f].",vAt.x,vAt.y,vAt.z);
		return	FALSE;
	}

	// *** Calc normal
	Fvector vNorm;
	vNorm.set(0,0,0);
	for (u32 n=0; n<normals.size(); n++)
		vNorm.add(normals[n]);
	vNorm.div(float(normals.size()));
	vNorm.normalize();
	/*
	{
		// second algorithm (Magic)
		Fvector N,O;
		N.set(vNorm);
		O.set(points[0]);
		Mgc::OrthogonalPlaneFit(
			points.size(),(Mgc::Vector3*)points.begin(),
			*((Mgc::Vector3*)&O),
			*((Mgc::Vector3*)&N)
		);
		if (N.y<0) N.invert();
		N.normalize();
		vNorm.lerp(vNorm,N,.3f);
		vNorm.normalize();
	}
	*/

 
	// *** Align plane
	Fvector vOffs;
	vOffs.set(0,-1000,0);
	Fplane PL; 	PL.build(vOffs,vNorm);
	for (u32 p=0; p<points.size(); p++)
	{
		float dist = PL.classify(points[p]);
		if (dist>0) {
			vOffs = points[p];
			PL.build(vOffs,vNorm);
		}
	}

	// *** Create node and register it
	N.Sector		=Sector;						// sector
	N.Plane.build	(vOffs,vNorm);					// build plane
	D.set			(0,1,0);
	N.Plane.intersectRayPoint(PointDown,D,N.Pos);	// "project" position

	// *** Validate results
	vNorm.set(0,1,0);
	if (vNorm.dotproduct(N.Plane.n)<_cos(deg2rad(60.f)))  return FALSE;

	float y_old = vAt.y;
	float y_new = N.Pos.y;
	if (y_old>y_new) {
		// down
		if (y_old-y_new > g_params.fCanDOWN ) return FALSE;
	} else {
		// up
		if (y_new-y_old > g_params.fCanUP	) return FALSE;
	}
 
	// *** Validate plane
	{
		Fvector PLP; D.set(0,-1,0);
		int num_successed_rays = 0;
		for (int x=-RCAST_Count; x<=RCAST_Count; x++) 
		{
			P.x = N.Pos.x + coeff*float(x);
			for (int z=-RCAST_Count; z<=RCAST_Count; z++) {
				P.z = N.Pos.z + coeff*float(z);
				P.y = N.Pos.y;
				N.Plane.intersectRayPoint(P,D,PLP);	// "project" position
				P.y = PLP.y+RCAST_VALID*0.01f;
				
				float	tri_min_range	= flt_max;
				int		tri_selected	= -1;
				float	range,u,v;
				for (i=0; i<float(tris.size()); i++) 
				{
					if (CDB::TestRayTri(P,D,tris[i].v,u,v,range,false)) 
					{
						if (range<tri_min_range) {
							tri_min_range	= range;
							tri_selected	= i;
						}
					}
				}
				if (tri_selected>=0) {
					if (tri_min_range<RCAST_VALID) num_successed_rays++;
				}
			}
		}
		float perc = float(num_successed_rays)/float(RCAST_Total);
		if (perc < 0.5f) {
			//			Msg		("Floating node.");
			return	FALSE;
		}
	}

	// *** Mask check
	// ???

	return TRUE;
}
Esempio n. 17
0
 virtual void operator()(model_ref & mdl, unsigned goal_idx) {
     SASSERT(goal_idx == 0);
     for (unsigned i = 0; i < m_const.size(); ++i) {
         mdl->register_decl(m_const[i].first->get_decl(), m_const[i].second);
     }
 }
Esempio n. 18
0
 void div(svector<Numeral> & as, mpz const & g) {
     unsigned n = as.size();
     for (unsigned i = 0; i < n; i++)
         m().div(as[i], g, as[i]);
 }
Esempio n. 19
0
 unsigned size() const {
     return m_names.size();
 }
Esempio n. 20
0
static void add_random_ineq(
    expr_ref_vector& fmls, 
    opt::model_based_opt& mbo,
    random_gen& r,
    svector<int>  const& values,
    unsigned max_vars,
    unsigned max_coeff) 
{
    ast_manager& m = fmls.get_manager();
    arith_util a(m);

    unsigned num_vars = values.size();
    uint_set used_vars;
    vector<var_t> vars;
    int value = 0;
    for (unsigned i = 0; i < max_vars; ++i) {
        unsigned x = r(num_vars);
        if (used_vars.contains(x)) {
            continue;
        }
        used_vars.insert(x);
        int coeff = r(max_coeff + 1);
        if (coeff == 0) {
            continue;
        }
        unsigned sign = r(2);
        coeff = sign == 0 ? coeff : -coeff;
        vars.push_back(var_t(x, rational(coeff)));
        value += coeff*values[x];
    }
    unsigned abs_value = value < 0 ? - value : value;
    // value + k <= 0
    // k <= - value
    // range for k is 2*|value|
    // k <= - value - range
    opt::ineq_type rel = opt::t_le;

    int coeff = 0;
    if (r(4) == 0) {
        rel = opt::t_eq;
        coeff = -value;
    }
    else {
        if (abs_value > 0) {
            coeff = -value - r(2*abs_value);
        }
        else {
            coeff = 0;
        }
        if (coeff != -value && r(3) == 0) {
            rel = opt::t_lt;
        }   
    }
    expr_ref fml(m);
    app_ref t1(m);
    app_ref t2(a.mk_numeral(rational(0), a.mk_real()), m);
    mk_term(vars, rational(coeff), t1);
    switch (rel) {
    case opt::t_eq:
        fml = m.mk_eq(t1, t2);
        break;
    case opt::t_lt:
        fml = a.mk_lt(t1, t2);
        break;
    case opt::t_le:
        fml = a.mk_le(t1, t2);
        break;
    case opt::t_mod:
        NOT_IMPLEMENTED_YET();        
        break;        
    }
    fmls.push_back(fml);
    mbo.add_constraint(vars, rational(coeff), rel);
}
Esempio n. 21
0
static void is_sorted(svector<unsigned> const& v) {
    for (unsigned i = 0; i + 1 < v.size(); ++i) {
        SASSERT(v[i] <= v[i+1]);
    }
}
Esempio n. 22
0
 static Z3_ast and_vec(Z3_context ctx, svector<Z3_ast> &c){
     return (c.size() > 1) ? Z3_mk_and(ctx, c.size(), &c[0]) : c[0];
 }
Esempio n. 23
0
BOOL	ValidNode(vertex& N)
{
	// *** Query and cache polygons for ray-casting
	Fvector	PointUp;		PointUp.set(N.Pos);		PointUp.y	+= RCAST_Depth/2;
	Fvector	PointDown;		PointDown.set(N.Pos);	PointDown.y	-= RCAST_Depth/2;

	Fbox	BB;				BB.set	(PointUp,PointUp);		BB.grow(g_params.fPatchSize/2);	// box 1
	Fbox	B2;				B2.set	(PointDown,PointDown);	B2.grow(g_params.fPatchSize/2);	// box 2
	BB.merge(B2			);
	BoxQuery(BB,false	);
	u32	dwCount = XRC.r_count();
	if (dwCount==0)	{
		Log("chasm1");
		return FALSE;			// chasm?
	}

	// *** Transfer triangles and compute sector
	R_ASSERT(dwCount<RCAST_MaxTris);
	static svector<tri,RCAST_MaxTris> tris;		tris.clear();
	for (u32 i=0; i<dwCount; i++)
	{
		tri&		D = tris.last();
		CDB::RESULT&rp = XRC.r_begin()[i];
		*(Level.get_tris()+XRC.r_begin()[i].id);

		D.v[0].set	(rp.verts[0]);
		D.v[1].set	(rp.verts[1]);
		D.v[2].set	(rp.verts[2]);

		Fvector		N;
		N.mknormal	(D.v[0],D.v[1],D.v[2]);
		if (N.y<=0)	continue;

		tris.inc	();
	}
	if (tris.size()==0)	{
		Log("chasm2");
		return FALSE;			// chasm?
	}

	// *** Perform ray-casts and calculate sector
	Fvector P,D,PLP; D.set(0,-1,0);

	float coeff = 0.5f*g_params.fPatchSize/float(RCAST_Count);

	int num_successed_rays = 0;
	for (int x=-RCAST_Count; x<=RCAST_Count; x++) 
	{
		P.x = N.Pos.x + coeff*float(x);
		for (int z=-RCAST_Count; z<=RCAST_Count; z++) {
			P.z = N.Pos.z + coeff*float(z);
			P.y = N.Pos.y;
			N.Plane.intersectRayPoint(P,D,PLP);	// "project" position
			P.y = PLP.y+RCAST_DepthValid/2;

			float	tri_min_range	= flt_max;
			int		tri_selected	= -1;
			float	range = 0.f,u,v;
			for (i=0; i<u32(tris.size()); i++) 
			{
				if (CDB::TestRayTri(P,D,tris[i].v,u,v,range,false)) 
				{
					if (range<tri_min_range) {
						tri_min_range	= range;
						tri_selected	= i;
					}
				}
			}
			if (tri_selected>=0) {
				if (range<RCAST_DepthValid)	num_successed_rays++;

			}
		}
	}
	if (float(num_successed_rays)/float(RCAST_Total) < 0.5f) {
		Msg		("Floating node.");
		return	FALSE;
	}
	return TRUE;
}