コード例 #1
0
ファイル: blame.cpp プロジェクト: AljGaber/imp
void assign_blame(const RestraintsTemp &rs,
                  const ParticlesTemp &ps, FloatKey attribute) {
  IMP_FUNCTION_LOG;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    if (ps[i]->has_attribute(attribute)) {
      ps[i]->set_value(attribute, 0);
    } else {
      ps[i]->add_attribute(attribute, 0, false);
    }
  }
  Restraints drs;
  for (unsigned int i = 0; i < rs.size(); ++i) {
    Pointer<Restraint> rd = rs[i]->create_decomposition();
    if (rd) {
      drs.push_back(rd);
    }
  }
  IMP_NEW(RestraintsScoringFunction, rsf, (drs));
  rsf->evaluate(false);
  DependencyGraph dg = get_dependency_graph(IMP::internal::get_model(rs));
  // attempt to get around boost/gcc bug and the most vexing parse
  DependencyGraphVertexIndex dgi((IMP::get_vertex_index(dg)));
  ControlledBy controlled_by;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    ParticlesTemp cps = get_dependent_particles(ps[i], ps, dg, dgi);
    IMP_INTERNAL_CHECK(cps.size() > 0, "No dependent particles for " << ps[i]);
    for (unsigned int j = 0; j < cps.size(); ++j) {
      controlled_by[cps[j]] = ps[i];
    }
  }
  for (unsigned int i = 0; i < drs.size(); ++i) {
    distribute_blame(drs[i], controlled_by, attribute, 1.0);
  }
}
コード例 #2
0
void MDNetworkParticipant::network_datagram(Datagram &dg)
{
	DatagramIterator dgi(dg);
	unsigned short channels = dgi.read_uint8();
	if(channels == 1 && dgi.read_uint64() == CONTROL_MESSAGE)
	{
		unsigned short msg_type = dgi.read_uint16();
		switch(msg_type)
		{
			case CONTROL_ADD_CHANNEL:
			{
				subscribe_channel(dgi.read_uint64());
			}
			break;
			case CONTROL_REMOVE_CHANNEL:
			{
				unsubscribe_channel(dgi.read_uint64());
			}
			break;
			case CONTROL_ADD_RANGE:
			{
				channel_t lo = dgi.read_uint64();
				channel_t hi = dgi.read_uint64();
				subscribe_range(lo, hi);
			}
			break;
			case CONTROL_REMOVE_RANGE:
			{
				channel_t lo = dgi.read_uint64();
				channel_t hi = dgi.read_uint64();
				unsubscribe_range(lo, hi);
			}
			break;
			case CONTROL_ADD_POST_REMOVE:
			{
				add_post_remove(dgi.read_string());
			}
			break;
			case CONTROL_CLEAR_POST_REMOVE:
			{
				clear_post_removes();
			}
			break;
			default:
				logger().error() << "MDNetworkParticipant got unknown control message, type : " << msg_type << std::endl;
		}
		return;
	}
	send(dg);
}
コード例 #3
0
ファイル: EventLogger.cpp プロジェクト: Astron/Astron
void EventLogger::process_packet(DatagramHandle dg, const uvw::Addr& sender)
{
    DatagramIterator dgi(dg);
    std::stringstream stream;

    try {
        msgpack_decode(stream, dgi);
    } catch(DatagramIteratorEOF&) {
        m_log.error() << "Received truncated packet from "
                      << sender.ip << ":" << sender.port << std::endl;
        return;
    }

    if(dgi.tell() != dg->size()) {
        m_log.error() << "Received packet with extraneous data from "
                      << sender.ip << ":" << sender.port << std::endl;
        return;
    }

    std::string data = stream.str();
    m_log.trace() << "Received: " << data << std::endl;

    // This is a little bit of a kludge, but we should make sure we got a
    // MessagePack map as the event log element, and not some other type. The
    // easiest way to do this is to make sure that the JSON representation
    // begins with {
    if(data[0] != '{') {
        m_log.error() << "Received non-map event log from "
                      << sender.ip << ":" << sender.port
                      << ": " << data << std::endl;
        return;
    }

    // Now let's insert our timestamp:
    time_t rawtime;
    time(&rawtime);

    char timestamp[64];
    strftime(timestamp, 64, "{\"_time\": \"%Y-%m-%d %H:%M:%S%z\", ", localtime(&rawtime));

    *m_file.get() << timestamp << data.substr(1) << std::endl;
}
コード例 #4
0
 void InternalConnection::handle_datagram(const Datagram &dg)
 {
     uint8_t channels = 0;
     DatagramIterator dgi(dg);
     
     try {
         
         channels = dgi.read_uint8();
         DatagramIterator msg_dgi(dg, 1 + channels * sizeof(channel_t));
         handle_datagram(dg, msg_dgi);
         
         
         
     } catch (DatagramIteratorEOF &e) {
         //Really, if this has been sent to us by the MD... we really shouldn't have an issue.
         //But as always, better safe then sorry.
         std::cout << "Detected truncated datagram in handle_datagram" << std::endl;
         
     }
     
     
     
     
 }
