Example #1
0
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
from ddp import DDP



# Simple test of using the linear pair cost...
dp = DDP()

dp.prepare(12, 5)

uc = numpy.zeros((12, 5), dtype=numpy.float32)
uc[0,:]  = [0.0, 5.0, 5.0, 5.0, 5.0]
uc[2,:]  = [5.0, 5.0, 0.0, 5.0, 5.0]
uc[5,:]  = [5.0, 5.0, 5.0, 5.0, 0.0]
uc[8,:]  = [5.0, 5.0, 0.0, 5.0, 5.0]
uc[11,:] = [0.0, 5.0, 5.0, 5.0, 5.0]
dp.unary(0, uc)


pc = numpy.ones((11, 1), dtype=numpy.float32)
pc *= 0.5
dp.pairwise(0, ['linear'] * 11, pc)
Example #2
0
#! /usr/bin/env python
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.



from ddp import DDP



for name in DDP.names():
  print '%s:' % name
  
  desc = DDP.description(name)
  for i in xrange(0, len(desc), 60):
    print '    %s' % desc[i:i+60].strip()
Example #3
0
#! /usr/bin/env python

# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
from ddp import DDP

# More advanced test of the linear node; makes use of the linear offset with variable numbers of nodes...
dp = DDP()

dp.prepare([3, 5, 7, 9, 7, 5, 3])

dp.unary(0, [10.0, 0.0, 10.0])
dp.unary(3, [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 0.0])
dp.unary(6, [10.0, 0.0, 10.0])

pc = numpy.empty((6, 4), dtype=numpy.float32)
pc[:, 0] = [1.0 / 3.0, 1.0 / 5.0, 1.0 / 7.0, 1.0 / 9.0, 1.0 / 7.0, 1.0 / 5.0]
pc[:, 1] = [0.2, 0.2, 0.2, -0.2, -0.2, -0.2]
pc[:, 2] = [3.0 / 5.0, 5.0 / 7.0, 7.0 / 9.0, 9.0 / 7.0, 7.0 / 5.0, 5.0 / 3.0]
pc[:, 3] = 0.3

dp.pairwise(0, ['linear'] * 6, pc)

best, cost = dp.best()
Example #4
0
#! /usr/bin/env python
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

from ddp import DDP

for name in DDP.names():
    print '%s:' % name

    desc = DDP.description(name)
    for i in xrange(0, len(desc), 60):
        print '    %s' % desc[i:i + 60].strip()
Example #5
0
#! /usr/bin/env python

# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

from ddp import DDP



# Simple test of the different pair cost...
dp = DDP()

dp.prepare(9, 3)

dp.unary(0, [0.0, 1.0, 2.0])
dp.unary(4, [2.0, 1.0, 0.0])
dp.unary(8, [0.0, 1.0, 2.0])

dp.pairwise(0, ['different'] * 8, [[0.5]] * 8)


dp.solve()

best, cost = dp.best()

print 'Best cost = %.1f' % cost
Example #6
0
#! /usr/bin/env python

# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

from ddp import DDP

# Simple test of the different pair cost...
dp = DDP()

dp.prepare(9, 3)

dp.unary(0, [0.0, 1.0, 2.0])
dp.unary(4, [2.0, 1.0, 0.0])
dp.unary(8, [0.0, 1.0, 2.0])

dp.pairwise(0, ['different'] * 8, [[0.5]] * 8)

dp.solve()

best, cost = dp.best()

print 'Best cost = %.1f' % cost
print 'Best solution: %s' % str(best)

print 'Costs:'
Example #7
0
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
from ddp import DDP



# Test of the ordered node, used when you have a fixed sequence and want to infer the splits...
dp = DDP()
dp.prepare(32, 4)

