/* expr : expr1 {('+' | '-') expr1}; */ int expr(ParserData *pd) { double right, left; int currToken; if ( !expr1(pd) ) return 0; while ( pd->m_Token.Type == T_PLUS || pd->m_Token.Type == T_MINUS ) { currToken = pd->m_Token.Type; GetNextToken(pd->m_strExpr, &(pd->m_Token)); if ( !expr1(pd) ) return 0; right = pd->m_stack[pd->m_top--]; left = pd->m_stack[pd->m_top--]; if ( currToken == T_PLUS ) { pd->m_stack[++pd->m_top] = left + right; } else if ( currToken == T_MINUS ) { pd->m_stack[++pd->m_top] = left - right; } } return 1; }
static expr *expr0(int critical) { expr *e, *f; e = expr1(critical); if (!e) return NULL; while (i == '|') { i = scan(scpriv, tokval); f = expr1(critical); if (!f) return NULL; if (!(is_simple(e) || is_just_unknown(e)) || !(is_simple(f) || is_just_unknown(f))) { nasm_error(ERR_NONFATAL, "`|' operator may only be applied to" " scalar values"); } if (is_just_unknown(e) || is_just_unknown(f)) e = unknown_expr(); else e = scalarvect(reloc_value(e) | reloc_value(f)); } return e; }
static ksp_tree_t* expr(ksp_parser_t* parser) { ksp_lexer_t* lexer = parser->lexer; ksp_tree_t* left = expr1(parser); if (left == K_NULL) return left; ksp_tree_t* right = K_NULL; while (K_TRUE) { ksp_word_t* w = ksp_word_look(lexer); ksp_tag_t tag = w->tag; if (tag == TAG_GE || tag == TAG_GT || tag == TAG_EQ || tag == TAG_LE || tag == TAG_LT || tag == TAG_NE) { ksp_tree_t* t = tree_new3(parser, EXPR1, w); ksp_word_next(lexer); right = expr1(parser); tree_set_left(t, left); tree_set_right(t, right); left = t; } else { return left; } } }
Tree expr(int tok) { static char stop[] = { IF, ID, '}', 0 }; Tree p = expr1(0); while (t == ',') { Tree q; t = gettok(); q = pointer(expr1(0)); p = tree(RIGHT, q->type, root(value(p)), q); } if (tok) test(tok, stop); return p; }
void test_expressions() { RPN::Context cxt; cxt.insert("a", new RPN::ArgumentNode(1, 3)); cxt.insert("b", new RPN::ArgumentNode(2, 3)); cxt.insert("c", new RPN::ArgumentNode(3, 3)); RPN::Expression fixed("13 + sin(0)", cxt); insist_equal(fixed.evaluate(), 13.0); RPN::Expression functor("a + b * c", cxt, 3); insist_equal(functor.evaluate(1, 2, 3), 7.0); cxt.insert("e", new RPN::ExpressionNode(&fixed)); cxt.insert("f", new RPN::ExpressionNode(&functor)); const RPN::Node* node = cxt.lookup("e"); insist_equal(node->isValue(), 1); insist_zero(node->arguments()); node = cxt.lookup("f"); insist_equal(node->isFunction(), 1); insist_equal(node->arguments(), 3); RPN::Expression expr1("2 * e + 5", cxt); insist_equal(expr1.evaluate(), 31.0); RPN::Expression expr2("f(1, 2, 3)", cxt); insist_equal(expr2.evaluate(), 7.0); RPN::Expression expr3("2 * f(3, e, 2) + 5", cxt); insist_equal(expr3.evaluate(), 63.0); }
void test_expression_comparison::test_fraction_to_a_power() { std::string expr1("(A/B+C/D)^E"); std::string expr2("(C/D+A/B)^E"); bool result = are_expressions_equal(expr1, expr2, "are_expressions_equal.xhtml", false); CPPUNIT_ASSERT(result == true); }
Tree expr1(int tok) { static char stop[] = {IF, ID, 0}; Tree p = expr2(); if (t == '=' || (prec[t] >= 6 && prec[t] <= 8) || (prec[t] >= 11 && prec[t] <= 13)) { int op = t; t = gettok(); if (oper[op] == ASGN) p = asgntree(ASGN, p, value(expr1(0))); else { expect('='); p = incr(op, p, expr1(0)); } } if (tok) test(tok, stop); return p; }
int expr(big_int *a) { big_int *b = NULL; int result = 0; b = big_int_create(1); if (b == NULL) { printf("error when creating [b]\n"); result = 1; goto done; } if (expr1(a)) { result = 2; goto done; } while (1) { switch ((int) curr_lex.token) { case OR1 : case '|' : match(curr_lex.token); if (expr1(b)) { result = 3; goto done; } if (big_int_or(a, b, 0, a)) { printf("error in big_int_or()\n"); result = 4; goto done; } continue; default : goto done; } } done: big_int_destroy(b); return result; }
static bool expr3 (int * pn) { if (* cur == '-' && (cur == text_start || strchr ("+-*/(", cur [-1]))) { cur ++; if (! expr3 (pn)) return false; * pn = - *pn; } else if (* cur == '(') { cur ++; if (! expr1 (pn)) return false; if (* cur != ')') return false; cur ++; } else if (isdigit (* cur)) { int n = 0; do { int d = * cur - '0'; int nn = n * 10 + d; // checking overflow if ((nn - d) / 10 != n) return false; n = nn; } while (isdigit (* ++ cur)); * pn = n; } else { return false; } return true; }
void CCFCreater::CreateMutantsRun(std::vector<std::string> &expressions) { int varnumber=m_varlist.size(); int literalnum=m_literal_var_map.size(); //for each literal x for(int i=0;i<literalnum;i++) { //for each variable y for(int k=0;k<varnumber;k++) { std::string expr1(m_expression); std::string expr2(m_expression); //replace x by (x+y) and (x+!y) if(m_literal_var_map[i].first==0 || m_expression[m_literal_var_map[i].first-1]!='!') { expr1.insert(expr1.begin()+m_literal_var_map[i].first,'('); expr1.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+1, std::string("*)")); expr1.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+2, m_varlist[k]); expr2.insert(expr2.begin()+m_literal_var_map[i].first,'('); expr2.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+1, std::string("*!)")); expr2.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+3, m_varlist[k]); } //replace !x by (!x+y) and (!x+!y) else//if(m_varappear[i][j]-1>=0 && m_expression[m_varappear[i][j]-1]=='!') { expr1.insert(expr1.begin()+m_literal_var_map[i].first-1,'('); expr1.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+1, std::string("*)")); expr1.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+2, m_varlist[k]); expr2.insert(expr2.begin()+m_literal_var_map[i].first-1,'('); expr2.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+1, std::string("*!)")); expr2.insert(m_literal_var_map[i].first+m_varlength[m_literal_var_map[i].second]+3, m_varlist[k]); } expressions.push_back(expr1); expressions.push_back(expr2); } } }
Object* Parser::parseSubExpression3() { std::auto_ptr<Object> expr1(parseSubExpression2()); while(hasTokens() && peekToken().getType() == T_EXPONENT) { Token tok = getToken(); std::auto_ptr<Object> expr2(parseSubExpression2()); expr1.reset(new Exponent(expr1.release(), expr2.release())); expr1->setLineNumber(tok.getLineNumber()); expr1->setColumnNumber(tok.getColumnNumber()); } return expr1.release(); }
Object* Parser::parseSubExpression7() { std::auto_ptr<Object> expr1(parseSubExpression6()); while(hasTokens() && (peekToken().getType() == T_SLASHEQ || peekToken().getType() == T_EQUAL)) { Token tok = getToken(); std::auto_ptr<Object> expr2(parseSubExpression6()); if(tok.getType() == T_SLASHEQ) expr1.reset(new Unequal(expr1.release(), expr2.release())); else expr1.reset(new Equal(expr1.release(), expr2.release())); expr1->setLineNumber(tok.getLineNumber()); expr1->setColumnNumber(tok.getColumnNumber()); } return expr1.release(); }
Object* Parser::parseSubExpression5() { std::auto_ptr<Object> expr1(parseSubExpression4()); while(hasTokens() && (peekToken().getType() == T_PLUS || peekToken().getType() == T_MINUS)) { Token tok = getToken(); unsigned char operation = tok.getType(); std::auto_ptr<Object> expr2(parseSubExpression4()); if(operation == T_PLUS) expr1.reset(new Add(expr1.release(), expr2.release())); else expr1.reset(new Subtract(expr1.release(), expr2.release())); expr1->setLineNumber(tok.getLineNumber()); expr1->setColumnNumber(tok.getColumnNumber()); } return expr1.release(); }
Object* Parser::parseSubExpression6() { std::auto_ptr<Object> expr1(parseSubExpression5()); while(hasTokens() && (peekToken().getType() == T_LESS || peekToken().getType() == T_GREAT || peekToken().getType() == T_LESSEQ || peekToken().getType() == T_GREATEQ)) { Token tok = getToken(); unsigned char operation = tok.getType(); std::auto_ptr<Object> expr2(parseSubExpression5()); if(operation == T_LESS) expr1.reset(new Less(expr1.release(), expr2.release())); else if(operation == T_GREAT) expr1.reset(new Greater(expr1.release(), expr2.release())); else if(operation == T_LESSEQ) expr1.reset(new LessEq(expr1.release(), expr2.release())); else expr1.reset(new GreatEq(expr1.release(), expr2.release())); expr1->setLineNumber(tok.getLineNumber()); expr1->setColumnNumber(tok.getColumnNumber()); } return expr1.release(); }
Object* Parser::parseSubExpression4() { std::auto_ptr<Object> expr1(parseSubExpression3()); while(hasTokens() && (peekToken().getType() == T_MULTIPLY || peekToken().getType() == T_DIVIDE || peekToken().getType() == T_INTDIVIDE || peekToken().getType() == T_REMAINDER)) { Token tok = getToken(); unsigned char operation = tok.getType(); std::auto_ptr<Object> expr2(parseSubExpression3()); if(operation == T_MULTIPLY) expr1.reset(new Multiply(expr1.release(), expr2.release())); else if(operation == T_DIVIDE) expr1.reset(new Divide(expr1.release(), expr2.release())); else if(operation == T_INTDIVIDE) expr1.reset(new IntDivide(expr1.release(), expr2.release())); else expr1.reset(new Remainder(expr1.release(), expr2.release())); expr1->setLineNumber(tok.getLineNumber()); expr1->setColumnNumber(tok.getColumnNumber()); } return expr1.release(); }
static bool expr1 (int * pn) { int op, n1, n2, n; if (! expr2 (& n1)) return false; op = * cur; if (op != '+' && op != '-') { * pn = n1; return true; } cur ++; if (! expr1 (& n2)) return false; if (op == '-') n2 = - n2; n = n1 + n2; // checking overflow if ( n1 > 0 && n2 > 0 && n < 0 || n1 < 0 && n2 < 0 && n > 0) { return false; } * pn = n; return true; }
/**************************************************程序入口****************************************************/ int main() { clock_t start,finish; double totaltime; start=clock(); time_t nowTime=time(0); struct tm* nowTimeStruct=localtime(&nowTime);//打印系统时间 // output<<"系统当前时间:"<<1900+nowTimeStruct->tm_year<<"."<<nowTimeStruct->tm_mon+1<<"."<< // nowTimeStruct->tm_mday<<" "<<nowTimeStruct->tm_hour<<":"<<nowTimeStruct->tm_min<<":"<<nowTimeStruct->tm_sec<<endl; try { // output<<">>>>>>>>>>>>>>>>>>>>>>>>>数据区<<<<<<<<<<<<<<<<<<<<"<<endl; define_data(env);//首先初始化全局变量 // output<<">>>>>>>>>>>>>>>>>>>>>>>>>/数据区<<<<<<<<<<<<<<<<<<<"<<endl<<endl; IloInvert(env,B0,B0l,Node-1);//求逆矩阵 //在此创建两种形式的目标函数 IloNumExpr Cost(env);//目标函数 for(IloInt w=0;w<NW;++w) { Cost+=detaPw[w]; } IloObjective min(env,Cost,IloObjective::Sense::Minimize,"min"); IloObjective max(env,Cost,IloObjective::Sense::Maximize,"max"); Master_Model.add(min); // Master_Model.add(IloMaximize(env,Cost));//目标函数二选一 Cost.end(); IloNumExpr expr1(env),expr2(env);//功率平衡约束 for(IloInt i=0;i<NG;++i) { expr1+=detaP[i]; } for(IloInt w=0;w<NW;++w) { expr2+=detaPw[w]; } Master_Model.add(expr1+expr2==0); expr1.end(); expr2.end(); for(IloInt i=0;i<NG;++i)//机组可调节范围 { Master_Model.add(detaP[i]>=Unit[i][1]*u[i]-P1[i]); Master_Model.add(detaP[i]<=Unit[i][2]*u[i]-P1[i]); Master_Model.add(detaP[i]>=-detaa[i]); Master_Model.add(detaP[i]<=detaa[i]); } IloNumExprArray detaP_node(env,Node-1),detaPw_node(env,Node-1);//安全约束,实际上安全约束影响不大 IloNumExprArray Theta(env,Node); for(IloInt b=0;b<Node-1;++b) { detaP_node[b]=IloNumExpr(env); detaPw_node[b]=IloNumExpr(env); IloInt i=0; for(;i<NG;++i) { if(Unit[i][0]==b-1)break; } if(i<NG) { detaP_node[b]+=detaP[i]; } if(Sw[b]>=0) { detaPw_node[b]+=detaPw[ Sw[b] ]; } } for(IloInt b=0;b<Node-1;++b) { Theta[b]=IloNumExpr(env); for(IloInt k=0;k<Node-1;++k) { Theta[b]+=B0l[b][k]*(detaP_node[k]+detaPw_node[k]); } } Theta[Node-1]=IloNumExpr(env); for(IloInt h=0;h<Branch;++h) { IloNumExpr exprTheta(env);//莫明其妙的错误 exprTheta+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1]); Master_Model.add(exprTheta<=Info_Branch[h][3]*(Info_Branch[h][4]-PL[h])); Master_Model.add(exprTheta>=Info_Branch[h][3]*(-Info_Branch[h][4]-PL[h])); exprTheta.end(); //两个相减的节点顺序没有影响么? } Theta.end(); detaP_node.end(); detaPw_node.end(); Master_Cplex.extract(Master_Model); Master_Cplex.solve(); if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果 { output<<"Master Problem Have No Solution"<<endl; goto lable2; } /************************************************************输出显示过程**************************************************/ // output/*<<endl<<"Min:"*/<<Master_Cplex.getObjValue()<<endl; Master_Model.remove(min); Master_Model.add(max); Master_Cplex.extract(Master_Model); Master_Cplex.solve(); if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果 { output<<"Master Problem Have No Solution"<<endl; goto lable2; } output/*<<endl<<"Max:"*/<<Master_Cplex.getObjValue()<<endl<<endl;; // output<<endl<<"常规机组出力调整量:"<<endl; // for(IloInt i=0;i<NG;++i) // { // output<<Master_Cplex.getValue(detaP[i])<<" "; // } // output<<endl<<"风电机组出力调整量:"<<endl; // for(IloInt i=0;i<NW;++i) // { // output<<Master_Cplex.getValue(detaPw[i])<<" "; // } // output<<endl; lable2: Master_Model.end(); Master_Cplex.end(); env.end(); } catch(IloException& ex)//异常捕获 { output<<"Error: "<<ex<<endl; } catch(...) { output<<"Error: Unknown exception caught!" << endl; } finish=clock(); totaltime=(double)(finish-start)/CLOCKS_PER_SEC; output<<"totaltime: "<<totaltime<<"s"<<endl<<endl; output.close(); return 0; }
Tree call(Tree f, Type fty, Coordinate src) { int n = 0; Tree args = NULL, r = NULL, e; Type *proto, rty = unqual(freturn(fty)); Symbol t3 = NULL; if (fty->u.f.oldstyle) proto = NULL; else proto = fty->u.f.proto; if (hascall(f)) r = f; if (isstruct(rty)) { t3 = temporary(AUTO, unqual(rty)); if (rty->size == 0) error("illegal use of incomplete type `%t'\n", rty); } if (t != ')') for (;;) { Tree q = pointer(expr1(0)); if (proto && *proto && *proto != voidtype) { Type aty; q = value(q); aty = assign(*proto, q); if (aty) q = cast(q, aty); else error("type error in argument %d to %s; found `%t' expected `%t'\n", n + 1, funcname(f), q->type, *proto); if ((isint(q->type) || isenum(q->type)) && q->type->size != inttype->size) q = cast(q, promote(q->type)); ++proto; } else { if (!fty->u.f.oldstyle && *proto == NULL) error("too many arguments to %s\n", funcname(f)); q = value(q); if (isarray(q->type) || q->type->size == 0) error("type error in argument %d to %s; `%t' is illegal\n", n + 1, funcname(f), q->type); else q = cast(q, promote(q->type)); } if (!IR->wants_argb && isstruct(q->type)) if (iscallb(q)) q = addrof(q); else { Symbol t1 = temporary(AUTO, unqual(q->type)); q = asgn(t1, q); q = tree(RIGHT, ptr(t1->type), root(q), lvalue(idtree(t1))); } if (q->type->size == 0) q->type = inttype; if (hascall(q)) r = r ? tree(RIGHT, voidtype, r, q) : q; args = tree(mkop(ARG, q->type), q->type, q, args); n++; if (Aflag >= 2 && n == 32) warning("more than 31 arguments in a call to %s\n", funcname(f)); if (t != ',') break; t = gettok(); } expect(')'); if (proto && *proto && *proto != voidtype) error("insufficient number of arguments to %s\n", funcname(f)); if (r) args = tree(RIGHT, voidtype, r, args); e = calltree(f, rty, args, t3); if (events.calls) apply(events.calls, &src, &e); return e; }
} /* sub[id] - return 1 if min <= x-y <= max, 0 otherwise */ static int subi(long x, long y, long min, long max, int needconst) { return addi(x, -y, min, max, needconst); } static int subd(double x, double y, double min, double max, int needconst) { return addd(x, -y, min, max, needconst); } Tree constexpr(int tok) { Tree p; needconst++; p = expr1(tok); needconst--; return p; } int intexpr(int tok, int n) { Tree p = constexpr(tok); needconst++; if (p->op == CNST+I || p->op == CNST+U) n = cast(p, inttype)->u.v.i; else error("integer expression must be constant\n"); needconst--; return n; }
/**************************************************程序入口****************************************************/ int main() { clock_t start,finish; double totaltime; start=clock(); try { define_data(env);//首先初始化全局变量 IloInvert(env,B0,B0l,Node-1);//求逆矩阵 /*************************************************************主问题目标函数*******************************************************/ IloNumExpr Cost(env); for(IloInt w=0;w<NW;++w) { Cost+=detaPw[w]; } Master_Model.add(IloMinimize(env,Cost)); //Master_Model.add(IloMaximize(env,Cost));//目标函数二先一 Cost.end(); /********************************************************机组出力上下限约束**************************************************/ IloNumExpr expr1(env),expr2(env); for(IloInt i=0;i<NG;++i) { expr1+=detaP[i]; } for(IloInt w=0;w<NW;++w) { expr2+=detaPw[w]; } Master_Model.add(expr1==expr2); expr1.end(); expr2.end(); for(IloInt i=0;i<NG;++i) { Master_Model.add(detaP[i]>=Unit[i][1]*u[i]-P1[i]); Master_Model.add(detaP[i]<=Unit[i][2]*u[i]-P1[i]); Master_Model.add(detaP[i]>=-detaa[i]); Master_Model.add(detaP[i]<=detaa[i]); } IloNumExprArray detaP_node(env,Node-1),detaPw_node(env,Node-1); IloNumExprArray Theta(env,Node); for(IloInt b=0;b<Node-1;++b) { detaP_node[b]=IloNumExpr(env); detaPw_node[b]=IloNumExpr(env); IloInt i=0; for(;i<NG;++i) { if(Unit[i][0]==b-1)break; } if(i<NG) { detaP_node[b]+=detaP[i]; } else { detaP_node[b]+=0; } if(Sw[b]>=0) { detaPw_node[b]+=detaPw[ Sw[b] ]; } else { detaPw_node[b]+=0; } } for(IloInt b=0;b<Node-1;++b) { Theta[b]=IloNumExpr(env); for(IloInt k=0;k<Node-1;++k) { Theta[b]+=B0l[b][k]*(detaP_node[k]+detaPw_node[k]); } } Theta[Node-1]=IloNumExpr(env); for(IloInt h=0;h<Branch;++h) { IloNumExpr exprTheta(env);//莫明其妙的错误 exprTheta+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1]); Master_Model.add(exprTheta<=Info_Branch[h][3]*(Info_Branch[h][4]-PL[h])); Master_Model.add(exprTheta>=Info_Branch[h][3]*(-Info_Branch[h][4]-PL[h])); exprTheta.end(); //两个相减的节点顺序没有影响么? } Theta.end(); detaP_node.end(); detaPw_node.end(); Master_Cplex.extract(Master_Model); Master_Cplex.solve(); if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果 { output<<"Master Problem Have No Solution"<<endl; goto lable2; } /************************************************************输出显示过程**************************************************/ output<<endl<<"Min/Max:"<<Master_Cplex.getObjValue()<<endl; lable2: Master_Model.end(); Master_Cplex.end(); env.end(); } catch(IloException& ex)//异常捕获 { output<<"Error: "<<ex<<endl; } catch(...) { output<<"Error: Unknown exception caught!" << endl; } finish=clock(); totaltime=(double)(finish-start)/CLOCKS_PER_SEC; output<<"totaltime: "<<totaltime<<"s"<<endl<<endl; output.close(); return 0; }
typename Evaluator<iDim, Iterator, Pset, ExprT>::eval_element_type Evaluator<iDim, Iterator, Pset, ExprT>::operator()( mpl::size_t<MESH_FACES> ) const { boost::timer __timer; VLOG(2) << "evaluator(MESH_FACES) " << "\n"; // // a few typedefs // // mesh element typedef typename mesh_element_type::entity_type geoelement_type; //typedef typename geoelement_type::face_type face_type; typedef mesh_element_type face_type; // geometric mapping context typedef typename geoelement_type::gm_type gm_type; typedef boost::shared_ptr<gm_type> gm_ptrtype; typedef typename geoelement_type::gm1_type gm1_type; typedef boost::shared_ptr<gm1_type> gm1_ptrtype; typedef typename gm_type::template Context<context, geoelement_type> gmc_type; typedef boost::shared_ptr<gmc_type> gmc_ptrtype; typedef fusion::map<fusion::pair<vf::detail::gmc<0>, gmc_ptrtype> > map_gmc_type; typedef typename gm1_type::template Context<context, geoelement_type> gmc1_type; typedef boost::shared_ptr<gmc1_type> gmc1_ptrtype; typedef fusion::map<fusion::pair<vf::detail::gmc<0>, gmc1_ptrtype> > map_gmc1_type; // expression //typedef typename expression_type::template tensor<map_gmc_type,fecontext_type> t_expr_type; //typedef decltype( basis_type::isomorphism( M_expr ) ) the_expression_type; typedef expression_type the_expression_type; typedef typename boost::remove_reference<typename boost::remove_const<the_expression_type>::type >::type iso_expression_type; typedef typename iso_expression_type::template tensor<map_gmc_type> t_expr_type; typedef typename iso_expression_type::template tensor<map_gmc1_type> t_expr1_type; typedef typename t_expr_type::shape shape; // // start // iterator_type __face_it, __face_en; boost::tie( boost::tuples::ignore, __face_it, __face_en ) = M_range; int npoints = M_pset.fpoints(0,1).size2(); element_type __v( M_pset.fpoints(0,1).size2()*std::distance( __face_it, __face_en )*shape::M ); node_type __p( mesh_element_type::nRealDim, M_pset.fpoints(0,1).size2()*std::distance( __face_it, __face_en ) ); __v.setZero(); __p.setZero(); VLOG(2) << "pset: " << M_pset.fpoints(0,1); VLOG(2) << "Checking trivial result..."; if ( __face_it == __face_en ) return boost::make_tuple( __v, __p ); gm_ptrtype __gm( new gm_type ); gm1_ptrtype __gm1( new gm1_type ); // // Precompute some data in the reference element for // geometric mapping and reference finite element // typedef typename geoelement_type::permutation_type permutation_type; typedef typename gm_type::precompute_ptrtype geopc_ptrtype; typedef typename gm_type::precompute_type geopc_type; typedef typename gm1_type::precompute_ptrtype geopc1_ptrtype; typedef typename gm1_type::precompute_type geopc1_type; std::vector<std::map<permutation_type, geopc_ptrtype> > __geopc( M_pset.nFaces() ); std::vector<std::map<permutation_type, geopc1_ptrtype> > __geopc1( M_pset.nFaces() ); VLOG(2) << "computing geopc..."; for ( uint16_type __f = 0; __f < M_pset.nFaces(); ++__f ) { for ( permutation_type __p( permutation_type::IDENTITY ); __p < permutation_type( permutation_type::N_PERMUTATIONS ); ++__p ) { __geopc[__f][__p] = geopc_ptrtype( new geopc_type( __gm, M_pset.fpoints(__f, __p.value() ) ) ); __geopc1[__f][__p] = geopc1_ptrtype( new geopc1_type( __gm1, M_pset.fpoints(__f, __p.value() ) ) ); DVLOG(2) << "pset " << __f << " : " << M_pset.fpoints(__f, __p.value() ); CHECK( __geopc[__f][__p]->nPoints() ) << "invalid number of points for geopc"; CHECK( __geopc1[__f][__p]->nPoints() ) << "invalid number of points for geopc1"; } } uint16_type __face_id = __face_it->pos_first(); gmc_ptrtype __c( new gmc_type( __gm, __face_it->element( 0 ), __geopc, __face_id ) ); gmc1_ptrtype __c1( new gmc1_type( __gm1, __face_it->element( 0 ), __geopc1, __face_id ) ); map_gmc_type mapgmc( fusion::make_pair<vf::detail::gmc<0> >( __c ) ); t_expr_type expr( M_expr, mapgmc ); map_gmc1_type mapgmc1( fusion::make_pair<vf::detail::gmc<0> >( __c1 ) ); t_expr1_type expr1( M_expr, mapgmc1 ); size_type nbFaceDof = invalid_size_type_value; for ( int e = 0; __face_it != __face_en; ++__face_it, ++e ) { FEELPP_ASSERT( __face_it->isOnBoundary() && !__face_it->isConnectedTo1() ) ( __face_it->marker() ) ( __face_it->isOnBoundary() ) ( __face_it->ad_first() ) ( __face_it->pos_first() ) ( __face_it->ad_second() ) ( __face_it->pos_second() ) ( __face_it->id() ).warn( "inconsistent data face" ); DVLOG(2) << "[evaluator] FACE_ID = " << __face_it->id() << " element id= " << __face_it->ad_first() << " pos in elt= " << __face_it->pos_first() << " marker: " << __face_it->marker() << "\n"; DVLOG(2) << "[evaluator] FACE_ID = " << __face_it->id() << " real pts=" << __face_it->G() << "\n"; uint16_type __face_id = __face_it->pos_first(); switch ( M_geomap_strategy ) { default: case GeomapStrategyType::GEOMAP_OPT: case GeomapStrategyType::GEOMAP_HO: { __c->update( __face_it->element( 0 ), __face_id ); DVLOG(2) << "[evaluator::GEOMAP_HO|GEOMAP_OPT] FACE_ID = " << __face_it->id() << " ref pts=" << __c->xRefs() << "\n"; DVLOG(2) << "[evaluator::GEOMAP_HO|GEOMAP_OPT] FACE_ID = " << __face_it->id() << " real pts=" << __c->xReal() << "\n"; map_gmc_type mapgmc( fusion::make_pair<vf::detail::gmc<0> >( __c ) ); expr.update( mapgmc ); for ( uint16_type p = 0; p < npoints; ++p ) { for ( uint16_type c1 = 0; c1 < mesh_element_type::nRealDim; ++c1 ) { __p(c1, e*npoints+p) = __c->xReal(p)[c1]; } for ( uint16_type c1 = 0; c1 < shape::M; ++c1 ) { __v( e*npoints*shape::M+shape::M*p+c1) = expr.evalq( c1, 0, p ); } } } break; case GeomapStrategyType::GEOMAP_O1: { __c1->update( __face_it->element( 0 ), __face_id ); DVLOG(2) << "[evaluator::GEOMAP_O1] FACE_ID = " << __face_it->id() << " ref pts=" << __c1->xRefs() << "\n"; DVLOG(2) << "[evaluator::GEOMAP_O1] FACE_ID = " << __face_it->id() << " real pts=" << __c1->xReal() << "\n"; map_gmc1_type mapgmc1( fusion::make_pair<vf::detail::gmc<0> >( __c1 ) ); expr1.update( mapgmc1 ); for ( uint16_type p = 0; p < npoints; ++p ) { for ( uint16_type c1 = 0; c1 < mesh_element_type::nRealDim; ++c1 ) { __p(c1, e*npoints+p) = __c1->xReal(p)[c1]; } for ( uint16_type c1 = 0; c1 < shape::M; ++c1 ) { __v( e*npoints*shape::M+shape::M*p+c1) = expr1.evalq( c1, 0, p ); } } } break; } } // face_it return boost::make_tuple( __v, __p ); }
void CheckCondition::checkIncorrectLogicOperator() { const bool printStyle = _settings->isEnabled("style"); const bool printWarning = _settings->isEnabled("warning"); if (!printWarning && !printStyle) return; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); for (std::size_t ii = 0; ii < functions; ++ii) { const Scope * scope = symbolDatabase->functionScopes[ii]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (!Token::Match(tok, "%oror%|&&") || !tok->astOperand1() || !tok->astOperand2()) continue; // Opposite comparisons around || or && => always true or always false if ((tok->astOperand1()->isName() || tok->astOperand2()->isName()) && isOppositeCond(true, _tokenizer->isCPP(), tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) { const bool alwaysTrue(tok->str() == "||"); incorrectLogicOperatorError(tok, tok->expressionString(), alwaysTrue, false); continue; } // 'A && (!A || B)' is equivalent with 'A && B' // 'A || (!A && B)' is equivalent with 'A || B' if (printStyle && ((tok->str() == "||" && tok->astOperand2()->str() == "&&") || (tok->str() == "&&" && tok->astOperand2()->str() == "||"))) { const Token* tok2 = tok->astOperand2()->astOperand1(); if (isOppositeCond(true, _tokenizer->isCPP(), tok->astOperand1(), tok2, _settings->library.functionpure)) { std::string expr1(tok->astOperand1()->expressionString()); std::string expr2(tok->astOperand2()->astOperand1()->expressionString()); std::string expr3(tok->astOperand2()->astOperand2()->expressionString()); if (expr1.length() + expr2.length() + expr3.length() > 50U) { if (expr1[0] == '!' && expr2[0] != '!') { expr1 = "!A"; expr2 = "A"; } else { expr1 = "A"; expr2 = "!A"; } expr3 = "B"; } const std::string cond1 = expr1 + " " + tok->str() + " (" + expr2 + " " + tok->astOperand2()->str() + " " + expr3 + ")"; const std::string cond2 = expr1 + " " + tok->str() + " " + expr3; redundantConditionError(tok, tok2->expressionString() + ". '" + cond1 + "' is equivalent to '" + cond2 + "'", false); continue; } } // Comparison #1 (LHS) const Token *comp1 = tok->astOperand1(); if (comp1 && comp1->str() == tok->str()) comp1 = comp1->astOperand2(); // Comparison #2 (RHS) const Token *comp2 = tok->astOperand2(); bool inconclusive = false; // Parse LHS bool not1; std::string op1, value1; const Token *expr1; if (!parseComparison(comp1, ¬1, &op1, &value1, &expr1, &inconclusive)) continue; // Parse RHS bool not2; std::string op2, value2; const Token *expr2; if (!parseComparison(comp2, ¬2, &op2, &value2, &expr2, &inconclusive)) continue; if (inconclusive && !_settings->inconclusive) continue; if (isSameExpression(_tokenizer->isCPP(), true, comp1, comp2, _settings->library.functionpure)) continue; // same expressions => only report that there are same expressions if (!isSameExpression(_tokenizer->isCPP(), true, expr1, expr2, _settings->library.functionpure)) continue; const bool isfloat = astIsFloat(expr1, true) || MathLib::isFloat(value1) || astIsFloat(expr2, true) || MathLib::isFloat(value2); // don't check floating point equality comparisons. that is bad // and deserves different warnings. if (isfloat && (op1 == "==" || op1 == "!=" || op2 == "==" || op2 == "!=")) continue; const double d1 = (isfloat) ? MathLib::toDoubleNumber(value1) : 0; const double d2 = (isfloat) ? MathLib::toDoubleNumber(value2) : 0; const MathLib::bigint i1 = (isfloat) ? 0 : MathLib::toLongNumber(value1); const MathLib::bigint i2 = (isfloat) ? 0 : MathLib::toLongNumber(value2); const bool useUnsignedInt = (std::numeric_limits<MathLib::bigint>::max()==i1)||(std::numeric_limits<MathLib::bigint>::max()==i2); const MathLib::biguint u1 = (useUnsignedInt) ? MathLib::toLongNumber(value1) : 0; const MathLib::biguint u2 = (useUnsignedInt) ? MathLib::toLongNumber(value2) : 0; // evaluate if expression is always true/false bool alwaysTrue = true, alwaysFalse = true; bool firstTrue = true, secondTrue = true; for (int test = 1; test <= 5; ++test) { // test: // 1 => testvalue is less than both value1 and value2 // 2 => testvalue is value1 // 3 => testvalue is between value1 and value2 // 4 => testvalue value2 // 5 => testvalue is larger than both value1 and value2 bool result1, result2; if (isfloat) { const double testvalue = getvalue<double>(test, d1, d2); result1 = checkFloatRelation(op1, testvalue, d1); result2 = checkFloatRelation(op2, testvalue, d2); } else if (useUnsignedInt) { const MathLib::biguint testvalue = getvalue<MathLib::biguint>(test, u1, u2); result1 = checkIntRelation(op1, testvalue, u1); result2 = checkIntRelation(op2, testvalue, u2); } else { const MathLib::bigint testvalue = getvalue<MathLib::bigint>(test, i1, i2); result1 = checkIntRelation(op1, testvalue, i1); result2 = checkIntRelation(op2, testvalue, i2); } if (not1) result1 = !result1; if (not2) result2 = !result2; if (tok->str() == "&&") { alwaysTrue &= (result1 && result2); alwaysFalse &= !(result1 && result2); } else { alwaysTrue &= (result1 || result2); alwaysFalse &= !(result1 || result2); } firstTrue &= !(!result1 && result2); secondTrue &= !(result1 && !result2); } const std::string cond1str = conditionString(not1, expr1, op1, value1); const std::string cond2str = conditionString(not2, expr2, op2, value2); if (printWarning && (alwaysTrue || alwaysFalse)) { const std::string text = cond1str + " " + tok->str() + " " + cond2str; incorrectLogicOperatorError(tok, text, alwaysTrue, inconclusive); } else if (printStyle && secondTrue) { const std::string text = "If '" + cond1str + "', the comparison '" + cond2str + "' is always " + (secondTrue ? "true" : "false") + "."; redundantConditionError(tok, text, inconclusive); } else if (printStyle && firstTrue) { //const std::string text = "The comparison " + cond1str + " is always " + // (firstTrue ? "true" : "false") + " when " + // cond2str + "."; const std::string text = "If '" + cond2str + "', the comparison '" + cond1str + "' is always " + (firstTrue ? "true" : "false") + "."; redundantConditionError(tok, text, inconclusive); } } } }
//on the reduced problem void solveIP(double time_limit, int ite, double gap){ IloEnv env; IloModel model(env); IloIntVarArray x(env, n_var, 0, 1); IloRangeArray con(env); IloExpr obj(env); int i,j,k; for(i=0;i<n;i++){ for(j=0;j<group[i].nItem;j++){ obj+=group[i].profit[j]*x[sum_n_item[i]+j]; } } model.add(IloMaximize(env, obj)); obj.end(); for(k=0;k<m;k++){ IloExpr expr(env); for(i=0;i<n;i++){ for(j=0;j<group[i].nItem;j++){ expr+=group[i].consume[j][k]*x[sum_n_item[i]+j]; } } con.add(expr<=Gb[k]); expr.end(); } for(i=0;i<n;i++){ IloExpr expr(env); for(j=0;j<group[i].nItem;j++){ expr+=x[sum_n_item[i]+j]; } con.add(expr==1); expr.end(); } /* int *tempFix=new int[n_var]; memset(tempFix, 0, n_var*sizeof(int)); for(i=0;i<rp.len;i++){ tempFix[rp.posi[i]]=1; } //force those not appeared in rp to 0 for(i=0;i<n;i++){ IloExpr expr(env); for(j=0;j<group[i].nItem;j++){ if(tempFix[sum_n_item[i]+j]==0){ expr+=x[sum_n_item[i]+j]; } } con.add(expr==0); expr.end(); } delete []tempFix; */ //add constrains from the last iteration if(ite>0){ for(k=0;k<ite;k++){ IloExpr expr(env); for(i=0;i<len_v1[k];i++){ expr+=x[pre_v1[k][i]]; } for(i=0;i<len_v0[k];i++){ expr-=x[pre_v0[k][i]]; } con.add(expr<=sum_pre_v1[k]-1); expr.end(); } } if(0&&len_rdCost0>0&&len_rdCost1>0){ IloExpr expr(env); for(i=0;i<len_rdCost1;i++){ expr+=(1-x[rdCost_sort1[i].posi])*rdCost_sort1[i].value; } for(i=0;i<len_rdCost0;i++){ expr+=x[rdCost_sort[i].posi]*rdCost_sort[i].value; } con.add(expr<=gap); expr.end(); } if(len_rdCost0>0){ printf("0 rdCostSort_len:%d. \n",len_rdCost0); k=1; int start=0, end; int cnt=0; //for(i=0;i<len_rdCost0;i++) printf("%lf, ", rdCost_sort[i].value); while(start<len_rdCost0){ double sum=0; for(i=start;i<len_rdCost0-k;i++){ sum=0; for(int ii=i;ii<i+k;ii++){ sum+=rdCost_sort[ii].value; } if(sum<gap) break; } if(i>=len_rdCost0-k && sum<gap) break; //printf("cut %d, start=%d, end=%d\n",k-1, start, i+k); end=i+k; IloExpr expr1(env); for(i=start;i<end;i++){ expr1+=x[rdCost_sort[i].posi]; } con.add(expr1<=k-1); expr1.end(); for(i=start;i<end;i++){ pre_v0[ite][cnt++]=rdCost_sort[i].posi; } start=end; k++; } len_v0[ite]=cnt; } if(len_rdCost1>0){ printf("1 rdCost_sort length:%d. \n",len_rdCost1); k=1; int start=0, end; int cnt=0; sum_pre_v1[ite]=0; //for(i=0;i<len_rdCost1;i++) printf("%lf, ", rdCost_sort1[i].value); while(start<len_rdCost1){ double sum=0; for(i=start;i<len_rdCost1-k;i++){ sum=0; for(int ii=i;ii<i+k;ii++){ sum+=rdCost_sort1[ii].value; } if(sum<gap) break; } if(i>=len_rdCost1-k && sum<gap) break; //printf("cut %d, start=%d, end=%d\n",k-1, start, i+k); end=i+k; IloExpr expr1(env); for(i=start;i<end;i++){ expr1+=x[rdCost_sort1[i].posi]; } int cnt1=(end-start)-(k-1); con.add(expr1>=cnt1); expr1.end(); for(i=start;i<end;i++){ pre_v1[ite][cnt++]=rdCost_sort1[i].posi; } sum_pre_v1[ite]=sum_pre_v1[ite]+(end-start); start=end; k++; } len_v1[ite]=cnt; } model.add(con); IloCplex cplex(model); cplex.setParam(IloCplex::TiLim, time_limit); cplex.setParam(IloCplex::Threads, 1); cplex.setParam(IloCplex::NodeFileInd,2); //cplex.setOut(env.getNullStream()); if(LB>0){ printf("\t add start point\n"); IloNumVarArray start_var(env); IloNumArray start_val(env); int k=0; for(i=0;i<n;i++){ for(j=0;j<group[i].nItem;j++){ if(bestSolVec[i]==j){ start_val.add(1); } else start_val.add(0); start_var.add(x[k]); k++; } } cplex.addMIPStart(start_var, start_val); start_var.end(); start_val.end(); } if(!cplex.solve()){ std::cout<<"not solved IP."<<std::endl; } else { std::cout<<"solution status "<<cplex.getStatus()<<std::endl; std::cout<<"solution value "<<cplex.getObjValue()<<std::endl; if((double)cplex.getObjValue()>LB){ LB=(double)cplex.getObjValue(); IloNumArray vals(env); cplex.getValues(vals, x); printf("new best solution vector is: "); for(i=0;i<n;i++){ for(j=0;j<group[i].nItem;j++){ if(cplex.getValue(x[sum_n_item[i]+j])>1-EPSILON){ printf("%d ", j); bestSolVec[i]=j; } } } printf("\n\n"); } } env.end(); }
inline void Not::jumping(const std::uint32_t &to, const std::uint32_t &from) { expr1()->jumping(from, to); }
inline std::string Not::to_string() const { return oper()->to_string() + " " + expr1()->to_string(); }
char * calculator (char in) { static char prev_in = equal; switch (in) { case add : case substract : case multiply : case divide : break; default: if (prev_in == equal) cur = text_start; break; } prev_in = in; if (in == equal) { int n; cur = text_start; if (expr1 (& n) && * cur == '\0') { int_to_string (n, text_start); cur = text_start + strlen (text_start); } else { strcpy (text_start, "error @ "); int_to_string ( cur - text_start + 1, text_start + strlen (text_start) ); cur = text_start; } return newline_before_text; } switch (in) { case add : in = '+'; break; case substract : in = '-'; break; case multiply : in = '*'; break; case divide : in = '/'; break; case parentheses: if (cur == text_start || strchr ("+-*/(", cur [-1])) in = '('; else in = ')'; break; default: in = '0' + in; break; } if (cur == buf + sizeof (buf) - 2) { strcpy (text_start, "buffer overflow @ "); int_to_string ( cur - text_start + 1, text_start + strlen (text_start) ); return newline_before_text; } * cur ++ = in; * cur = '\0'; return cur == text_start + 1 ? newline_before_text : cur - 1; }