コード例 #5
0
void FncKhunTucker::jacobian(const IntervalVector& x_lambda, IntervalMatrix& J, const BitSet& components, int v) const {

	if (components.size()!=n+nb_mult) {
		not_implemented("FncKhunTucker: 'jacobian' for selected components");
		//J.resize(n+nb_mult,n+nb_mult);
	}

	IntervalVector x=x_lambda.subvector(0,n-1);

	int lambda0=n;	// The index of lambda0 in the box x_lambda is nb_var.

	int l=lambda0; // mutipliers indices counter. The first multiplier is lambda0.

	// matrix corresponding to the "Hessian expression" lambda_0*d^2f+lambda_1*d^2g_1+...=0
	IntervalMatrix hessian=x_lambda[l] * df.jacobian(x,v<n? v : -1); // init
	if (v==-1 || v==l) J.put(0, l, df.eval_vector(x), false);

	l++;

	IntervalVector gx;
	if (!active.empty())
		gx = sys.f_ctrs.eval_vector(x,active);

	// normalization equation (init)
	J[lambda0].put(0,Vector::zeros(n));
	J[lambda0][lambda0]=1.0;

	IntervalVector dgi(n); // store dg_i([x]) (used in several places)

	for (BitSet::const_iterator i=ineq.begin(); i!=ineq.end(); ++i) {
		hessian += x_lambda[l] * dg[i].jacobian(x,v<n? v : -1);
		dgi=dg[i].eval_vector(x);
		J.put(0, l, dgi, false);

		J.put(l, 0, (x_lambda[l]*dgi), true);
		J.put(l, n, Vector::zeros(nb_mult), true);
		J[l][l]=gx[i];

		J[lambda0][l] = 1.0;

		l++;
	}

	for (BitSet::const_iterator i=ineq.begin(); i!=ineq.end(); ++i) {
		hessian += x_lambda[l] * dg[i].jacobian(x,v<n? v : -1);
		dgi=dg[i].eval_vector(x);
		J.put(0, l, dgi, false);

		J.put(l, 0, dgi, true);
		J.put(l, n, Vector::zeros(nb_mult), true);

		J[lambda0][l] = 2*x_lambda[l];

		l++;
	}

	for (BitSet::const_iterator v=bound_left.begin(); v!=bound_left.end(); ++v) {
		// this constraint does not contribute to the "Hessian expression"
		dgi=Vector::zeros(n);
		dgi[v]=-1.0;
		J.put(0, l, dgi, false);

		J.put(l, 0, (x_lambda[l]*dgi), true);
		J.put(l, n, Vector::zeros(nb_mult), true);
		J[l][l]=(-x[v]+sys.box[v].lb());

		J[lambda0][l] = 1.0;
		l++;
	}


	for (BitSet::const_iterator v=bound_right.begin(); v!=bound_right.end(); ++v) {
		// this constraint does not contribute to the "Hessian expression"
		dgi=Vector::zeros(n);
		dgi[v]=1.0;
		J.put(0, l, dgi, false);

		J.put(l, 0, (x_lambda[l]*dgi), true);
		J.put(l, n, Vector::zeros(nb_mult), true);
		J[l][l]=(x[v]-sys.box[v].ub());

		J[lambda0][l] = 1.0;
		l++;
	}

	assert(l==nb_mult+n);

	J.put(0,0,hessian);

}
コード例 #6
0
void MDNetworkParticipant::receive_datagram(DatagramHandle dg)
{
	DatagramIterator dgi(dg);
	uint16_t channels = dgi.read_uint8();
	if(channels == 1 && dgi.read_channel() == CONTROL_MESSAGE)
	{
		uint16_t msg_type = dgi.read_uint16();
		switch(msg_type)
		{
			case CONTROL_ADD_CHANNEL:
			{
				subscribe_channel(dgi.read_channel());
				break;
			}
			case CONTROL_REMOVE_CHANNEL:
			{
				unsubscribe_channel(dgi.read_channel());
				break;
			}
			case CONTROL_ADD_RANGE:
			{
				channel_t lo = dgi.read_channel();
				channel_t hi = dgi.read_channel();
				subscribe_range(lo, hi);
				break;
			}
			case CONTROL_REMOVE_RANGE:
			{
				channel_t lo = dgi.read_channel();
				channel_t hi = dgi.read_channel();
				unsubscribe_range(lo, hi);
				break;
			}
			case CONTROL_ADD_POST_REMOVE:
			{
				add_post_remove(dgi.read_datagram());
				break;
			}
			case CONTROL_CLEAR_POST_REMOVE:
			{
				clear_post_removes();
				break;
			}
			case CONTROL_SET_CON_NAME:
			{
				set_con_name(dgi.read_string());
				break;
			}
			case CONTROL_SET_CON_URL:
			{
				set_con_url(dgi.read_string());
				break;
			}
			default:
				logger().error() << "MDNetworkParticipant got unknown control message, type : "
				                 << msg_type << std::endl;
		}
		return;
	}
	route_datagram(dg);
}
コード例 #7
0
DatasetReference StaticGeometryHandler::load_dataset(const Filename &src) {

    // TODO: Check for duplicates
    // for (DatasetList::const_iterator iter = _datasets.cbegin(); iter != _datasets.cend(); ++iter) {
    // }

    ifstream infile(src.get_fullpath(), ios::in | ios::binary | ios::ate);

    if (!infile.is_open()) {
        cout << "ERROR: Could not open " << src.get_fullpath() << "!" << endl;
        return -1;
    }

    // Extract filesize and reset filepointer afterwards
    size_t fsize = infile.tellg();
    infile.seekg(0, ios::beg);

    // Read file content
    char *data = new char[fsize];
    infile.read(data, fsize);

    // Construct datagram and an iterator from file content
    Datagram dg(data, fsize);
    DatagramIterator dgi(dg);

    // For now we assume the file is structured correctly, and the header is
    // correct aswell
    string header = dgi.get_fixed_string(4);
    size_t tri_group_size = dgi.get_uint32();

    if (tri_group_size != SG_TRI_GROUP_SIZE) {
        cout << "ERROR: The rpsg file uses a tri-group size of " << tri_group_size << ", but expected"
            << " a size of "<< SG_TRI_GROUP_SIZE << endl;
        cout << "Please regenerate the file!" << endl;
        delete [] data;
        return -1;    
    } 

    size_t num_strips = dgi.get_uint32();

    if (num_strips >= SG_MAX_DATASET_STRIPS) {
        cout << "ERROR! Dataset exceeds maximum amount of " << SG_MAX_DATASET_STRIPS << " strips (has " << num_strips << ")!" << endl;
        delete [] data;
        return -1;
    }

    SGDataset *dataset = new SGDataset();
    dataset->read_bounds(dgi);

    // Read in all strips and store them
    PTA_uchar dataset_handle = _dataset_tex->modify_ram_image();
    for (size_t i = 0; i < num_strips; ++i) {
        SGTriangleStrip* strip = new  SGTriangleStrip();
        strip->load_from_datagram(dgi);
        strip->write_to(dataset_handle, _dataset_index++);
        dataset->attach_strip(strip);
    }

    PTA_uchar mapping_handle = _mapping_tex->modify_ram_image();

    dataset->write_mappings(mapping_handle, _datasets.size());

    if (dgi.get_remaining_size() != 0) {
        cout << "Corrupt RPSG file! " << dgi.get_remaining_size() << " bytes left!" << endl;
    }

    // Write out debug textures?
    // _dataset_tex->write("dataset.png");
    // _mapping_tex->write("mappings.png");

    // Attach dataset, clean up the variables, and finally return a handle to the dataset
    _datasets.push_back(dataset);
    delete [] data;

    return _datasets.size() - 1;
}