Exemplo n.º 1
0
/*
 * Create a new OlaDaemon
 */
OlaDaemon::OlaDaemon(const OlaServer::Options &options,
                     ExportMap *export_map)
    : m_options(options),
      m_export_map(export_map),
      m_ss(m_export_map) {
  if (m_export_map) {
    IntegerVariable *var = m_export_map->GetIntegerVar(K_RPC_PORT_VAR);
    var->Set(FLAGS_rpc_port);
  }
}
Exemplo n.º 2
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 */
void SelectServerTest::testRemoteEndClose() {
  LoopbackDescriptor loopback_socket;
  loopback_socket.Init();
  loopback_socket.SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveFromSelectServer,
      reinterpret_cast<ConnectedDescriptor*>(&loopback_socket)));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // now the Write end closes
  loopback_socket.CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 3
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 * This uses the delete_on_close option.
 */
void SelectServerTest::testRemoteEndCloseWithDelete() {
  // Ownership is transferred to the SelectServer.
  LoopbackDescriptor *loopback_socket = new LoopbackDescriptor();
  loopback_socket->Init();
  loopback_socket->SetOnClose(NewSingleCallback(
      this, &SelectServerTest::Terminate));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback_socket, true));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // Now the Write end closes
  loopback_socket->CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 4
0
void 
LinearConstraint::addToSpace() const
{
	int size = _varsIDs->size();

	// vars is an array of the IntVar instances implicated in the constraint
	IntVarArgs vars(size);
	// coeffs is an array of integer coefficients for the variables
	IntArgs coeffs(size);

	for (int i=0; i<size; i++)
	{
		IntegerVariable *iv = _solver->varFromID(_varsIDs->at(i));
		vars[i] = _solver->getSpace()->getIntVar(iv->getTotalIndex());
		coeffs[i] = _varsCoeffs->at(i);
	}

//TODO: avant "Gecode::linear((Space*)_solver->getSpace(), coeffs, vars, _relType, _val);"
	Gecode::linear(*(Space*)_solver->getSpace(), coeffs, vars, _relType, _val); 
}
Exemplo n.º 5
0
/*
 * Test the interaction between read and write descriptor.
 */
void SelectServerTest::testReadWriteInteraction() {
  UnixSocket socket;
  socket.Init();
  socket.SetOnClose(NewSingleCallback(this, &SelectServerTest::Terminate));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));
  OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&socket));
  m_ss->RemoveWriteDescriptor(&socket);

  // now the Write end closes
  auto_ptr<UnixSocket> other_end(socket.OppositeEnd());
  other_end->CloseClient();

  m_ss->RegisterSingleTimeout(
      100, ola::NewSingleCallback(this, &SelectServerTest::FatalTimeout));
  m_ss->Run();
  m_ss->RemoveReadDescriptor(&socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 6
0
/*
 * Check that RemoveWriteDescriptor is reentrant.
 * We use the Execute() method to close a write descriptor during the same
 * cycle in which it becomes writeable. See
 * https://github.com/OpenLightingProject/ola/pull/429 for details.
 */
void SelectServerTest::testRemoveWriteWhenOtherReadable() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor *loopback = new LoopbackDescriptor();
  loopback->Init();
  loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler));

  write_set.insert(loopback);
  delete_set.insert(loopback);

  OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback));
  OLA_ASSERT_EQ(1, write_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  m_ss->Execute(NewSingleCallback(
      this, &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  m_ss->Run();
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 7
0
/*
 * Check that RemoveWriteDescriptor is reentrant.
 * Similar to the case above, but this removes & deletes the descriptor from
 * within the OnRead callback of the same descriptor.
 */
void SelectServerTest::testRemoveWriteWhenReadable() {
  // Ownership is transferred to the SelectServer.
  LoopbackDescriptor *loopback = new LoopbackDescriptor();
  loopback->Init();

  loopback->SetOnData(NewCallback(
      this, &SelectServerTest::ReadDataAndRemove,
      static_cast<ConnectedDescriptor*>(loopback)));
  loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback));
  OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, write_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // Send some data to make this descriptor readable.
  uint8_t data[] = {'a'};
  loopback->Send(data, arraysize(data));

  m_ss->Run();
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 8
0
/*
 * Check the delete_on_close feature handles the case where the descriptor
 * being closed is removed from the on_close handler.
 */
void SelectServerTest::testRemoteEndCloseWithRemoveAndDelete() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor *loopback = new LoopbackDescriptor();
  loopback->Init();

  read_set.insert(loopback);
  loopback->SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  // Ownership is transferred.
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback, true));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // Close the write end of the descriptor.
  loopback->CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