uc = numpy.empty((32, 4), dtype=numpy.float32)
uc[:, 0] = 1.0 - numpy.cos(0.0*numpy.pi + 3.0 * numpy.pi * numpy.arange(32) / 31.0)
uc[:, 1] = 1.0 - numpy.cos(1.0*numpy.pi + 3.0 * numpy.pi * numpy.arange(32) / 31.0)
uc[:, 2] = 1.0 - numpy.cos(2.0*numpy.pi + 3.0 * numpy.pi * numpy.arange(32) / 31.0)
uc[:, 3] = 1.0 - numpy.cos(3.0*numpy.pi + 3.0 * numpy.pi * numpy.arange(32) / 31.0)
uc[0, 1:] = float('inf')
dp.unary(0, uc)

dp.pairwise(0, ['ordered'] * 32, [[0.0, 5.0]] * 32)



best, cost = dp.best(3)
Example #8
0
#! /usr/bin/env python

# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

from ddp import DDP



# Test that putting multiple problems into a single solver and solving them all at once works, even though I have no clue why you would ever want to do this...
dp = DDP()

dp.prepare(14, 3)

dp.unary(0, [0.0, 4.0, 4.0])
dp.unary(3, [4.0, 4.0, 0.0])
dp.unary(6, [0.0, 4.0, 4.0])
dp.unary(7, [4.0, 4.0, 0.0])
dp.unary(10, [0.0, 4.0, 4.0])
dp.unary(13, [4.0, 4.0, 0.0])

dp.pairwise(0, ['different'] * 13, [[0.5]] * 13)
dp.pairwise(6, '', None)


Example #9
0
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
from ddp import DDP



# More advanced test of the linear node; makes use of the linear offset with variable numbers of nodes...
dp = DDP()

dp.prepare([3, 5, 7, 9, 7, 5, 3])

dp.unary(0, [10.0, 0.0, 10.0])
dp.unary(3, [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 0.0])
dp.unary(6, [10.0, 0.0, 10.0])

pc = numpy.empty((6, 4), dtype=numpy.float32)
pc[:,0] = [1.0/3.0, 1.0/5.0, 1.0/7.0, 1.0/9.0, 1.0/7.0, 1.0/5.0]
pc[:,1] = [0.2, 0.2, 0.2, -0.2, -0.2, -0.2]
pc[:,2] = [3.0/5.0, 5.0/7.0, 7.0/9.0, 9.0/7.0, 7.0/5.0, 5.0/3.0]
pc[:,3] = 0.3

dp.pairwise(0, ['linear'] * 6, pc)
Example #10
0
#! /usr/bin/env python

# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

from ddp import DDP

# Test that putting multiple problems into a single solver and solving them all at once works, even though I have no clue why you would ever want to do this...
dp = DDP()

dp.prepare(14, 3)

dp.unary(0, [0.0, 4.0, 4.0])
dp.unary(3, [4.0, 4.0, 0.0])
dp.unary(6, [0.0, 4.0, 4.0])
dp.unary(7, [4.0, 4.0, 0.0])
dp.unary(10, [0.0, 4.0, 4.0])
dp.unary(13, [4.0, 4.0, 0.0])

dp.pairwise(0, ['different'] * 13, [[0.5]] * 13)
dp.pairwise(6, '', None)

best, cost = dp.best()

print 'Best cost = %.1f' % cost
print 'Best solution: %s' % str(best)
Example #11
0
# Copyright 2016 Tom SF Haines

# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import numpy
from ddp import DDP



# Test the full type, where you provide a complete cost matrix...
dp = DDP()
dp.prepare(9, 3)

dp.unary(0, [0.0, 5.0, 5.0])

cyclic = numpy.ones((3,3), dtype=numpy.float32)
cyclic[0,1] = 0.0
cyclic[1,2] = 0.0
cyclic[2,0] = 0.0
dp.pairwise(0, ['full'] * 8, numpy.repeat(cyclic[numpy.newaxis,:,:], 8, axis=0))



best, cost = dp.best()

print 'Best cost = %.1f' % cost