Exemplo n.º 9
0
/*
 * Confirm we correctly detect the remote end closing the connection.
 */
void SelectServerTest::testRemoteEndClose() {
  Descriptors read_set, write_set, delete_set;
  LoopbackDescriptor loopback;
  loopback.Init();

  read_set.insert(&loopback);

  loopback.SetOnClose(NewSingleCallback(
      this, &SelectServerTest::RemoveAndDeleteDescriptors,
      read_set, write_set, delete_set));

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());

  // now the Write end closes
  loopback.CloseClient();

  m_ss->Run();
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
BinaryVariable *HammingPitchAdjustment::adjust(Problem *problem, int variable, Variable* value, Random* random) {
    /* Resulting variable */
    BinaryVariable *result = NULL;

    /* Get fret width for the variable */
    IntegerVariable *fw = (IntegerVariable *) problem->getFretWidth(variable);

    /* Count the number of bits that the variable can have */
    BinaryVariable *max = (BinaryVariable *) problem->getMaximum(variable);
    unsigned int length = max->countBitsSet();

    /* Decide how many bits will be flipped */
    unsigned int hammingDistance = random->boundedIntegerRandom(1, fw->getValue()+1);

    /* 
     * Randomly decide which bits will flip building a mask that has 1 in
     * positions where the variable bit will flip.
     */
    unsigned int count = 0;
    BinaryVariableBuilder *builder = (BinaryVariableBuilder *) problem->getVariableBuilder();
    BinaryVariable *mask = builder->createVariable(length);
    while (count < hammingDistance) {
        unsigned int pos = random->boundedIntegerRandom(0, length);

        /*
         * Check not to select an already selected bit.
         */
        if (!mask->getBit(pos)) {
            mask->setBit(1, pos);
            count++;
        }
    }

    result = builder->mutateVariable((BinaryVariable *) value, mask);

    delete mask;
    return result;
}
Exemplo n.º 11
0
/*
 * Confirm we can't add invalid descriptors to the SelectServer
 */
void SelectServerTest::testAddInvalidDescriptor() {
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());  // internal socket
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Adding and removing a uninitialized socket should fail
  LoopbackDescriptor bad_socket;
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket));
  OLA_ASSERT_FALSE(m_ss->AddWriteDescriptor(&bad_socket));
  m_ss->RemoveReadDescriptor(&bad_socket);
  m_ss->RemoveWriteDescriptor(&bad_socket);

  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());  // internal socket
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
}
Exemplo n.º 12
0
/*
 * Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
 * export map is updated.
 */
void SelectServerTest::testAddRemoveReadDescriptor() {
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  LoopbackDescriptor loopback;
  loopback.Init();

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Add a udp socket
  UDPSocket udp_socket;
  OLA_ASSERT_TRUE(udp_socket.Init());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Check remove works
  m_ss->RemoveReadDescriptor(&loopback);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  m_ss->RemoveReadDescriptor(&udp_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
}
Exemplo n.º 13
0
/*
 * Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
 * export map is updated.
 */
void SelectServerTest::testAddRemoveReadDescriptor() {
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  LoopbackDescriptor bad_socket;
  // adding and removing a non-connected socket should fail
  OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket));
  m_ss->RemoveReadDescriptor(&bad_socket);

  LoopbackDescriptor loopback_socket;
  loopback_socket.Init();

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Add a udp socket
  UDPSocket udp_socket;
  OLA_ASSERT_TRUE(udp_socket.Init());
  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Check remove works
  m_ss->RemoveReadDescriptor(&loopback_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  m_ss->RemoveReadDescriptor(&udp_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());
}
Exemplo n.º 14
0
/*
 * Confirm we can't add the same descriptor twice.
 */
void SelectServerTest::testDoubleAddAndRemove() {
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());  // internal socket
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  LoopbackDescriptor loopback_socket;
  loopback_socket.Init();

  OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback_socket));
  OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, write_descriptor_count->Get());

  m_ss->RemoveReadDescriptor(&loopback_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(1, write_descriptor_count->Get());

  m_ss->RemoveWriteDescriptor(&loopback_socket);
  OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, read_descriptor_count->Get());
  OLA_ASSERT_EQ(0, write_descriptor_count->Get());

  // Trying to remove a second time shouldn't crash
  m_ss->RemoveReadDescriptor(&loopback_socket);
  m_ss->RemoveWriteDescriptor(&loopback_socket);
}
Exemplo n.º 15
0
// rebuild a Gecode space with the current variable and constraints
void
Solver::updateState()
{
	if (_space)
	{
		delete _space;
		_space = NULL;
	}

	_space = new CustomSpace();

	// create gecode variables
	for (map<int, IntegerVariable*>::iterator q = _integerVariablesMap->begin(); q != _integerVariablesMap->end(); q++)
	{
		IntegerVariable *v = q->second;

		if ((_suggest) && (_maxModification != NO_MAX_MODIFICATION)) {
			v->adjustMinMax(_suggest, _maxModification);
		} else {
			v->adjustMinMax(_suggest);
		}


		v->setIndex(_space->addVariable(v->getVal(), v->getVal()));
		v->setPosDeltaIndex(_space->addVariable(0, v->getMax() - v->getVal() + 1));
		v->setNegDeltaIndex(_space->addVariable(0, v->getVal() - v->getMin() + 1));
		v->setTotalIndex(_space->addVariable(v->getMin(), v->getMax()));
	}

	// add constraints to space
	for (map<int, LinearConstraint*>::iterator p = _constraintsMap->begin(); p != _constraintsMap->end(); p++)
	{
		(p->second)->addToSpace();
	}

	LinExpr expr;
	bool init=false;

	// construct the linear combination of delta variables balanced by the weight associated with their type
	for (map<int, IntegerVariable*>::iterator q = _integerVariablesMap->begin(); q != _integerVariablesMap->end(); q++)
	{
		IntegerVariable *currVar = q->second;

		// add a delta variable for each beginning or length
		int multiplier = 1;

		bool isStrong = false;

		// give more weight to the edited variables
		if (_suggest)
		{
			for (unsigned int i=0; i<_strongVars->size(); i++)
				if (_strongVars->at(i) == q->first)
				{
					isStrong = true;
					//multiplier = 10;
					break;
				}
		}

		IntVarArgs vars(4);
		IntArgs coeffs(4);

		if (isStrong) {
			vars[0] = _space->getIntVar(currVar->getTotalIndex());
			vars[1] = _space->getIntVar(currVar->getIndex());
			vars[2] = _space->getIntVar(currVar->getTotalIndex());
			vars[3] = _space->getIntVar(currVar->getIndex());

			coeffs[0] = -1;
			coeffs[1] = 1;
			coeffs[2] = -1;
			coeffs[3] = 1;
		} else {
			// constraint : <initial or wanted value> + <positive delta> - <negative delta> = <optimal value>
			vars[0] = _space->getIntVar(currVar->getNegDeltaIndex());
			vars[1] = _space->getIntVar(currVar->getPosDeltaIndex());
			vars[2] = _space->getIntVar(currVar->getTotalIndex());
			vars[3] = _space->getIntVar(currVar->getIndex());
			coeffs[0] = -1;
			coeffs[1] = 1;
			coeffs[2] = 1;
			coeffs[3] = -1;
		}

		linear(*_space, coeffs, vars, IRT_EQ, 0);

		// construction of the objective function
		if (!init)
		{
			expr = LinExpr(vars[0], currVar->getWeight()*multiplier);

			init = true;

			LinExpr tmp(vars[1], currVar->getWeight()*multiplier);

			expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp);
		}
		else
		{
			LinExpr tmp(vars[0], currVar->getWeight()*multiplier);

			expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp);

			tmp = LinExpr(vars[1], currVar->getWeight()*multiplier);

			expr = LinExpr(expr, Gecode::LinExpr::NT_ADD, tmp);
		}
	}

	// the objective function is a linear combination of the delta variables (lengths are more important than beginnings)
	_space->setObjFunc(Gecode::expr(*_space, expr));
}
Exemplo n.º 16
0
 void visit(IntegerVariable& var)
 {
     p_value = static_cast<double>(var.initial());
 }
Exemplo n.º 17
0
 void visit(IntegerVariable& var)
 {
     var.initial(static_cast<int>(p_value));
 }